2012-04-30 08:40:55 +02:00
|
|
|
package main
|
|
|
|
|
2012-05-02 07:46:30 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2012-05-05 08:55:38 +02:00
|
|
|
"unicode"
|
2012-05-02 07:46:30 +02:00
|
|
|
)
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Request represents all XML 'request' nodes.
|
|
|
|
// If the request doesn't have a reply, Reply is nil.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Request struct {
|
2012-05-06 08:21:31 +02:00
|
|
|
srcName string // The Go name of this request.
|
|
|
|
xmlName string // The XML name of this request.
|
2012-04-30 08:44:31 +02:00
|
|
|
Opcode int
|
2012-05-07 10:09:19 +02:00
|
|
|
Combine bool // Not currently used.
|
2012-05-06 08:21:31 +02:00
|
|
|
Fields []Field // All fields in the request.
|
2012-05-07 10:09:19 +02:00
|
|
|
Reply *Reply // A reply, if one exists for this request.
|
2012-04-30 08:40:55 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Initialize creates the proper Go source name for this request.
|
|
|
|
// It also initializes the reply if one exists, and all fields in this request.
|
2012-04-30 08:40:55 +02:00
|
|
|
func (r *Request) Initialize(p *Protocol) {
|
2012-05-06 00:21:48 +02:00
|
|
|
r.srcName = SrcName(p, r.xmlName)
|
2012-05-06 23:48:40 +02:00
|
|
|
if p.isExt() {
|
2012-05-10 23:01:42 +02:00
|
|
|
r.srcName = r.srcName
|
2012-05-06 00:21:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
if r.Reply != nil {
|
|
|
|
r.Reply.Initialize(p)
|
|
|
|
}
|
|
|
|
for _, field := range r.Fields {
|
|
|
|
field.Initialize(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:46:30 +02:00
|
|
|
func (r *Request) SrcName() string {
|
|
|
|
return r.srcName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) XmlName() string {
|
|
|
|
return r.xmlName
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// ReplyName gets the Go source name of the function that generates a
|
|
|
|
// reply type from a slice of bytes.
|
|
|
|
// The generated function is not currently exported.
|
2012-05-02 07:46:30 +02:00
|
|
|
func (r *Request) ReplyName() string {
|
2012-05-05 08:55:38 +02:00
|
|
|
if r.Reply == nil {
|
|
|
|
log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.",
|
|
|
|
r.SrcName())
|
|
|
|
}
|
|
|
|
name := r.SrcName()
|
|
|
|
lower := string(unicode.ToLower(rune(name[0]))) + name[1:]
|
|
|
|
return fmt.Sprintf("%sReply", lower)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// ReplyTypeName gets the Go source name of the type holding all reply data
|
|
|
|
// for this request.
|
2012-05-05 08:55:38 +02:00
|
|
|
func (r *Request) ReplyTypeName() string {
|
2012-05-02 07:46:30 +02:00
|
|
|
if r.Reply == nil {
|
|
|
|
log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.",
|
|
|
|
r.SrcName())
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%sReply", r.SrcName())
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// ReqName gets the Go source name of the function that generates a byte
|
|
|
|
// slice from a list of parameters.
|
|
|
|
// The generated function is not currently exported.
|
2012-05-03 07:00:01 +02:00
|
|
|
func (r *Request) ReqName() string {
|
2012-05-05 08:55:38 +02:00
|
|
|
name := r.SrcName()
|
|
|
|
lower := string(unicode.ToLower(rune(name[0]))) + name[1:]
|
|
|
|
return fmt.Sprintf("%sRequest", lower)
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// CookieName gets the Go source name of the type that holds cookies for
|
|
|
|
// this request.
|
2012-05-05 08:55:38 +02:00
|
|
|
func (r *Request) CookieName() string {
|
|
|
|
return fmt.Sprintf("%sCookie", r.SrcName())
|
2012-05-03 07:00:01 +02:00
|
|
|
}
|
|
|
|
|
2012-05-02 07:46:30 +02:00
|
|
|
// Size for Request needs a context.
|
|
|
|
// Namely, if this is an extension, we need to account for *four* bytes
|
|
|
|
// of a header (extension opcode, request opcode, and the sequence number).
|
|
|
|
// If it's a core protocol request, then we only account for *three*
|
|
|
|
// bytes of the header (remove the extension opcode).
|
|
|
|
func (r *Request) Size(c *Context) Size {
|
|
|
|
size := newFixedSize(0)
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// If this is a core protocol request, we squeeze in an extra byte of
|
2013-01-26 18:51:48 +01:00
|
|
|
// data (from the fields below) between the opcode and the size of the
|
2012-05-06 08:21:31 +02:00
|
|
|
// request. In an extension request, this byte is always occupied
|
|
|
|
// by the opcode of the request (while the first byte is always occupied
|
|
|
|
// by the opcode of the extension).
|
2012-05-06 23:48:40 +02:00
|
|
|
if !c.protocol.isExt() {
|
2012-05-02 07:46:30 +02:00
|
|
|
size = size.Add(newFixedSize(3))
|
|
|
|
} else {
|
|
|
|
size = size.Add(newFixedSize(4))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, field := range r.Fields {
|
2012-05-03 07:00:01 +02:00
|
|
|
switch field.(type) {
|
2012-05-06 08:21:31 +02:00
|
|
|
case *LocalField: // local fields don't go over the wire
|
2012-05-03 07:00:01 +02:00
|
|
|
continue
|
|
|
|
case *SingleField:
|
|
|
|
// mofos!!!
|
|
|
|
if r.SrcName() == "ConfigureWindow" &&
|
|
|
|
field.SrcName() == "ValueMask" {
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
size = size.Add(field.Size())
|
|
|
|
default:
|
|
|
|
size = size.Add(field.Size())
|
|
|
|
}
|
2012-05-02 07:46:30 +02:00
|
|
|
}
|
2012-05-05 08:55:38 +02:00
|
|
|
return newExpressionSize(&Padding{
|
|
|
|
Expr: size.Expression,
|
|
|
|
})
|
2012-05-02 07:46:30 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Reply encapsulates the fields associated with a 'reply' element.
|
2012-04-30 08:40:55 +02:00
|
|
|
type Reply struct {
|
|
|
|
Fields []Field
|
|
|
|
}
|
|
|
|
|
2012-05-06 08:21:31 +02:00
|
|
|
// Size gets the number of bytes in this request's reply.
|
|
|
|
// A reply always has at least 7 bytes:
|
|
|
|
// 1 byte: A reply discriminant (first byte set to 1)
|
|
|
|
// 2 bytes: A sequence number
|
|
|
|
// 4 bytes: Number of additional bytes in 4-byte units past initial 32 bytes.
|
2012-05-02 07:46:30 +02:00
|
|
|
func (r *Reply) Size() Size {
|
|
|
|
size := newFixedSize(0)
|
|
|
|
|
|
|
|
// Account for reply discriminant, sequence number and reply length
|
|
|
|
size = size.Add(newFixedSize(7))
|
|
|
|
|
|
|
|
for _, field := range r.Fields {
|
|
|
|
size = size.Add(field.Size())
|
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2012-04-30 08:40:55 +02:00
|
|
|
func (r *Reply) Initialize(p *Protocol) {
|
|
|
|
for _, field := range r.Fields {
|
|
|
|
field.Initialize(p)
|
|
|
|
}
|
|
|
|
}
|