Beispiel #1
0
func (this *tcpProtocolServerOperation) read(conn net.Conn, data []byte, parse bool, callback func(conn net.Conn, id int, version byte, serverType byte, dataType byte, dataLen int, data []byte, rawData []byte)) {
	this.conn = conn

	timer.CancelInaccuracyTimer(this.timer_id)
	timer.NewInaccuracyTimerCallback(this.disconnected_time, func(id int64) {
		this.timer_id = id
	}, func(time int64) {
		if time != 0 {
			if this.disconnected_handler != nil {
				this.disconnected_handler(conn, this.id)
			}
		}
	})
	if parse {
		result, id, version, serverType, dataType, dataLen, protocolData, raw := this.OpData(conn, data)
		if result {
			//需要处理
			if callback != nil {
				callback(conn, id, version, serverType, dataType, dataLen, protocolData, raw)
			}
		}
	} else {
		result, id, version, serverType, dataType, dataLen, raw := this.OpRawData(conn, data)
		if result {
			//需要处理
			if callback != nil {
				callback(conn, id, version, serverType, dataType, dataLen, nil, raw)
			}
		}
	}
}
func (this *BaseProtocolTcpClient) doPing() {
	timer.CancelInaccuracyTimer(this.ping_timer_id)
	timer.NewInaccuracyTimerCallback(this.ping_interval, func(id int64) {
		this.ping_timer_id = id
	}, func(time int64) {
		if time != 0 {
			log.I_NET(this.client.GetConnection().LocalAddr(), " write to ", this.client.GetConnection().RemoteAddr(), ", data : ping")
			this.Write(protocol.NewPingProtocol(this.entity.ID))
		}
	})
}
Beispiel #3
0
func (this *BaseClient) doPing() {
	timer.CancelInaccuracyTimer(this.ping_timer_id)
	timer.NewInaccuracyTimerCallback(this.ping_interval, func(id int64) {
		this.ping_timer_id = id
	}, func(time int64) {
		if time != 0 {
			log.I_NET(this.conn.LocalAddr(), " write to ", this.conn.RemoteAddr(), ", data : ping")
			this.Write(protocol.NewPingProtocol(this.id), nil)
			this.doPing()
		}
	})
}
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
}