Example #1
0
        private void OnRecieve(IAsyncResult state)
        {
            CitpPacket newPacket;
            bool       restartRecieve = false;

            CitpRecieveData recieveState = (CitpRecieveData)(state.AsyncState);

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

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

                            if (NewPacket != null)
                            {
                                recieveState.WritePosition = recieveState.Length;
                                while (CitpPacketBuilder.TryBuild(recieveState, out newPacket) && newPacket != null)
                                {
                                    CitpTrace.RxPacket((IPEndPoint)client.Client.RemoteEndPoint, newPacket);

                                    //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 recieve another packet.
                if (restartRecieve)
                {
                    StartRecieve(recieveState);
                }
                else
                {
                    RaiseDisconnected();
                }
            }
        }
Example #2
0
 protected void StartRecieve(CitpRecieveData recieveState)
 {
     try
     {
         recieveState.SetLength(recieveState.WritePosition + recieveState.ReadNibble);
         client.Client.BeginReceive(recieveState.GetBuffer(), (int)recieveState.WritePosition, recieveState.ReadNibble, SocketFlags.None, new AsyncCallback(OnRecieve), recieveState);
     }
     catch (Exception ex)
     {
         OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving CITP.", ex));
     }
 }
Example #3
0
 public void StartRecieve()
 {
     try
     {
         EndPoint        localPort    = new IPEndPoint(IPAddress.Any, Port);
         CitpRecieveData recieveState = new CitpRecieveData();
         recieveState.SetLength(recieveState.Capacity);
         BeginReceiveFrom(recieveState.GetBuffer(), 0, recieveState.ReadNibble, SocketFlags.None, ref localPort, new AsyncCallback(OnRecieve), recieveState);
     }
     catch (Exception ex)
     {
         OnUnhandledException(new ApplicationException("An error ocurred while trying to start recieving CITP.", ex));
     }
 }
Example #4
0
        private void OnRecieve(IAsyncResult state)
        {
            CitpPacket newPacket;
            EndPoint   remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);

            if (PortOpen)
            {
                try
                {
                    CitpRecieveData recieveState = (CitpRecieveData)(state.AsyncState);

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

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

                            if (NewPacket != null)
                            {
                                if (CitpPacketBuilder.TryBuild(recieveState, out newPacket))
                                {
                                    NewPacket(this, new CitpNewPacketEventArgs((IPEndPoint)LocalEndPoint, (IPEndPoint)remoteEndPoint, newPacket));

                                    CitpTrace.RxPacket((IPEndPoint)remoteEndPoint, newPacket);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    OnUnhandledException(ex);
                }
                finally
                {
                    //Attempt to recieve another packet.
                    StartRecieve();
                }
            }
        }