コード例 #1
0
func writePacketGeneric(buf []byte, packet *pokerth.PokerTHMessage, headerSize uint32) uint32 {
	var bytesWritten uint32 = 0
	num, err := packet.MarshalTo(buf[headerSize:])
	if err == nil {
		if headerSize > 0 {
			binary.BigEndian.PutUint32(buf[0:headerSize], uint32(num))
			bytesWritten = uint32(num) + headerSize
		} else {
			bytesWritten = uint32(num)
		}
	}
	return bytesWritten
}
コード例 #2
0
ファイル: session.go プロジェクト: liuxing9848/pokerth
func (s *Session) handleSend() {
	var buf [RECV_BUF_SIZE]byte
	var packet *pokerth.PokerTHMessage
	for {
		select {
		case packet = <-s.sender:
			packetSize := s.WritePacket(buf[0:RECV_BUF_SIZE], packet)
			if packetSize > 0 {
				var bufStart uint32 = 0
				for bufStart < packetSize {
					num, err := s.Connection.Write(buf[bufStart:packetSize])
					if err != nil {
						log.Printf("Write error:  %s\n", err.Error())
						return
					}
					bufStart += uint32(num)
				}
				log.Printf("Packet out: %d, size: %d\n", packet.GetMessageType(), packetSize)
			}
		}
	}
}