protected void HandleReader(NetworkReader reader, int receivedSize, int channelId) { while ((ulong)reader.Position < (ulong)((long)receivedSize)) { ushort num = reader.ReadUInt16(); short num2 = reader.ReadInt16(); byte[] array = reader.ReadBytes((int)num); NetworkReader reader2 = new NetworkReader(array); if (this.logNetworkMessages) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < (int)num; i++) { stringBuilder.AppendFormat("{0:X2}", array[i]); if (i > 150) { break; } } Debug.Log(string.Concat(new object[] { "ConnectionRecv con:", this.connectionId, " bytes:", num, " msgId:", num2, " ", stringBuilder })); } NetworkMessageDelegate networkMessageDelegate = null; if (this.m_MessageHandlersDict.ContainsKey(num2)) { networkMessageDelegate = this.m_MessageHandlersDict[num2]; } if (networkMessageDelegate == null) { if (LogFilter.logError) { Debug.LogError(string.Concat(new object[] { "Unknown message ID ", num2, " connId:", this.connectionId })); } break; } this.m_NetMsg.msgType = num2; this.m_NetMsg.reader = reader2; this.m_NetMsg.conn = this; this.m_NetMsg.channelId = channelId; networkMessageDelegate(this.m_NetMsg); this.lastMessageTime = Time.time; } }
// unpack message after receiving public static bool UnpackMessage(byte[] message, out ushort msgType, out byte[] content) { NetworkReader reader = new NetworkReader(message); // read message type (varint) msgType = (UInt16)reader.ReadPackedUInt32(); // read content (remaining data in message) content = reader.ReadBytes(reader.Length - reader.Position); return(true); }
protected void HandleReader(NetworkReader reader, int receivedSize, int channelId) { short num2; while (true) { if (reader.Position >= receivedSize) { return; } ushort num = reader.ReadUInt16(); num2 = reader.ReadInt16(); byte[] array = reader.ReadBytes(num); NetworkReader reader2 = new NetworkReader(array); if (logNetworkMessages) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < num; i++) { stringBuilder.AppendFormat("{0:X2}", array[i]); if (i > 150) { break; } } Debug.Log("ConnectionRecv con:" + connectionId + " bytes:" + num + " msgId:" + num2 + " " + stringBuilder); } NetworkMessageDelegate networkMessageDelegate = null; if (m_MessageHandlersDict.ContainsKey(num2)) { networkMessageDelegate = m_MessageHandlersDict[num2]; } if (networkMessageDelegate == null) { break; } m_NetMsg.msgType = num2; m_NetMsg.reader = reader2; m_NetMsg.conn = this; m_NetMsg.channelId = channelId; networkMessageDelegate(m_NetMsg); lastMessageTime = Time.time; } if (LogFilter.logError) { Debug.LogError("Unknown message ID " + num2 + " connId:" + connectionId); } }
protected void HandleReader( NetworkReader reader, int receivedSize, int channelId) { // read until size is reached. // NOTE: stream.Capacity is 1300, NOT the size of the available data while (reader.Position < receivedSize) { // the reader passed to user code has a copy of bytes from the real stream. user code never touches the real stream. // this ensures it can never get out of sync if user code reads less or more than the real amount. ushort sz = reader.ReadUInt16(); short msgType = reader.ReadInt16(); // create a reader just for this message byte[] msgBuffer = reader.ReadBytes(sz); NetworkReader msgReader = new NetworkReader(msgBuffer); if (logNetworkMessages) { StringBuilder msg = new StringBuilder(); for (int i = 0; i < sz; i++) { msg.AppendFormat("{0:X2}", msgBuffer[i]); if (i > k_MaxMessageLogSize) { break; } } Debug.Log("ConnectionRecv con:" + connectionId + " bytes:" + sz + " msgId:" + msgType + " " + msg); } NetworkMessageDelegate msgDelegate = null; if (m_MessageHandlersDict.ContainsKey(msgType)) { msgDelegate = m_MessageHandlersDict[msgType]; } if (msgDelegate != null) { // create message here instead of caching it. so we can add it to queue more easily. NetworkMessage msg = new NetworkMessage(); msg.msgType = msgType; msg.reader = msgReader; msg.conn = this; msg.channelId = channelId; // add to queue while paused, otherwise process directly if (pauseQueue != null) { pauseQueue.Enqueue(msg); if (LogFilter.logWarn) { Debug.LogWarning("HandleReader: added message to pause queue: " + msgType + " str=" + MsgType.MsgTypeToString(msgType) + " queue size=" + pauseQueue.Count); } } else { msgDelegate(msg); } lastMessageTime = Time.time; #if UNITY_EDITOR UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, MsgType.HLAPIMsg, "msg", 1); if (msgType > MsgType.Highest) { UnityEditor.NetworkDetailStats.IncrementStat( UnityEditor.NetworkDetailStats.NetworkDirection.Incoming, MsgType.UserMessage, msgType.ToString() + ":" + msgType.GetType().Name, 1); } #endif #if UNITY_EDITOR if (m_PacketStats.ContainsKey(msgType)) { PacketStat stat = m_PacketStats[msgType]; stat.count += 1; stat.bytes += sz; } else { PacketStat stat = new PacketStat(); stat.msgType = msgType; stat.count += 1; stat.bytes += sz; m_PacketStats[msgType] = stat; } #endif } else { //NOTE: this throws away the rest of the buffer. Need moar error codes if (LogFilter.logError) { Debug.LogError("Unknown message ID " + msgType + " connId:" + connectionId); } break; } } }
/// <summary> /// This makes the connection process the data contained in the stream, and call handler functions. /// <para>The data in the stream is assumed to have come from the network, and contains network messages.</para> /// <para>This function is used by network connections when they receive data.</para> /// </summary> /// <param name="reader">Stream that contains data.</param> /// <param name="receivedSize">Size of the data.</param> /// <param name="channelId">Channel the data was received on.</param> protected void HandleReader( NetworkReader reader, int receivedSize, int channelId) { // read until size is reached. // NOTE: stream.Capacity is 1300, NOT the size of the available data while (reader.Position < receivedSize) { // the reader passed to user code has a copy of bytes from the real stream. user code never touches the real stream. // this ensures it can never get out of sync if user code reads less or more than the real amount. ushort sz = reader.ReadUInt16(); short msgType = reader.ReadInt16(); // Bail out here if we're about to read beyond the recieved size of the buffer // This could be a sign of an attack which could cause us to behave unexpectedly by reading old data if (reader.Position + sz > receivedSize) { if (LogFilter.logError) { Debug.LogError("Declared packet size larger than recieved size, possible corruption or attack"); } break; } // create a reader just for this message //TODO: Allocation!! byte[] msgBuffer = reader.ReadBytes(sz); NetworkReader msgReader = new NetworkReader(msgBuffer); if (logNetworkMessages) { StringBuilder msg = new StringBuilder(); for (int i = 0; i < sz; i++) { msg.AppendFormat("{0:X2}", msgBuffer[i]); if (i > k_MaxMessageLogSize) { break; } } Debug.Log("ConnectionRecv con:" + connectionId + " bytes:" + sz + " msgId:" + msgType + " " + msg); } NetworkMessageDelegate msgDelegate = null; if (m_MessageHandlersDict.ContainsKey(msgType)) { msgDelegate = m_MessageHandlersDict[msgType]; } if (msgDelegate != null) { m_NetMsg.msgType = msgType; m_NetMsg.reader = msgReader; m_NetMsg.conn = this; m_NetMsg.channelId = channelId; msgDelegate(m_NetMsg); lastMessageTime = Time.time; #if UNITY_EDITOR if (m_PacketStats.ContainsKey(msgType)) { PacketStat stat = m_PacketStats[msgType]; stat.count += 1; stat.bytes += sz; } else { PacketStat stat = new PacketStat(); stat.msgType = msgType; stat.count += 1; stat.bytes += sz; m_PacketStats[msgType] = stat; } #endif } else { //NOTE: this throws away the rest of the buffer. Need moar error codes if (LogFilter.logError) { Debug.LogError("Unknown message ID " + msgType + " connId:" + connectionId); } break; } } }
/// <summary> /// <para>This makes the connection process the data contained in the stream, and call handler functions.</para> /// </summary> /// <param name="reader">Stream that contains data.</param> /// <param name="receivedSize">Size of the data.</param> /// <param name="channelId">Channel the data was received on.</param> protected void HandleReader(NetworkReader reader, int receivedSize, int channelId) { while (reader.Position < receivedSize) { ushort count = reader.ReadUInt16(); short key = reader.ReadInt16(); byte[] buffer = reader.ReadBytes(count); NetworkReader reader2 = new NetworkReader(buffer); if (this.logNetworkMessages) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < count; i++) { builder.AppendFormat("{0:X2}", buffer[i]); if (i > 150) { break; } } Debug.Log(string.Concat(new object[] { "ConnectionRecv con:", this.connectionId, " bytes:", count, " msgId:", key, " ", builder })); } NetworkMessageDelegate delegate2 = null; if (this.m_MessageHandlersDict.ContainsKey(key)) { delegate2 = this.m_MessageHandlersDict[key]; } if (delegate2 != null) { this.m_NetMsg.msgType = key; this.m_NetMsg.reader = reader2; this.m_NetMsg.conn = this; this.m_NetMsg.channelId = channelId; delegate2(this.m_NetMsg); this.lastMessageTime = Time.time; NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Incoming, 0x1c, "msg", 1); if (key > 0x2f) { NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Incoming, 0, key.ToString() + ":" + key.GetType().Name, 1); } if (this.m_PacketStats.ContainsKey(key)) { PacketStat stat = this.m_PacketStats[key]; stat.count++; stat.bytes += count; } else { PacketStat stat2 = new PacketStat { msgType = key }; stat2.count++; stat2.bytes += count; this.m_PacketStats[key] = stat2; } } else { if (LogFilter.logError) { Debug.LogError(string.Concat(new object[] { "Unknown message ID ", key, " connId:", this.connectionId })); } break; } } }
/// <summary> /// <para>This makes the connection process the data contained in the stream, and call handler functions.</para> /// </summary> /// <param name="reader">Stream that contains data.</param> /// <param name="receivedSize">Size of the data.</param> /// <param name="channelId">Channel the data was received on.</param> protected void HandleReader(NetworkReader reader, int receivedSize, int channelId) { while ((long)reader.Position < (long)receivedSize) { ushort num = reader.ReadUInt16(); short key = reader.ReadInt16(); byte[] buffer = reader.ReadBytes((int)num); NetworkReader networkReader = new NetworkReader(buffer); if (this.logNetworkMessages) { StringBuilder stringBuilder = new StringBuilder(); for (int index = 0; index < (int)num; ++index) { stringBuilder.AppendFormat("{0:X2}", (object)buffer[index]); if (index > 150) { break; } } Debug.Log((object)("ConnectionRecv con:" + (object)this.connectionId + " bytes:" + (object)num + " msgId:" + (object)key + " " + (object)stringBuilder)); } NetworkMessageDelegate networkMessageDelegate = (NetworkMessageDelegate)null; if (this.m_MessageHandlersDict.ContainsKey(key)) { networkMessageDelegate = this.m_MessageHandlersDict[key]; } if (networkMessageDelegate != null) { this.m_NetMsg.msgType = key; this.m_NetMsg.reader = networkReader; this.m_NetMsg.conn = this; this.m_NetMsg.channelId = channelId; networkMessageDelegate(this.m_NetMsg); this.lastMessageTime = Time.time; NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Incoming, (short)28, "msg", 1); if ((int)key > 47) { NetworkDetailStats.IncrementStat(NetworkDetailStats.NetworkDirection.Incoming, (short)0, key.ToString() + ":" + key.GetType().Name, 1); } if (this.m_PacketStats.ContainsKey(key)) { NetworkConnection.PacketStat packetStat = this.m_PacketStats[key]; ++packetStat.count; packetStat.bytes += (int)num; } else { NetworkConnection.PacketStat packetStat = new NetworkConnection.PacketStat(); packetStat.msgType = key; ++packetStat.count; packetStat.bytes += (int)num; this.m_PacketStats[key] = packetStat; } } else { if (!LogFilter.logError) { break; } Debug.LogError((object)("Unknown message ID " + (object)key + " connId:" + (object)this.connectionId)); break; } } }
/// <summary> /// This makes the connection process the data contained in the stream, and call handler functions. /// <para>The data in the stream is assumed to have come from the network, and contains network messages.</para> /// <para>This function is used by network connections when they receive data.</para> /// </summary> /// <param name="reader">Stream that contains data.</param> /// <param name="receivedSize">Size of the data.</param> /// <param name="channelId">Channel the data was received on.</param> protected void HandleReader( NetworkReader reader, int receivedSize, int channelId) { // read until size is reached. // NOTE: stream.Capacity is 1300, NOT the size of the available data while (reader.Position < receivedSize) { // the reader passed to user code has a copy of bytes from the real stream. user code never touches the real stream. // this ensures it can never get out of sync if user code reads less or more than the real amount. ushort sz = reader.ReadUInt16(); short msgType = reader.ReadInt16(); // create a reader just for this message //TODO: Allocation!! byte[] msgBuffer = reader.ReadBytes(sz); NetworkReader msgReader = new NetworkReader(msgBuffer); if (logNetworkMessages) { StringBuilder msg = new StringBuilder(); for (int i = 0; i < sz; i++) { msg.AppendFormat("{0:X2}", msgBuffer[i]); if (i > k_MaxMessageLogSize) { break; } } Debug.Log("ConnectionRecv con:" + connectionId + " bytes:" + sz + " msgId:" + msgType + " " + msg); } NetworkMessageDelegate msgDelegate = null; if (m_MessageHandlersDict.ContainsKey(msgType)) { msgDelegate = m_MessageHandlersDict[msgType]; } if (msgDelegate != null) { m_NetMsg.msgType = msgType; m_NetMsg.reader = msgReader; m_NetMsg.conn = this; m_NetMsg.channelId = channelId; msgDelegate(m_NetMsg); lastMessageTime = Time.time; #if UNITY_EDITOR Profiler.IncrementStatIncoming(MsgType.HLAPIMsg); if (msgType > MsgType.Highest) { Profiler.IncrementStatIncoming(MsgType.UserMessage, msgType + ":" + msgType.GetType().Name); } #endif #if UNITY_EDITOR if (m_PacketStats.ContainsKey(msgType)) { PacketStat stat = m_PacketStats[msgType]; stat.count += 1; stat.bytes += sz; } else { PacketStat stat = new PacketStat(); stat.msgType = msgType; stat.count += 1; stat.bytes += sz; m_PacketStats[msgType] = stat; } #endif } else { //NOTE: this throws away the rest of the buffer. Need moar error codes if (LogFilter.logError) { Debug.LogError("Unknown message ID " + msgType + " connId:" + connectionId); } break; } } }