Ejemplo n.º 1
0
        private void OnReceive(IAsyncResult state)
        {
            CitpPacket newPacket;
            bool       restartReceive = false;

            CitpReceiveData receiveState = (CitpReceiveData)(state.AsyncState);

            try
            {
                if (receiveState != null && client != null && client.Connected)
                {
                    receiveState.SetLength((receiveState.Length - receiveState.ReadNibble) + client.Client.EndReceive(state));

                    if (receiveState.Length > 0 && !IsDisposed())
                    {
                        //We want to start the receive again to listen for more data.
                        //Only do this when the client is in a position to do so.
                        restartReceive = true;

                        if (NewPacket != null)
                        {
                            while (CitpPacketBuilder.TryBuild(receiveState, out newPacket))
                            {
                                receiveState.ReadPosition += (int)((CitpHeader)newPacket).MessageSize;

                                //Packet has been read successfully.
                                NewPacket(this, new CitpNewPacketEventArgs((IPEndPoint)client.Client.LocalEndPoint, (IPEndPoint)client.Client.RemoteEndPoint, newPacket));
                            }
                        }
                    }
                }
            }
            catch (SocketException)
            {
                //Connect has been closed.
            }
            catch (Exception ex)
            {
                OnUnhandledException(ex);
            }
            finally
            {
                //Attempt to receive another packet.
                if (restartReceive)
                {
                    StartReceive(receiveState);
                }
                else
                {
                    RaiseDisconnected();
                }
            }
        }
Ejemplo n.º 2
0
 protected void StartReceive(CitpReceiveData receiveState)
 {
     try
     {
         receiveState.SetLength(receiveState.ReadPosition + receiveState.ReadNibble);
         client.Client.BeginReceive(receiveState.GetBuffer(), receiveState.ReadPosition, receiveState.ReadNibble, SocketFlags.None, new AsyncCallback(OnReceive), receiveState);
     }
     catch (Exception ex)
     {
         OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving CITP.", ex));
     }
 }
Ejemplo n.º 3
0
 public void StartReceive()
 {
     try
     {
         EndPoint        localPort    = new IPEndPoint(IPAddress.Any, Port);
         CitpReceiveData receiveState = new CitpReceiveData();
         receiveState.SetLength(receiveState.Capacity);
         BeginReceiveFrom(receiveState.GetBuffer(), 0, receiveState.ReadNibble, SocketFlags.None, ref localPort, new AsyncCallback(OnReceive), receiveState);
     }
     catch (Exception ex)
     {
         OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving CITP.", ex));
     }
 }
Ejemplo n.º 4
0
        private void OnReceive(IAsyncResult state)
        {
            CitpPacket newPacket;
            EndPoint   remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            if (PortOpen)
            {
                try
                {
                    CitpReceiveData receiveState = (CitpReceiveData)(state.AsyncState);

                    if (receiveState != null)
                    {
                        receiveState.SetLength(EndReceiveFrom(state, ref remoteEndPoint));

                        //Protect against UDP loopback where we receive our own packets.
                        if (LocalEndPoint != remoteEndPoint && receiveState.Valid)
                        {
                            LastPacket = DateTime.Now;

                            if (NewPacket != null)
                            {
                                if (CitpPacketBuilder.TryBuild(receiveState, out newPacket))
                                {
                                    NewPacket(this, new CitpNewPacketEventArgs((IPEndPoint)LocalEndPoint, (IPEndPoint)remoteEndPoint, newPacket));
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnUnhandledException(ex);
                }
                finally
                {
                    //Attempt to receive another packet.
                    StartReceive();
                }
            }
        }