Beispiel #1
0
func ProcessConnection(conn *net.TCPConn) {
	defer logger.CatchException()

	SocketBase := network.NewSocketBase(conn)
	defer SocketBase.Close()

	SocketBase.SetNoDelay(true)                                        //无延迟
	SocketBase.SetKeepAlive(true)                                      //保持激活
	SocketBase.SetReadBuffer(64 * 1024)                                //设置读缓冲区大小
	SocketBase.SetWriteBuffer(64 * 1024)                               //设置写缓冲区大小
	SocketBase.SetReadDeadline(time.Now().Add(30000000 * time.Second)) //设置读超时

	for {
		msgs, err := SocketBase.RecvMsgs()
		if err != nil {
			logger.Errorf("SocketBase.RecvMsgs failed! err=%v", err)
			return
		}

		if len(msgs) == 0 {
			continue
		}

		for _, msg := range msgs {
			ret := network.Dispatcher(SocketBase, msg)
			if ret == ID.MESSAGE_OK {
				return
			}
		}
	}
}
Beispiel #2
0
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
}