Exemplo n.º 1
0
func (this *BaseTcpOperation) OpRawData(conn net.Conn, data []byte) (result bool, id int, version byte, serverType byte, dataType byte, dataLen int, rawData []byte) {
	this.copyBuf(data)

	for true {

		exist, id, version, serverType, dataType, oLen, rawData := protocol.ParseRawProtocol(this.buf, this.buf_len)
		if exist {
			if protocol.IsPingByType(dataType) {
				log.I_NET(conn.LocalAddr(), "write to client", conn.RemoteAddr(), "pong")
				conn.Write(protocol.NewPongProtocol(id))
				this.FinishOneProtocol()
			} else if protocol.IsPongByType(dataType) {
				log.I_NET("client", conn.LocalAddr(), "read from server", conn.RemoteAddr(), " pong")
				this.FinishOneProtocol()
			} else {

				d := make([]byte, oLen)
				copy(d, rawData)

				this.FinishOneProtocol()
				return true, id, version, serverType, dataType, oLen, d
			}
		} else {
			return false, 0, 0, 0, 0, 0, nil
		}
	}
	return false, 0, 0, 0, 0, 0, nil
}
func (this *BaseProtocolTcpServerOperation) OpReadData(data []byte) bool {

	this.copyBuf(data)

	ret := true
	for true {
		exist, id, _, _, dataType, _, _, flen, _ := protocol.ParseProtocol(this.buf, this.buf_len)
		if exist {
			this.id = id
			if protocol.IsPingByType(dataType) {
				log.I_NET(this.conn.LocalAddr(), "write to client", this.conn.RemoteAddr(), "pong")
				this.conn.Write(protocol.NewPongProtocol(id))
				this.FinishOneProtocol()
				ret = true
			} else {
				ret = false
				this.protocol_buf = this.buf[:(flen + protocol.PROTOCOL_HEADER_LEN)]
				break
			}
		} else {
			this.protocol_buf = nil
			break
		}
	}

	timer.CancelInaccuracyTimer(this.timer_id)
	timer.NewInaccuracyTimerCallback(this.time_for_disconnected, func(id int64) {
		this.timer_id = id
	}, func(time int64) {
		if time != 0 {
			this.OpDisconnected()
		}
	})

	return ret
}