/// <summary> /// <para>Resets the statistics that are returned from NetworkClient.GetConnectionStats().</para> /// </summary> public void ResetStats() { for (short i = 0; i < s_MaxPacketStats; i = (short)(i + 1)) { if (this.m_PacketStats.ContainsKey(i)) { PacketStat stat = this.m_PacketStats[i]; stat.count = 0; stat.bytes = 0; NetworkTransport.SetPacketStat(0, i, 0, 0); NetworkTransport.SetPacketStat(1, i, 0, 0); } } }
public PacketStat(PacketStat s) { msgType = s.msgType; count = s.count; bytes = s.bytes; }
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> /// 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; } } }