protected virtual void OnRecieveMessage(NetIncomingMessage msg, bool isInternal) { #if DEBUGBUILD ClassLogger.LogDebug("Recieved a high level message from client ID: " + msg.SenderConnection.RemoteUniqueIdentifier); #endif if (Clients.HasKey(msg.SenderConnection.RemoteUniqueIdentifier)) //Client sent the message { NetworkMessageHandler.DispatchMessage(Clients[msg.SenderConnection.RemoteUniqueIdentifier].HighlevelPeer, msg, isInternal); } else if (ServerConnections.HasKey(msg.SenderConnection.RemoteUniqueIdentifier)) //Subserver sent a message { NetworkMessageHandler.DispatchMessage(ServerConnections[msg.SenderConnection.RemoteUniqueIdentifier].HighlevelPeer, msg, isInternal); } else { ClassLogger.LogWarn("Recieved highlevel message with no receving object."); } //At this point the message is for nobody and we shouldn't have recieved it. //In a perfect world we'd disconnect whoever sent it but we can't be sure a real client actually sent it //The package could be faked so just drop it. }
private void MessagePoll(NetPeer peer) { //TODO: Examine if this will cause latency issues for handling messages //This is done so that CPU usage isn't too high. It blocks the thread and waits for a message //Nothing but GladNet should be executing on the main thread anyway. NetIncomingMessage msg = peer.WaitMessage(10); //TODO: Refactor if (msg != null && msg.SenderConnection != null) { #if DEBUGBUILD ClassLogger.LogDebug("Recieved a message from client ID: " + msg.SenderConnection.RemoteUniqueIdentifier); #endif switch (msg.MessageType) { case NetIncomingMessageType.ConnectionApproval: ClassLogger.LogDebug("Hit connection approval"); if (this.NetworkMessageHandler.TryReadHailMessage(msg, ExpectedClientHailMessage)) { ClassLogger.LogDebug("About to approve."); try { msg.SenderConnection.Approve(); this.TryRegisterApprovedConnection(msg.SenderConnection, msg.SenderConnection.RemoteHailMessage.ReadByte()); } catch (NetException e) { #if DEBUGBUILD ClassLogger.LogError("Failed to read connection type byte from hail message packet. Exception: " + e.Message); #endif } } else { ClassLogger.LogWarn("Client failed to satisfy hailmessage. Expected: " + ExpectedClientHailMessage); } break; case NetIncomingMessageType.StatusChanged: //Malicious user could try to send a fake StatusChange without the extra byte so try to catch NetException. try { if (Clients.HasKey(msg.SenderConnection.RemoteUniqueIdentifier)) { this.ReadStatusChange((NetConnectionStatus)msg.ReadByte(), Clients[msg.SenderConnection.RemoteUniqueIdentifier]); } else if (ServerConnections.HasKey(msg.SenderConnection.RemoteUniqueIdentifier)) { this.ReadStatusChange((NetConnectionStatus)msg.ReadByte(), ServerConnections[msg.SenderConnection.RemoteUniqueIdentifier]); } else { //If this point is reached it indicates that the status change is not from a registered connection and //this could indicate potentially a subserver connecting or maybe a client message before hail approval. ReadStatusChange((NetConnectionStatus)msg.ReadByte(), msg.SenderConnection); } } catch (NetException e) { #if DEBUGBUILD ClassLogger.LogError("NetConnection ID: " + msg.SenderConnection.RemoteUniqueIdentifier + " sent a potentially malicious StatusChange update. Error: " + e.Message); #endif } break; case NetIncomingMessageType.ExternalHighlevelMessage: OnRecieveMessage(msg, false); break; case NetIncomingMessageType.Data: OnRecieveMessage(msg, true); break; } peer.Recycle(msg); } }