public void Host_ReceiveDataTest() { GameMessage inMessage = new GameMessage { Content = _messageContent }; GameMessage outMessage = new GameMessage(); Host host = new Host(); Action<IPAddress, IConfigurable> theAction = (notImportant, receivedObject) => outMessage = (GameMessage)receivedObject; host.ObjectReceived += theAction; host.ReceiveData(_someAddress, _converter.ObjectToString(inMessage)); Assert.That(outMessage.Content.Equals(inMessage.Content)); host.ObjectReceived -= theAction; host.StopMonitoringDataFromSender(_someAddress); bool malformedEventNotRaised = true; Action<IPAddress, string> shouldNotExecute = (notImportant, whoCares) => malformedEventNotRaised = false; object outObject = new object(); theAction = (notImportant, receivedObject) => outObject = receivedObject; host.ObjectReceived += theAction; host.ReceiveData(_someAddress, _validObjectString); Assert.That(malformedEventNotRaised); Assert.That(outObject == null); host.ObjectReceived -= theAction; host.StopMonitoringDataFromSender(_someAddress); string mixedString = _validObjectString + _validObjectString + _malformedObjectString + _otherMessageContent; List<object> validObjects = new List<object>(); Action<IPAddress, IConfigurable> onValidReceived = (notImportant, receivedObject) => validObjects.Add(receivedObject); host.ObjectReceived += onValidReceived; host.ReceiveData(_someAddress, mixedString); Assert.That(validObjects[0] == null); Assert.That(validObjects[1] == null); }
public void SendChat(string message) { var gameMessage = new GameMessage(); gameMessage.Content = _mainMenu.PlayerName + ": " + message; if (IsHost) { _host.SendObjectTCP(gameMessage); ChatText.AppendLine(gameMessage.Content); NewChatMessage = true; } else { _client.SendObjectTcp(gameMessage); } }
/* gimme an IP address or I'll tell you to and close the connection. * This is supposed to be the first real data that we get */ private void OnInitialTcpDataReceived(IAsyncResult asyncResult) { TcpClient client = (TcpClient)asyncResult.AsyncState; NetworkStream stream = client.GetStream(); byte[] bytes = _clientsAndBytes[client]; int numberOfBytesRead = stream.EndRead(asyncResult); string dataReceived = System.Text.Encoding.ASCII.GetString(bytes, 0, numberOfBytesRead); Match match = _sloppyIPAddressRegex.Match(dataReceived); if (!match.Success) { try { SendObjectTcpToClient(new NetworkMessage { Content = "Problem. No IP address." }, client); } /* My connection to this client might have broken already. * If it has, I just want to prevent a crash on this end. * Nothing else needs be done. */ catch { } _clientsToClose.Add(client); return; } IPAddress ipAddress; if (!IPAddress.TryParse(match.Value, out ipAddress)) { try { SendObjectTcpToClient(new NetworkMessage { Content = "Problem. Could not parse IP address." }, client); } /* My connection to this client might have broken already. * If it has, I just want to prevent a crash on this end. * Nothing else needs be done. */ catch { } _clientsToClose.Add(client); return; } if (CurrentlyAcceptingPlayers || _ipAddressesWithGameAccess.Contains(ipAddress)) { bool updatedClientsAndUdpEndPoints = false; var keys = _clientsAndUdpEndPoints.Keys.ToArray(); var values = _clientsAndUdpEndPoints.Values.ToArray(); for (int j = values.Length - 1; j >= 0; --j) { IPEndPoint endPoint = values[j]; if (endPoint.Address.Equals(ipAddress)) { _clientsAndUdpEndPoints.Remove(keys[j]); _clientsAndUdpEndPoints.Add(client, endPoint); updatedClientsAndUdpEndPoints = true; } } if (!updatedClientsAndUdpEndPoints) { _clientsAndUdpEndPoints.Add(client, new IPEndPoint(ipAddress, 8307)); } if (!_ipAddressesWithGameAccess.Contains(ipAddress)) { _ipAddressesWithGameAccess.Add(ipAddress); } stream.BeginRead(bytes, 0, bytes.Length, OnTcpDataReceived, client); } else { IConfigurable objectSent; if (null == WhatToTellPlayersThatCantJoin) { objectSent = new GameMessage { Content = String.Empty }; } else { objectSent = new GameMessage { Content = WhatToTellPlayersThatCantJoin }; } try { SendObjectTcpToClient(objectSent, client); } /* My connection to this client might have broken already. * If it has, I just want to prevent a crash on this end. * Nothing else needs be done. */ catch { } _clientsToClose.Add(client); } }