public override void OnReceive(Gamnet.Buffer buf) { recvBuffer += buf; while (recvBuffer.Size() >= PACKET_HEADER_SIZE) { ushort packetLength = BitConverter.ToUInt16(recvBuffer.buffer, recvBuffer.readIndex + PACKET_SIZE_OFFSET); if (packetLength > Gamnet.Buffer.BUFFER_SIZE) { throw new Exception(string.Format("The packet length is greater than the buffer max length.")); } if (packetLength > recvBuffer.Size()) { return; } Gamnet.Buffer tmpBuffer = new Gamnet.Buffer(recvBuffer); int msgID = BitConverter.ToInt32(tmpBuffer.buffer, tmpBuffer.readIndex + MSGID_OFFSET); if (false == _dictHandler.ContainsKey(msgID)) { UnityEngine.Debug.Log("msg_id:" + msgID + " is not registered"); ErrorEvent evt = new ErrorEvent(); evt.session = this; evt.error = new System.Collections.Generic.KeyNotFoundException(); eventQueue.Enqueue(evt); return; } HandlerContainer container = _dictHandler[msgID]; if (null != container.timer) { container.timer.Stop(); container.timer.Close(); } tmpBuffer.readIndex += PACKET_HEADER_SIZE; try { container.handler(tmpBuffer); } catch (System.Exception) {} recvBuffer.readIndex += packetLength; recvBuffer = new Gamnet.Buffer(recvBuffer); } }
public void OnReceive(Gamnet.Buffer buf) { _recv_buff += buf; while (_recv_buff.Size() >= Packet.HEADER_SIZE) { ushort packetLength = BitConverter.ToUInt16(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_LENGTH); if (packetLength > Gamnet.Buffer.BUFFER_SIZE) { Error(new Gamnet.Exception(ErrorCode.BufferOverflowError, "The packet length is greater than the buffer max length.")); return; } if (packetLength > _recv_buff.Size()) // not enough { return; } uint msgID = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGID); if (false == _handlers.ContainsKey(msgID)) { Error(new Gamnet.Exception(ErrorCode.UnhandledMsgError, "can't find registered msg(id:" + msgID + ")")); return; } _recv_buff.read_index += Packet.HEADER_SIZE; _timeout_monitor.UnsetTimeout(msgID); Delegate_OnReceive handler = _handlers[msgID]; try { handler(_recv_buff); } catch (System.Exception e) { Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.Message)); } _recv_buff.read_index += packetLength - Packet.HEADER_SIZE; } }
public override void OnReceive(Gamnet.Buffer buf) { recvBuffer += buf; while (recvBuffer.Size() >= PACKET_HEADER_SIZE) { ushort packetLength = BitConverter.ToUInt16(recvBuffer.data, recvBuffer.readIndex + PACKET_SIZE_OFFSET); if (packetLength > Gamnet.Buffer.BUFFER_SIZE) { throw new Exception(string.Format("The packet length is greater than the buffer max length.")); } if (packetLength > recvBuffer.Size()) { // not enough return; } int msgID = BitConverter.ToInt32(recvBuffer.data, recvBuffer.readIndex + MSGID_OFFSET); if (false == handlers.ContainsKey(msgID)) { ErrorEvent evt = new ErrorEvent(this); evt.exception = new System.Exception("can't find registered msg(id:" + msgID + ")"); eventQueue.Enqueue(evt); return; } recvBuffer.readIndex += PACKET_HEADER_SIZE; Action <Gamnet.Buffer> handler = handlers[msgID]; try { timeoutMonitor.UnsetTimeout(msgID); handler(recvBuffer); } catch (System.Exception) { } } }
private void OnReceive(Gamnet.Buffer buf) { _recv_buff += buf; while (_recv_buff.Size() >= Packet.HEADER_SIZE) { ushort packetLength = BitConverter.ToUInt16(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_LENGTH); uint msgID = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGID); uint msgSEQ = BitConverter.ToUInt32(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_MSGSEQ); bool reliable = BitConverter.ToBoolean(_recv_buff.data, _recv_buff.read_index + Packet.OFFSET_RELIABLE); if (packetLength > Gamnet.Buffer.BUFFER_SIZE) { Debug.LogError("[Session.OnReceive] The packet length is greater than the buffer max length."); Error(new Gamnet.Exception(ErrorCode.UndefinedError, "The packet length is greater than the buffer max length.")); return; } if (packetLength > _recv_buff.Size()) { // not enough return; } _recv_buff.read_index += Packet.HEADER_SIZE; _timeout_monitor.UnsetTimeout(msgID); try { if (false == _handlers.ContainsKey(msgID)) { Debug.LogWarning("[Session.OnReceive] can't find registered msg(id:" + msgID + ")"); throw new Gamnet.Exception(ErrorCode.UnhandledMsgError, "can't find registered msg(id:" + msgID + ")"); } if (false == reliable || _recv_seq < msgSEQ) { System.IO.MemoryStream ms = new System.IO.MemoryStream(); ms.Write(_recv_buff.data, _recv_buff.read_index, packetLength - Packet.HEADER_SIZE); ms.Seek(0, System.IO.SeekOrigin.Begin); IMsgHandler handler = _handlers[msgID]; handler.OnRecvMsg(ms); _recv_seq = Math.Max(_recv_seq, msgSEQ); if (true == reliable) { Send_ReliableAck_Ntf(_recv_seq); } } } catch (Gamnet.Exception e) { Debug.LogError("[Session.OnReceive] " + e.ToString()); Error(e); } catch (System.Exception e) { Debug.LogError("[Session.OnReceive] " + e.ToString()); Error(new Gamnet.Exception(ErrorCode.UndefinedError, e.ToString())); } _recv_buff.read_index += packetLength - Packet.HEADER_SIZE; } }