summaryrefslogtreecommitdiffstats
path: root/src/dma/vendor/github.com/go-redis/redis/internal/proto/write_buffer.go
blob: 664f4c333295402ed14bb996c1fe2cfc70ec80f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package proto

import (
	"encoding"
	"fmt"
	"strconv"
)

type WriteBuffer struct {
	b []byte
}

func NewWriteBuffer() *WriteBuffer {
	return &WriteBuffer{
		b: make([]byte, 0, 4096),
	}
}

func (w *WriteBuffer) Len() int      { return len(w.b) }
func (w *WriteBuffer) Bytes() []byte { return w.b }
func (w *WriteBuffer) Reset()        { w.b = w.b[:0] }

func (w *WriteBuffer) Append(args []interface{}) error {
	w.b = append(w.b, ArrayReply)
	w.b = strconv.AppendUint(w.b, uint64(len(args)), 10)
	w.b = append(w.b, '\r', '\n')

	for _, arg := range args {
		if err := w.append(arg); err != nil {
			return err
		}
	}
	return nil
}

func (w *WriteBuffer) append(val interface{}) error {
	switch v := val.(type) {
	case nil:
		w.AppendString("")
	case string:
		w.AppendString(v)
	case []byte:
		w.AppendBytes(v)
	case int:
		w.AppendString(formatInt(int64(v)))
	case int8:
		w.AppendString(formatInt(int64(v)))
	case int16:
		w.AppendString(formatInt(int64(v)))
	case int32:
		w.AppendString(formatInt(int64(v)))
	case int64:
		w.AppendString(formatInt(v))
	case uint:
		w.AppendString(formatUint(uint64(v)))
	case uint8:
		w.AppendString(formatUint(uint64(v)))
	case uint16:
		w.AppendString(formatUint(uint64(v)))
	case uint32:
		w.AppendString(formatUint(uint64(v)))
	case uint64:
		w.AppendString(formatUint(v))
	case float32:
		w.AppendString(formatFloat(float64(v)))
	case float64:
		w.AppendString(formatFloat(v))
	case bool:
		if v {
			w.AppendString("1")
		} else {
			w.AppendString("0")
		}
	case encoding.BinaryMarshaler:
		b, err := v.MarshalBinary()
		if err != nil {
			return err
		}
		w.AppendBytes(b)
	default:
		return fmt.Errorf(
			"redis: can't marshal %T (consider implementing encoding.BinaryMarshaler)", val)
	}
	return nil
}

func (w *WriteBuffer) AppendString(s string) {
	w.b = append(w.b, StringReply)
	w.b = strconv.AppendUint(w.b, uint64(len(s)), 10)
	w.b = append(w.b, '\r', '\n')
	w.b = append(w.b, s...)
	w.b = append(w.b, '\r', '\n')
}

func (w *WriteBuffer) AppendBytes(p []byte) {
	w.b = append(w.b, StringReply)
	w.b = strconv.AppendUint(w.b, uint64(len(p)), 10)
	w.b = append(w.b, '\r', '\n')
	w.b = append(w.b, p...)
	w.b = append(w.b, '\r', '\n')
}

func formatInt(n int64) string {
	return strconv.FormatInt(n, 10)
}

func formatUint(u uint64) string {
	return strconv.FormatUint(u, 10)
}

func formatFloat(f float64) string {
	return strconv.FormatFloat(f, 'f', -1, 64)
}
pan>&err) } return int(ret), nil } // See also https://libvirt.org/html/libvirt-libvirt-event.html#virEventUpdateTimeout func EventUpdateTimeout(timer int, freq int) { C.virEventUpdateTimeout((C.int)(timer), (C.int)(freq)) } // See also https://libvirt.org/html/libvirt-libvirt-event.html#virEventRemoveTimeout func EventRemoveTimeout(timer int) error { var err C.virError ret := C.virEventRemoveTimeoutWrapper((C.int)(timer), &err) if ret < 0 { return makeError(&err) } return nil } type EventHandleCallbackInfo struct { callback uintptr opaque uintptr free uintptr } type EventTimeoutCallbackInfo struct { callback uintptr opaque uintptr free uintptr } func (i *EventHandleCallbackInfo) Invoke(watch int, fd int, event EventHandleType) { C.eventHandleCallbackInvoke(C.int(watch), C.int(fd), C.int(event), C.uintptr_t(i.callback), C.uintptr_t(i.opaque)) } func (i *EventTimeoutCallbackInfo) Invoke(timer int) { C.eventTimeoutCallbackInvoke(C.int(timer), C.uintptr_t(i.callback), C.uintptr_t(i.opaque)) } func (i *EventHandleCallbackInfo) Free() { C.eventHandleCallbackFree(C.uintptr_t(i.free), C.uintptr_t(i.opaque)) } func (i *EventTimeoutCallbackInfo) Free() { C.eventTimeoutCallbackFree(C.uintptr_t(i.free), C.uintptr_t(i.opaque)) } type EventLoop interface { AddHandleFunc(fd int, event EventHandleType, callback *EventHandleCallbackInfo) int UpdateHandleFunc(watch int, event EventHandleType) RemoveHandleFunc(watch int) int AddTimeoutFunc(freq int, callback *EventTimeoutCallbackInfo) int UpdateTimeoutFunc(timer int, freq int) RemoveTimeoutFunc(timer int) int } var eventLoopImpl EventLoop // See also https://libvirt.org/html/libvirt-libvirt-event.html#virEventRegisterImpl func EventRegisterImpl(impl EventLoop) { eventLoopImpl = impl C.virEventRegisterImplWrapper() } //export eventAddHandleFunc func eventAddHandleFunc(fd C.int, event C.int, callback uintptr, opaque uintptr, free uintptr) C.int { if eventLoopImpl == nil { panic("Event loop impl is missing") } cbinfo := &EventHandleCallbackInfo{ callback: callback, opaque: opaque, free: free, } return C.int(eventLoopImpl.AddHandleFunc(int(fd), EventHandleType(event), cbinfo)) } //export eventUpdateHandleFunc func eventUpdateHandleFunc(watch C.int, event C.int) { if eventLoopImpl == nil { panic("Event loop impl is missing") } eventLoopImpl.UpdateHandleFunc(int(watch), EventHandleType(event)) } //export eventRemoveHandleFunc func eventRemoveHandleFunc(watch C.int) { if eventLoopImpl == nil { panic("Event loop impl is missing") } eventLoopImpl.RemoveHandleFunc(int(watch)) } //export eventAddTimeoutFunc func eventAddTimeoutFunc(freq C.int, callback uintptr, opaque uintptr, free uintptr) C.int { if eventLoopImpl == nil { panic("Event loop impl is missing") } cbinfo := &EventTimeoutCallbackInfo{ callback: callback, opaque: opaque, free: free, } return C.int(eventLoopImpl.AddTimeoutFunc(int(freq), cbinfo)) } //export eventUpdateTimeoutFunc func eventUpdateTimeoutFunc(timer C.int, freq C.int) { if eventLoopImpl == nil { panic("Event loop impl is missing") } eventLoopImpl.UpdateTimeoutFunc(int(timer), int(freq)) } //export eventRemoveTimeoutFunc func eventRemoveTimeoutFunc(timer C.int) { if eventLoopImpl == nil { panic("Event loop impl is missing") } eventLoopImpl.RemoveTimeoutFunc(int(timer)) }