Beispiel #1
0
func readCoroutine(client *Client) {
	defer Commen.HandlePanic()

	head := make([]byte, PACK_HEAD_LEN)
	var readLen = 0
	var err error

	for {
		if !client.running {
			goto Exit
		}

		if err = (*client.conn).SetReadDeadline(time.Now().Add(NetConf.READ_BLOCK_TIME)); err != nil {
			//Logger.Info(LogConf.NetCoreClient, client.Idx, "SetReadDeadline error: %v", err)
			goto Exit
		}

		lastRead := time.Now().UTC()
		readLen, err = io.ReadFull(client.conn, head)
		if err != nil || readLen < PACK_HEAD_LEN {
			Logger.Info(LogConf.NetCoreClient, client.Idx, "Read head error: %v %v %v", err, lastRead, time.Now().UTC())
			goto Exit
		}

		if err = (*client.conn).SetReadDeadline(time.Now().Add(NetConf.READ_BLOCK_TIME)); err != nil {
			Logger.Info(LogConf.NetCoreClient, client.Idx, "SetReadDeadline error: %v", err)
			goto Exit
		}

		var msg = new(NetMsg)
		//var msg = &NetMsg{}

		msg.BufLen = binary.LittleEndian.Uint32(head[0:4])
		msg.Cmd = binary.LittleEndian.Uint32(head[4:8])
		//msg.action = binary.LittleEndian.Uint16(head[6:8])

		if msg.BufLen > 0 {
			msg.Buf = make([]byte, msg.BufLen)
			readLen, err := io.ReadFull(client.conn, msg.Buf)
			if err != nil || readLen != int(msg.BufLen) {
				Logger.Info(LogConf.NetCoreClient, client.Idx, "Read body error: %v", err)
				goto Exit
			}
		}
		//Logger.Info(LogConf.NetCoreClient, client.Idx, "msg read ---->>> : %d %d %v", msg.Cmd, msg.BufLen, msg.Buf)

		if ENABLE_MSG_COROUTINE {
			client.recvQ <- msg
		} else {
			client.HandleMsg(msg)
		}

	}

Exit:
	client.Stop()
	Logger.Info(LogConf.NetCoreClient, client.Idx, "%d readCoroutine exit.", client.Idx)
	return
}
Beispiel #2
0
func sendCoroutine(client *Client) {
	defer Commen.HandlePanic()

	if ENABLE_MSG_COROUTINE {
		go msgCoroutine(client)
	} else {
		go readCoroutine(client)
	}

	var buf []byte
	for {
		if !client.running {
			return
		}

		//select {
		/*
			case <-client.chStop:
				Logger.Info(LogConf.NetCoreClient, client.Idx, "%d sendCoroutine exit.", client.Idx)
				return
		*/
		//case buf := <-client.sendQ:
		buf = <-client.sendQ
		if buf == nil {
			Logger.Info(LogConf.NetCoreClient, client.Idx, "%d sendCoroutine exit.", client.Idx)
			return
		}
		if err := (*client.conn).SetWriteDeadline(time.Now().Add(NetConf.WRITE_BLOCK_TIME)); err != nil {
			client.Stop()
			return
			//Logger.Info(LogConf.NetCoreClient, client.Idx, "SetWriteDeadline error: %v", err)
		}

		writeLen, err := client.conn.Write(buf)
		//Logger.Info(LogConf.NetCoreClient, client.Idx, "Write--->>, len: %d buf: %v", writeLen, buf)
		if err != nil || writeLen != len(buf) {
			client.Stop()
			return
			Logger.Info(LogConf.NetCoreClient, client.Idx, "Write error, len: %d error: %v", writeLen, buf)
		} else {
			//Logger.Info(LogConf.NetCoreClient, client.Idx, "Write %v", buf)
		}

		//}
	}
}
Beispiel #3
0
func msgCoroutine(client *Client) {
	defer Commen.HandlePanic()
	//defer client.Clear()
	go readCoroutine(client)

	var msg *NetMsg
	for {
		/*
			select {
			case <-client.chStop:
				Logger.Info(LogConf.NetCoreClient, client.Idx, "%d msgCoroutine exit.", client.Idx)
				return
		*/
		//case msg := <-client.recvQ:
		msg = <-client.recvQ

		if msg == nil {
			Logger.Info(LogConf.NetCoreClient, client.Idx, "%d msgCoroutine exit.", client.Idx)
			return
		}

		client.HandleMsg(msg)
	}
}