diff options
author | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-02 01:46:30 -0400 |
---|---|---|
committer | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-02 01:46:30 -0400 |
commit | 39507f86ab0468292c24081a80f41c1799d6b477 (patch) | |
tree | 002668a66ec90c71b3bbd05141b378d0b2218d58 /nexgb/xgbgen/representation.go | |
parent | f48b6fafc6d96527a554876e13fc5a0b548005af (diff) | |
download | haven-39507f86ab0468292c24081a80f41c1799d6b477.tar.gz haven-39507f86ab0468292c24081a80f41c1799d6b477.tar.xz haven-39507f86ab0468292c24081a80f41c1799d6b477.zip |
finally starting on the crescendo: requests and replies.
Diffstat (limited to 'nexgb/xgbgen/representation.go')
-rw-r--r-- | nexgb/xgbgen/representation.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/nexgb/xgbgen/representation.go b/nexgb/xgbgen/representation.go index 2d33a45..e5d2202 100644 --- a/nexgb/xgbgen/representation.go +++ b/nexgb/xgbgen/representation.go @@ -1,5 +1,10 @@ package main +import ( + "fmt" + "log" +) + type Protocol struct { Name string ExtXName string @@ -44,10 +49,58 @@ func (r *Request) Initialize(p *Protocol) { } } +func (r *Request) SrcName() string { + return r.srcName +} + +func (r *Request) XmlName() string { + return r.xmlName +} + +func (r *Request) ReplyName() string { + if r.Reply == nil { + log.Panicf("Cannot call 'ReplyName' on request %s, which has no reply.", + r.SrcName()) + } + return fmt.Sprintf("%sReply", r.SrcName()) +} + +// 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) + + if c.protocol.Name == "xproto" { + size = size.Add(newFixedSize(3)) + } else { + size = size.Add(newFixedSize(4)) + } + + for _, field := range r.Fields { + size = size.Add(field.Size()) + } + return size +} + type Reply struct { Fields []Field } +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 +} + func (r *Reply) Initialize(p *Protocol) { for _, field := range r.Fields { field.Initialize(p) |