private void Close(ShutdownEventArgs reason, bool abort)
        {
            try
            {
                if (!IsOpen)
                {
                    throw new AlreadyClosedException(reason);
                }

                CloseReason = reason;
                var modelsCopy = Models.ToList();
                modelsCopy.ForEach(m =>
                {
                    if (abort)
                    {
                        m.Abort();
                    }
                    else
                    {
                        m.Close();
                    }
                });

                ConnectionShutdown?.Invoke(this, reason);
            }
            catch
            {
                if (!abort)
                {
                    throw;
                }
            }
        }
Beispiel #2
0
 private void StartListeningAsync()
 {
     Connected?.Invoke(serverIPEndPoint);
     while (true)
     {
         try{
             int    dataSize = TCPSegmentSizeFormater.ReceiveTCPSegmentSize(serverSocket).GetAwaiter().GetResult();
             byte[] data     = new byte[dataSize];
             ArraySegment <byte> dataSegment = new ArraySegment <byte>(data);
             int recievedData = 0;
             while (recievedData < dataSize)
             {
                 int lost = dataSize - recievedData;
                 if (lost > RecieveBufferLength)
                 {
                     lost = RecieveBufferLength;
                 }
                 ArraySegment <byte> tempSegment = dataSegment.SliceEx(recievedData, lost);
                 recievedData += serverSocket.ReceiveAsync(tempSegment, SocketFlags.None).GetAwaiter().GetResult();
             }
             RecieveDataEvent?.Invoke(dataSegment.Array, serverIPEndPoint);
         }catch (ObjectDisposedException) {
             return;
         }catch (SocketException) {
             ConnectionShutdown?.Invoke(serverIPEndPoint);
             if (!AutoReconnect)
             {
                 Reconnect().Wait();
             }
         }
     }
 }
Beispiel #3
0
        private async void PingAsync()
        {
            while (true)
            {
                await Task.Delay(TimeOutPing).ConfigureAwait(false);

                bool isAlive = serverSocket.Ping();
                if (isAlive)
                {
                    continue;
                }
                ConnectionShutdown?.Invoke(serverIPEndPoint);
                if (AutoReconnect)
                {
                    await Reconnect().ConfigureAwait(false);
                }
                else
                {
                    Disconnect();
                }
                continue;
            }
        }
Beispiel #4
0
 private void OnConnectionShutdown(ShutdownEventArgs e)
 {
     ConnectionShutdown?.Invoke(this, e);
 }
Beispiel #5
0
 internal void OnConnectionShutdown(HttpClientConnection sender, int errorCode)
 {
     ConnectionShutdown?.Invoke(sender, new ConnectionShutdownEventArgs(errorCode));
 }
Beispiel #6
0
 private void OnConnectionShutdown(object sender, ShutdownEventArgs e)
 {
     ConnectionShutdown?.Invoke(sender, e);
 }
Beispiel #7
0
 protected void RaiseConnectionShutdown(object sender, ShutdownEventArgs shutdownEventArgs)
 {
     ConnectionShutdown?.Invoke(sender, shutdownEventArgs);
 }