Beispiel #1
0
        private void Callback(IAsyncResult ar)
        {
            var s = _socket.EndAccept(ar);

            NewPeer?.Invoke(new PeerWireClient(s));

            _socket.BeginAccept(Callback);
        }
Beispiel #2
0
        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);
                }
            }
        }
Beispiel #3
0
        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);
                        });
                    }
                }
            }
        }