func (this *C2M_Req_ShakeHand) UnPack(msg *network.Message) error { CommonPackage, pos, err := msg.ParseCommonPackage() if err != nil { return fmt.Errorf("ParseCommonPackage failed! Err=[%s]", err) } this.CommonPackage = CommonPackage GreetingLen := common.ReadUint16(msg.Data[pos : pos+2]) if GreetingLen == 0 || GreetingLen > common.MAX_GREETING_LEN { return fmt.Errorf("UnPack failed! GreetingLen==[0, %v]", common.MAX_GREETING_LEN) } pos += 2 this.Greeting = common.ReadString(msg.Data[pos : pos+int(GreetingLen)]) pos += common.MAX_GREETING_LEN return nil }
func main() { //==================================================================== //---------------------------- Package Info -------------------------- //==================================================================== size := uint32(common.MAX_HEADER_LEN + common.COMMON_PACKAGE_LEN + 2 + len(greeting)) header := new(network.Header) header.PacketID = 100 header.PackageLen = size - 8 //==================================================================== //---------------------------- Pack Message--------------------------- //==================================================================== message := network.NewMessage(100, size-common.MAX_HEADER_LEN) message.Data = make([]byte, size) //CommonPackage message.Account = account message.Signature = signature message.Token = token //Pack Message pos := message.PackHeader() pos = message.PackCommonPackage(pos) //Pach body leng := len(greeting) common.WriteUint16(message.Data[pos:pos+2], uint16(leng)) pos += 2 common.WriteString(message.Data[pos:pos+leng], greeting) pos += common.MAX_GREETING_LEN //==================================================================== //---------------------------- New Session --------------------------- //==================================================================== tcpaddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:4001") if err != nil { logger.Errorf("[client] net.ResolveTCPAddr failed! err=%v", err) } for { conn, err := net.DialTCP("tcp", nil, tcpaddr) if err != nil { logger.Errorf("[client] net.DiaTCP failed! err=%v", err) } defer func() { conn.Close() logger.Errorf("[client] ok...!") }() //==================================================================== //---------------------------- Send ---------------------------------- //==================================================================== TCPConn := network.NewSocketBase(conn) leng, err = TCPConn.Write(message.Data) if err != nil { logger.Errorf("[client] conn.Write failed! err=%v", err) } //==================================================================== //---------------------------- Recv ---------------------------------- //==================================================================== //Parse Message msgs, err := TCPConn.RecvMsgs() if err != nil { logger.Errorf("err=%v", err) return } //handler messages for _, msg := range msgs { result := common.ReadUint16(msg.Data[0:2]) logger.Errorf("result=%v\n", result) } time.Sleep(1 * time.Second) } return }