3 * Wire Level Encapsulation
22 TypeIdentifyClient = 1
26 TypeAcknowledgement = 5
30 ErrMalformedMessage = os.NewError("Malformed Message")
31 ErrUnknownMessage = os.NewError("Unknown Message")
34 func (p *WirePkt) ValidUnidentified() bool {
35 if p.Type == TypeNop {
38 if p.Type == TypeIdentifyClient {
45 func (p *WirePkt) Send(c net.Conn) (n int, err os.Error) {
47 preamble := make([]byte, 3)
49 preamble[1] = byte((p.Length >> 8) & 0xFF)
50 preamble[2] = byte(p.Length & 0xFF)
51 ninc, err := c.Write(preamble)
56 ninc, err = c.Write(p.Payload[0:p.Length])
64 func (p *WirePkt) Dump() {
65 fmt.Printf("Packet Dump: Type %d, Len %d\n", p.Type, p.Length)
66 for i := 0; i < int(p.Length); i++ {
68 fmt.Printf("%04x: ", i)
70 fmt.Printf("%02x ", p.Payload[i])
78 func Receive(c net.Conn) (msg *WirePkt, err os.Error) {
80 preamble := make([]byte, 3)
82 n, err := c.Read(preamble)
87 /* short read! wtf! err? */
88 return nil, ErrMalformedMessage
90 msg.Type = preamble[0]
91 msg.Length = (uint16(preamble[1]) << 8) | uint16(preamble[2])
93 msg.Payload = make([]byte, msg.Length)
94 n, err = c.Read(msg.Payload)
98 if n < int(msg.Length) {
99 /* short read! wtf! err? */
100 return nil, ErrMalformedMessage