aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/go_request_reply.go
blob: 1c0ce14603fdcdac6022f8e52f2b55e5ba892037 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main

import (
	"fmt"
	"sort"
	"strings"
)

func (r *Request) Define(c *Context) {
	c.Putln("// %s is a cookie used only for %s requests.",
		r.CookieName(), r.SrcName())
	c.Putln("type %s struct {", r.CookieName())
	c.Putln("*xgb.Cookie")
	c.Putln("}")
	c.Putln("")

	if r.Doc.Description != "" {
		c.PutComment(r.Doc.Description)
		c.Putln("//")
	}

	allErrors := make([]string, 0, len(r.Doc.Errors))
	for kind := range r.Doc.Errors {
		allErrors = append(allErrors, kind)
	}
	sort.Strings(allErrors)

	undocErrors := make([]string, 0)
	for _, kind := range allErrors {
		if desc := r.Doc.Errors[kind]; desc == "" {
			undocErrors = append(undocErrors, kind)
		} else {
			c.PutComment(fmt.Sprintf("May return a %s error if %s%s", kind,
				strings.ToLower(desc[:1]), desc[1:]))
			c.Putln("//")
		}
	}
	if len(undocErrors) > 0 {
		c.Putln("// May return %s errors.", strings.Join(undocErrors, ", "))
		c.Putln("//")
	}

	if r.Reply != nil {
		c.Putln("// %s sends a checked request.", r.SrcName())
		c.Putln("// If an error occurs, it will be returned with the reply "+
			"by calling %s.Reply.", r.CookieName())
		c.Putln("func %s(c *xgb.Conn, %s) %s {",
			r.SrcName(), r.ParamNameTypes(), r.CookieName())
		r.CheckExt(c)
		c.Putln("cookie := c.NewCookie(true, true)")
		c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
		c.Putln("return %s{cookie}", r.CookieName())
		c.Putln("}")
		c.Putln("")

		c.Putln("// %sUnchecked sends an unchecked request.", r.SrcName())
		c.Putln("// If an error occurs, it can only be retrieved using " +
			"xgb.WaitForEvent or xgb.PollForEvent.")
		c.Putln("func %sUnchecked(c *xgb.Conn, %s) %s {",
			r.SrcName(), r.ParamNameTypes(), r.CookieName())
		r.CheckExt(c)
		c.Putln("cookie := c.NewCookie(false, true)")
		c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
		c.Putln("return %s{cookie}", r.CookieName())
		c.Putln("}")
		c.Putln("")

		r.ReadReply(c)
	} else {
		c.Putln("// %s sends an unchecked request.", r.SrcName())
		c.Putln("// If an error occurs, it can only be retrieved using " +
			"xgb.WaitForEvent or xgb.PollForEvent.")
		c.Putln("func %s(c *xgb.Conn, %s) %s {",
			r.SrcName(), r.ParamNameTypes(), r.CookieName())
		r.CheckExt(c)
		c.Putln("cookie := c.NewCookie(false, false)")
		c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
		c.Putln("return %s{cookie}", r.CookieName())
		c.Putln("}")
		c.Putln("")

		c.Putln("// %sChecked sends a checked request.", r.SrcName())
		c.Putln("// If an error occurs, it can be retrieved using "+
			"%s.Check.", r.CookieName())
		c.Putln("func %sChecked(c *xgb.Conn, %s) %s {",
			r.SrcName(), r.ParamNameTypes(), r.CookieName())
		r.CheckExt(c)
		c.Putln("cookie := c.NewCookie(true, false)")
		c.Putln("c.NewRequest(%s(c, %s), cookie)", r.ReqName(), r.ParamNames())
		c.Putln("return %s{cookie}", r.CookieName())
		c.Putln("}")
		c.Putln("")

		c.Putln("// Check returns an error if one occurred for checked " +
			"requests that are not expecting a reply.")
		c.Putln("// This cannot be called for requests expecting a reply, " +
			"nor for unchecked requests.")
		c.Putln("func (cook %s) Check() error {", r.CookieName())
		c.Putln("return cook.Cookie.Check()")
		c.Putln("}")
		c.Putln("")
	}
	r.WriteRequest(c)
}

func (r *Request) CheckExt(c *Context) {
	if !c.protocol.isExt() {
		return
	}
	c.Putln("c.ExtLock.RLock()")
	c.Putln("defer c.ExtLock.RUnlock()")
	c.Putln("if _, ok := c.Extensions[\"%s\"]; !ok {", c.protocol.ExtXName)
	c.Putln("panic(\"Cannot issue request '%s' using the uninitialized "+
		"extension '%s'. %s.Init(connObj) must be called first.\")",
		r.SrcName(), c.protocol.ExtXName, c.protocol.PkgName())
	c.Putln("}")
}

func (r *Request) ReadReply(c *Context) {
	c.Putln("// %s represents the data returned from a %s request.",
		r.ReplyTypeName(), r.SrcName())
	c.Putln("type %s struct {", r.ReplyTypeName())
	c.Putln("Sequence uint16 // sequence number of the request for this reply")
	c.Putln("Length uint32 // number of bytes in this reply")
	for _, field := range r.Reply.Fields {
		field.Define(c)
	}
	c.Putln("}")
	c.Putln("")

	c.Putln("// Reply blocks and returns the reply data for a %s request.",
		r.SrcName())
	c.Putln("func (cook %s) Reply() (*%s, error) {",
		r.CookieName(), r.ReplyTypeName())
	c.Putln("buf, err := cook.Cookie.Reply()")
	c.Putln("if err != nil {")
	c.Putln("return nil, err")
	c.Putln("}")
	c.Putln("if buf == nil {")
	c.Putln("return nil, nil")
	c.Putln("}")
	c.Putln("return %s(buf), nil", r.ReplyName())
	c.Putln("}")
	c.Putln("")

	c.Putln("// %s reads a byte slice into a %s value.",
		r.ReplyName(), r.ReplyTypeName())
	c.Putln("func %s(buf []byte) *%s {",
		r.ReplyName(), r.ReplyTypeName())
	c.Putln("v := new(%s)", r.ReplyTypeName())
	c.Putln("b := 1 // skip reply determinant")
	c.Putln("")
	for i, field := range r.Reply.Fields {
		field.Read(c, "v.")
		c.Putln("")
		if i == 0 {
			c.Putln("v.Sequence = xgb.Get16(buf[b:])")
			c.Putln("b += 2")
			c.Putln("")
			c.Putln("v.Length = xgb.Get32(buf[b:]) // 4-byte units")
			c.Putln("b += 4")
			c.Putln("")
		}
	}
	c.Putln("return v")
	c.Putln("}")
	c.Putln("")
}

func (r *Request) WriteRequest(c *Context) {
	sz := r.Size(c)
	writeSize1 := func() {
		if sz.exact {
			c.Putln("xgb.Put16(buf[b:], uint16(size / 4)) " +
				"// write request size in 4-byte units")
		} else {
			c.Putln("blen := b")
		}
		c.Putln("b += 2")
		c.Putln("")
	}
	writeSize2 := func() {
		if sz.exact {
			c.Putln("return buf")
			return
		}
		c.Putln("b = xgb.Pad(b)")
		c.Putln("xgb.Put16(buf[blen:], uint16(b / 4)) " +
			"// write request size in 4-byte units")
		c.Putln("return buf[:b]")
	}
	c.Putln("// %s writes a %s request to a byte slice for transfer.",
		r.ReqName(), r.SrcName())
	c.Putln("func %s(c *xgb.Conn, %s) []byte {",
		r.ReqName(), r.ParamNameTypes())
	c.Putln("size := %s", sz)
	c.Putln("b := 0")
	c.Putln("buf := make([]byte, size)")
	c.Putln("")
	if c.protocol.isExt() {
		c.Putln("c.ExtLock.RLock()")
		c.Putln("buf[b] = c.Extensions[\"%s\"]", c.protocol.ExtXName)
		c.Putln("c.ExtLock.RUnlock()")
		c.Putln("b += 1")
		c.Putln("")
	}
	c.Putln("buf[b] = %d // request opcode", r.Opcode)
	c.Putln("b += 1")
	c.Putln("")
	if len(r.Fields) == 0 {
		if !c.protocol.isExt() {
			c.Putln("b += 1 // padding")
		}
		writeSize1()
	} else if c.protocol.isExt() {
		writeSize1()
	}
	for i, field := range r.Fields {
		field.Write(c, "")
		c.Putln("")
		if i == 0 && !c.protocol.isExt() {
			writeSize1()
		}
	}
	writeSize2()
	c.Putln("}")
	c.Putln("")
}

func (r *Request) ParamNames() string {
	names := make([]string, 0, len(r.Fields))
	for _, field := range r.Fields {
		switch f := field.(type) {
		case *ValueField:
			names = append(names, f.MaskName)
			names = append(names, f.ListName)
		case *PadField:
			continue
		case *ExprField:
			continue
		default:
			names = append(names, fmt.Sprintf("%s", field.SrcName()))
		}
	}
	return strings.Join(names, ", ")
}

func (r *Request) ParamNameTypes() string {
	nameTypes := make([]string, 0, len(r.Fields))
	for _, field := range r.Fields {
		switch f := field.(type) {
		case *ValueField:
			nameTypes = append(nameTypes,
				fmt.Sprintf("%s %s", f.MaskName, f.MaskType.SrcName()))
			nameTypes = append(nameTypes,
				fmt.Sprintf("%s []uint32", f.ListName))
		case *PadField:
			continue
		case *ExprField:
			continue
		case *RequiredStartAlign:
			continue
		default:
			nameTypes = append(nameTypes,
				fmt.Sprintf("%s %s", field.SrcName(), field.SrcType()))
		}
	}
	return strings.Join(nameTypes, ", ")
}