private IEnumerator ReceiveLoop()
        {
            int continuousZeroBytes = 0;

            while (m_IsConnected && m_Socket.Connected && m_IsReceiving)
            {
                IAsyncResult result = m_Socket.BeginReceive(m_ReceiveBuffer, 0, m_ReceiveBuffer.Length, SocketFlags.None, null, null);
                while (!result.IsCompleted)
                {
                    yield return(null);
                }

                int receivedSize = m_Socket.EndReceive(result);
                if (receivedSize > 0)
                {
                    continuousZeroBytes = 0;
                    byte[] receivedBytes = new byte[receivedSize];
                    for (int i = 0; i < receivedSize; i++)
                    {
                        receivedBytes[i] = m_ReceiveBuffer[i];
                    }
                    OnRecieved?.Invoke(receivedBytes);
                }
                else
                {
                    continuousZeroBytes = CheckConnection(continuousZeroBytes);
                }
            }

            //if stopped due to stop receiving by polling result
            m_Receiving = null;
            Close();
        }
Ejemplo n.º 2
0
    IEnumerator Start()
    {
        while (true)
        {
            if (_disconnectTriggered)
            {
                OnDisconnected?.Invoke();
                _disconnectTriggered = false;
            }

            if (_connectedTriggered)
            {
                OnConnected?.Invoke();
                _connectedTriggered = false;
            }

            if (_failedToConnectTriggered)
            {
                OnFailedToConnect?.Invoke();
                _failedToConnectTriggered = false;
            }

            while (_recievedQueue.Count > 0)
            {
                var nextRecieved = _recievedQueue.Dequeue();
                OnRecieved?.Invoke(nextRecieved);
            }

            yield return(null);
        }
    }
Ejemplo n.º 3
0
 private void method_5(SocketAsyncEventArgs socketAsyncEventArgs_2)
 {
     if (socketAsyncEventArgs_2.SocketError == SocketError.Success)
     {
         if (socketAsyncEventArgs_2.BytesTransferred > 0)
         {
             Socket obj = (Socket)socketAsyncEventArgs_2.UserToken;
             if (obj.Available == 0)
             {
                 byte[] array = new byte[socketAsyncEventArgs_2.BytesTransferred];
                 Array.Copy(socketAsyncEventArgs_2.Buffer, socketAsyncEventArgs_2.Offset, array, 0, array.Length);
                 if (arc4_1 != null)
                 {
                     arc4_1.decrypt(array);
                 }
                 OnRecieved onRecieved = onRecieved_0;
                 if (onRecieved != null)
                 {
                     onRecieved.BeginInvoke(array, null, null);
                 }
             }
             if (!obj.ReceiveAsync(socketAsyncEventArgs_2))
             {
                 method_5(socketAsyncEventArgs_2);
             }
         }
         else
         {
             method_6();
         }
     }
 }
Ejemplo n.º 4
0
        private void method_0(IAsyncResult iasyncResult_0)
        {
            NetworkStream networkStream = (NetworkStream)iasyncResult_0.AsyncState;

            try
            {
                int num = networkStream.EndRead(iasyncResult_0);
                if (num > 0)
                {
                    byte[] bytes = byte_0.Take(num).ToArray();
                    if (arc4_1 != null)
                    {
                        arc4_1.decrypt(bytes);
                    }
                    OnRecieved onRecieved = onRecieved_0;
                    if (onRecieved != null)
                    {
                        onRecieved(bytes);
                    }
                }
                byte_0 = new byte[8192];
                networkStream.BeginRead(byte_0, 0, byte_0.Length, method_0, networkStream);
            }
            catch
            {
            }
        }
Ejemplo n.º 5
0
 private void timer_0_Elapsed(object sender, ElapsedEventArgs e)
 {
     if (Interlocked.Exchange(ref int_1, 1) == 0)
     {
         try
         {
             if (tcpClient_0.Available > 0 && networkStream_0.CanRead)
             {
                 byte[] array = new byte[8192];
                 int    num   = networkStream_0.Read(array, 0, array.Length);
                 if (num != 0)
                 {
                     byte[] bytes = array.Take(num).ToArray();
                     if (arc4_1 != null)
                     {
                         arc4_1.decrypt(bytes);
                     }
                     OnRecieved onRecieved = onRecieved_0;
                     if (onRecieved != null)
                     {
                         onRecieved(bytes);
                     }
                 }
             }
         }
         catch
         {
         }
         finally
         {
             Interlocked.Exchange(ref int_1, 0);
         }
     }
 }
Ejemplo n.º 6
0
        //void Sent(IAsyncResult ar) {
        //    var stream = (ar.AsyncState as NetworkStream);
        //    stream.EndWrite(ar);
        //}

        void Recieve(TcpClient client)
        {
            var stream = client.GetStream();

            while (_connected)
            {
                if (stream.CanRead)
                {
                    try {
                        var recievedBytes = _packetIO.Recieve();
                        OnRecieved?.Invoke(recievedBytes);
                        //int incomingBytes = stream.ReadByte();
                        //byte[] buffer = new byte[incomingBytes];

                        //stream.BeginRead(
                        //    buffer, 0, incomingBytes,
                        //    new AsyncCallback(Recieved),
                        //    new ReadResult {
                        //        Buffer = buffer,
                        //        Stream = stream
                        //    });
                        //_wait.WaitOne();
                    } catch (IOException) {
                        _connected = false;
                    }
                }
            }

            OnDisconnected?.Invoke();
        }
Ejemplo n.º 7
0
        internal override void InvokeEvent(ServerType type, IConnection conn, IMessage msg)
        {
            switch (type)
            {
            case ServerType.Started:
                OnStarted?.Invoke(conn, msg);
                break;

            case ServerType.Accepted:
                OnAccepted?.Invoke(conn, msg);
                break;

            case ServerType.Sended:
                OnSended?.Invoke(conn, msg);
                break;

            case ServerType.Received:
                OnRecieved?.Invoke(conn, msg);
                break;

            case ServerType.Disconnected:
                OnDisconnected?.Invoke(conn, msg);
                break;

            case ServerType.Stopped:
                OnStopped?.Invoke(conn, msg);
                break;
            }
        }
Ejemplo n.º 8
0
 public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, System.Action completionHandler)
 {
     OnRecieved?.Invoke(this, response);
     completionHandler();
 }
Ejemplo n.º 9
0
 public void Write(string text)
 {
     x.Enqueue(text);
     OnRecieved?.Invoke(this, null);
 }