private void Callback(IAsyncResult ar) { var s = _socket.EndAccept(ar); NewPeer?.Invoke(new PeerWireClient(s)); _socket.BeginAccept(Callback); }
public void ElectNewManagerCommandTest() { var cmd = new ElectNewManagerCommand(Station.Address); NewPeer.AddPeer(Station.Address, new AsymmetricKey(Station.Crypto.KeyPair.Public)); Manager.StopListening(); cmd.Execute(NewPeer); Assert.That(NewPeer.Manager.Equals(Station.Address)); }
public void CryptoCommandTest() { var cmd = new CryptoCommand(Manager, Station.Address, new StartElectionCommand(Manager.Address)); Assert.That(cmd.Sender == Manager.Address); Assert.That(!Station.ElectionInProgress); cmd.Execute(Station); Assert.That(Station.ElectionInProgress); // Station sending to NewPeer NewPeer.RemovePeer(Manager.Address, false); cmd = new CryptoCommand(Station, NewPeer.Address, new StartElectionCommand(Station.Address)); Assert.That(!NewPeer.ElectionInProgress); Assert.Throws <TheOnlyException>(() => cmd.Execute(NewPeer)); Assert.That(!NewPeer.ElectionInProgress); }
private void Process() { var buffer = new byte[200]; while (!_killSwitch) { EndPoint endPoint = new IPEndPoint(0, 0); udpReaderSocket.ReceiveFrom(buffer, ref endPoint); var remoteAddress = ((IPEndPoint)endPoint).Address; var remotePort = 0; var remoteHash = string.Empty; var packet = Encoding.ASCII.GetString(buffer).Trim(); if (!packet.StartsWith("BT-SEARCH")) { continue; } var packetLines = packet.Split('\n'); foreach (var line in packetLines) { if (line.StartsWith("Port:")) { var portStr = line.Substring(5).Trim(); int.TryParse(portStr, out remotePort); } if (line.StartsWith("Infohash:")) { remoteHash = line.Substring(10, 40); } } if (!string.IsNullOrEmpty(remoteHash) && remotePort != 0) { NewPeer?.Invoke(remoteAddress, remotePort, remoteHash); } } }
public void TearDown() { if (Manager != null) { Manager.Dispose(); } if (Station != null) { Station.Dispose(); } if (NewPeer != null) { NewPeer.Dispose(); } Manager = null; Station = null; NewPeer = null; File.Delete("CommandsTestsManagerTestVoters.sqlite"); File.Delete("CommandsTestsManagerLogTestDb.sqlite"); File.Delete("CommandsTestsManagerPeerTestDb.sqlite"); }
private void OnMessageIn(string senderId, string messageType, string messagePayload) { if (disconnecting) { return; } DateTime messageTime = DateTime.UtcNow; if (messageType.Equals("I_AM_HERE", StringComparison.InvariantCultureIgnoreCase)) { TimeSpan statusExpirationTimeout = TimeSpan.FromSeconds(int.Parse(messagePayload)); bool newPeer = true; if (peers.ContainsKey(senderId)) { newPeer = false; peers[senderId].Resurrect(); PeerStatusChanged?.Invoke(this, peers[senderId]); } string response = $"[\"{(int)Me.StatusExpirationTimeout.TotalSeconds}\", \"{Me.Status.ToString()}\"]"; SendImportantMessage(senderId, false, "HELLO", response, true) .ContinueWith((res) => { if (newPeer) { peers[senderId] = new SignalNowPeer(senderId, PeerStatus.Online, statusExpirationTimeout); NewPeer?.Invoke(this, peers[senderId]); } }); } else if (messageType.Equals("HELLO", StringComparison.InvariantCultureIgnoreCase)) { JArray paramArray = JArray.Parse(messagePayload); var timeoutStr = paramArray[0].ToString(); var statusStr = paramArray[1].ToString(); TimeSpan statusExpirationTimeout = TimeSpan.FromSeconds(int.Parse(timeoutStr)); PeerStatus status = (PeerStatus)Enum.Parse(typeof(PeerStatus), statusStr, true); if (!peers.ContainsKey(senderId)) { peers[senderId] = new SignalNowPeer(senderId, status, statusExpirationTimeout); NewPeer?.Invoke(this, peers[senderId]); } else { peers[senderId].LastStatusTime = messageTime; peers[senderId].Status = status; peers[senderId].StatusExpirationTimeout = statusExpirationTimeout; PeerStatusChanged?.Invoke(this, peers[senderId]); } } else if (messageType.Equals("I_AM_OUTTA_HERE", StringComparison.InvariantCultureIgnoreCase)) { if (peers.ContainsKey(senderId)) { peers[senderId].Status = PeerStatus.Offline; PeerStatusChanged?.Invoke(this, peers[senderId]); #if UNITY_2018_2_OR_NEWER UnityEngine.Debug.Log($"{senderId} quit."); #else System.Diagnostics.Debug.WriteLine($"{senderId} quit."); #endif } } else { // only respect messages from peers we had a handshake with if (peers.ContainsKey(senderId)) { peers[senderId].LastDataMessageTime = messageTime; peers[senderId].LastStatusTime = messageTime; if (messageType.Equals("STILL_HERE", StringComparison.InvariantCultureIgnoreCase)) { PeerStatus status = (PeerStatus)Enum.Parse(typeof(PeerStatus), messagePayload, true); peers[senderId].Status = status; } else { Task.Run(() => { NewMessage?.Invoke(this, senderId, messageType, messagePayload); }); } } } }