Ejemplo n.º 1
0
 private void OnException(Peer peer, Exception exception)
 {
     MessageHandler.Run(() => {
         Debug.LogError(peer?.Remote + ": " + exception?.Message);
         OnPeerException?.Invoke(peer, exception);
     });
 }
Ejemplo n.º 2
0
 public void Close()
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             _cancelToken.Cancel();
             SpinWait.SpinUntil(() => _pollExited);
             //_client?.GetStream().Dispose();
             _client?.Dispose();
         }
         catch (Exception ex)
         {
             OnPeerException?.Invoke(this, ex);
         }
     });
 }
Ejemplo n.º 3
0
 public void Disconnect()
 {
     _cancelToken.Cancel();
     _resetEvent.WaitOne();
     try
     {
         _client?.Client?.Shutdown(SocketShutdown.Both);
     }
     catch (Exception ex)
     {
         OnPeerException?.Invoke(this, ex);
     }
     finally
     {
         _resetEvent.Set();
     }
 }
Ejemplo n.º 4
0
 public void Send(byte qualifier, byte command, byte[] data)
 {
     _resetEvent.WaitOne();
     try
     {
         //_client.Client.SendTimeout = 1000;
         var headerLength = _serviceIdentifier.Length + 6;
         var lenBuffer    = BitConverter.GetBytes(data.Length);
         var message      = new byte[headerLength + data.Length];
         _serviceIdentifier.CopyTo(message, 0);
         lenBuffer.CopyTo(message, _serviceIdentifier.Length);
         message[_serviceIdentifier.Length + 4] = qualifier;
         message[_serviceIdentifier.Length + 5] = command;
         data.CopyTo(message, headerLength);
         _client.Client.Send(message);
     }
     catch (SocketException ex)
     {
         switch (ex.SocketErrorCode)
         {
         case SocketError.ConnectionAborted:
         case SocketError.ConnectionReset:
         case SocketError.Disconnecting:
         case SocketError.HostDown:
         case SocketError.NetworkDown:
         case SocketError.NotConnected:
         case SocketError.Shutdown:
             _cancelToken.Cancel();
             OnDisconnect?.Invoke(this);
             Disconnect();
             break;
         }
     }
     catch (Exception ex)
     {
         OnPeerException?.Invoke(this, ex);
     }
     finally
     {
         _resetEvent.Set();
     }
 }
Ejemplo n.º 5
0
        private async void Poll()
        {
            _pollExited  = false;
            _cancelToken = new CancellationTokenSource();
            var headerLength = _serviceIdentifier.Length + 6;
            var timer        = new Timer(Maintain, null, TimeSpan.FromSeconds(120), TimeSpan.FromSeconds(120));

            SendIdentify();
            Send(NetworkQualifier, PingCommand, new byte[0]);
            while ((_client.Connected) && (!_cancelToken.IsCancellationRequested))
            {
                try
                {
                    var header = new byte[headerLength];

                    if (Recieve(header) != headerLength)
                    {
                        continue;
                    }

                    var servIdSegment  = new ArraySegment <byte>(header, 0, _serviceIdentifier.Length).ToArray();
                    var lengthSegment  = new ArraySegment <byte>(header, _serviceIdentifier.Length, 4).ToArray();
                    var commandSegment = new ArraySegment <byte>(header, _serviceIdentifier.Length + 4, 2).ToArray();

                    if (!servIdSegment.SequenceEqual(_serviceIdentifier))
                    {
                        var flushBuffer = new byte[_client.Available];
                        _client.Client.Receive(flushBuffer, _client.Available, SocketFlags.None);
                        continue;
                    }

                    var msgLength = BitConverter.ToInt32(lengthSegment, 0);

                    if (msgLength < 0)
                    {
                        continue;
                    }

                    if (msgLength > MaxMessageSize)
                    {
                        continue;
                    }

                    var msgBuffer = new byte[msgLength];

                    var actualRecv = Recieve(msgBuffer);

                    if (actualRecv != msgLength)
                    {
                        continue;
                    }

                    _lastContact = DateTime.Now;

                    switch (commandSegment[0])
                    {
                    case NetworkQualifier:
                        ProcessNetworkCommand(commandSegment[1], msgBuffer);
                        break;

                    default:
                        var evtTask = Task.Factory.StartNew(() => OnReceiveMessage?.Invoke(this, commandSegment[1], msgBuffer));
                        break;
                    }
                }
                catch (SocketException ex)
                {
                    switch (ex.SocketErrorCode)
                    {
                    case SocketError.ConnectionAborted:
                    case SocketError.ConnectionReset:
                    case SocketError.Disconnecting:
                    case SocketError.HostDown:
                    case SocketError.NetworkDown:
                    case SocketError.NotConnected:
                    case SocketError.Shutdown:
                        _cancelToken.Cancel();
                        OnDisconnect?.Invoke(this);
                        Disconnect();
                        break;
                    }
                }
                catch (Exception ex)
                {
                    OnPeerException?.Invoke(this, ex);
                    await Task.Delay(1000);
                }
            }
            timer.Dispose();
            _pollExited = true;
        }