// TODO: Documentation func (c *Client) Read() (*packet.Packet, error) { var buf [MaxPacketSize]byte parsed := new(packet.Packet) for { n, err := c.conn.Read(buf[:]) if err != nil { return nil, err } if err = proto.Unmarshal(buf[:n], parsed); err != nil { return nil, err } if an := parsed.GetArrivalNotice().GetPacketID(); an != 0 { c.lock.Lock() delete(c.ensured, an) c.lock.Unlock() continue } if ensure := parsed.GetEnsureArrival(); ensure != 0 { c.Write(&packet.Packet{ ArrivalNotice: &packet.Packet_ArrivalNotice{ PacketID: proto.Uint64(ensure), }, }, false) } return parsed, nil } panic("unreachable") }
// TODO: Documentation func (sc *ServerClient) Send(p *packet.Packet, ensure bool) { if ensure { p.EnsureArrival = sc.server.ensureID() } else { p.EnsureArrival = nil } b, err := proto.Marshal(p) if err != nil { log.Println("Error encoding packet to", sc.addr, "-", err) } else { sc.server.Send(sc.addr, b, p.GetEnsureArrival()) } }
// TODO: Documentation func (c *Client) Write(p *packet.Packet, ensure bool) error { if ensure { c.lock.Lock() c.lastEnsure++ p.EnsureArrival = proto.Uint64(c.lastEnsure) c.lock.Unlock() } else { p.EnsureArrival = nil } b, err := proto.Marshal(p) if err != nil { return err } if ensure { c.lock.Lock() c.ensured[p.GetEnsureArrival()] = &clientEnsure{b, time.Now(), MaxRetries} c.lock.Unlock() } _, err = c.conn.Write(b) return err }