Exemplo n.º 1
0
        internal virtual void DataArrival(IAsyncResult asyncResult)
        {
            var bytesRecived = 0;

            try { bytesRecived = ConnectionSocket.EndReceive(asyncResult); }
            catch (Exception)
            {
                //Client quit unexpectantly
            }

            if (bytesRecived != 0)
            {
                var data = new byte[bytesRecived];
                Array.Copy(DataBuffer, data, bytesRecived);

                OnPacket(data);

                try
                {
                    ConnectionSocket.BeginReceive(DataBuffer, 0, DataBuffer.Length, SocketFlags.None, new AsyncCallback(DataArrival), null);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error occured recieving packet!");
                }
            }
            else
            {
                Disconnect();
            }
        }
Exemplo n.º 2
0
        internal virtual void DataArrival(IAsyncResult asyncResult)
        {
            var bytesRecived = 0;

            try
            {
                // TODO: Foi Forçado o cancelamento de uma conexão pelo host remoto.
                bytesRecived = ConnectionSocket.EndReceive(asyncResult);
            }
            catch (Exception e)
            {
                var trace = new StackTrace(e, true);
                Log.Print(LogType.Error,
                          $"{e.Message}: {e.Source}" +
                          $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
            }

            if (bytesRecived != 0)
            {
                var data = new byte[bytesRecived];
                Array.Copy(DataBuffer, data, bytesRecived);

                OnPacket(data);

                try
                {
                    // TODO: Cannot access a disposed object.
                    ConnectionSocket.BeginReceive(DataBuffer, 0, DataBuffer.Length, SocketFlags.None, DataArrival,
                                                  null);
                }
                catch (SocketException e)
                {
                    var trace = new StackTrace(e, true);
                    Log.Print(LogType.Error,
                              $"{e.Message}: {e.Source}" +
                              $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
                    ConnectionSocket.Close();
                }
                catch (Exception e)
                {
                    var trace = new StackTrace(e, true);
                    Log.Print(LogType.Error,
                              $"{e.Message}: {e.Source}" +
                              $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
                }
            }
            else
            {
                Disconnect();
            }
        }
Exemplo n.º 3
0
        public virtual void DataArrival(IAsyncResult asyncResult)
        {
            var bytesRecived = 0;

            try
            {
                bytesRecived = ConnectionSocket.EndReceive(asyncResult);
            }
            catch (Exception e)
            {
                var trace = new StackTrace(e, true);
                Log.Print(LogType.Error,
                          $"{e.Message}: {e.Source}" +
                          $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
            }

            if (bytesRecived != 0)
            {
                var data = new byte[bytesRecived];
                Array.Copy(DataBuffer, data, bytesRecived);

                OnPacket(data);

                try
                {
                    ConnectionSocket.BeginReceive(DataBuffer, 0, DataBuffer.Length, SocketFlags.None, DataArrival,
                                                  null);
                }
                catch (SocketException e)
                {
                    var trace = new StackTrace(e, true);
                    Log.Print(LogType.Error,
                              $"{e.Message}: {e.Source}" +
                              $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
                    ConnectionSocket.Close();
                }
                catch (Exception e)
                {
                    var trace = new StackTrace(e, true);
                    Log.Print(LogType.Error,
                              $"{e.Message}: {e.Source}" +
                              $"{trace.GetFrame(trace.FrameCount - 1).GetFileName()}:{trace.GetFrame(trace.FrameCount - 1).GetFileLineNumber()}");
                }
            }
            else
            {
                Disconnect();
            }
        }
Exemplo n.º 4
0
        public AbstractSession(int connectionID, Socket connectionSocket)
        {
            this.ConnectionID     = connectionID;
            this.ConnectionSocket = connectionSocket;
            this.DataBuffer       = new byte[BufferSize];

            try
            {
                ConnectionSocket.BeginReceive(DataBuffer, 0, DataBuffer.Length, SocketFlags.None, DataArrival, null);
            }
            catch (SocketException)
            {
                Disconnect();
            }
        }
Exemplo n.º 5
0
 private void ReadCallback(IAsyncResult ar)
 {
     try
     {
         int bytesRead = ConnectionSocket.EndReceive(ar);
         if (bytesRead > 0)
         {
             streamParser.Push(buffer, 0, bytesRead);
             ConnectionSocket.BeginReceive(buffer, 0, BUFFERSIZE, 0, new AsyncCallback(ReadCallback), null);
         }
     }
     catch (Exception ex)
     {
         LogTools.Write(this.SessionId, ex, "Connection.ReadCallback");
     }
 }
Exemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="reintentos"></param>
 protected void Listen(int reintentos)
 {
     if (ConnectionSocket == null)
     {
         return;
     }
     try
     {
         ConnectionSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, 0, Read, null);
     }
     catch
     {
         if (reintentos < 3)
         {
             Listen(reintentos + 1);
         }
         else
         {
             Dispose();
         }
     }
 }
Exemplo n.º 7
0
        private int AsyncReceive(out byte[] data, int timeout)
        {
            data = new byte[ReceiveBufferSize];

            IAsyncResult receiveResult = ConnectionSocket.BeginReceive(data, 0, ReceiveBufferSize, SocketFlags.None, null, null);

            bool success = false;

            if (timeout > 0)
            {
                success = receiveResult.AsyncWaitHandle.WaitOne(timeout, true);
            }
            else if (timeout == 0)
            {
                success = receiveResult.AsyncWaitHandle.WaitOne();
            }

            if (!success)
            {
                data = null;
                ConnectionSocket.Dispose();
                return(-1);
            }

            int size = ConnectionSocket.EndReceive(receiveResult);

            if (size > 0)
            {
                TrimData(ref data, size);
            }
            else
            {
                data = null;
            }

            return(size);
        }
 private void Listen()
 {
     ConnectionSocket.BeginReceive(dataBuffer, 0, dataBuffer.Length, 0, Read, null);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Listens for incoming data
 /// </summary>
 private void Listen() => ConnectionSocket.BeginReceive(_dataBuffer, _dataBufferOffset, _dataBuffer.Length - _dataBufferOffset, 0, Read, null);