Exemple #1
0
        /// <summary>
        /// Async callback when a packet is received
        /// </summary>
        /// <param name="result"></param>
        private void OnPacketReceived(IAsyncResult result)
        {
            try
            {
                // Recalls the state
                var _state = result.AsyncState as StateObject;

                // Grabs the numbber of bytes received and ends the receive
                var bytes = _state.Socket.EndReceiveFrom(result, ref _state.RemoteEndpoint);

                // Creates a temp byte array to put the received data
                var packetData = new byte[bytes];

                // Coppies the buffer passed in the stateobject to the temp byte array instantiated above
                Array.Copy(_state.Buffer, 0, packetData, 0, packetData.Length);

                // Fires the packet received event for Tcp.cs to handle
                PacketReceived?.Invoke(new Packet(packetData));

                // Restarts the async receive loop
                _state.Socket.BeginReceiveFrom(_state.Buffer,
                                               0,
                                               _state.Buffer.Length,
                                               SocketFlags.None,
                                               ref _state.RemoteEndpoint,
                                               OnPacketReceived,
                                               new StateObject(_state.RemoteEndpoint, _state.Buffer, _state.Socket));
            }
            catch (Exception ex)
            {
                UdpError?.Invoke(ex);
            }
        }
Exemple #2
0
 /// <summary>
 /// Sends a single packet to the given endpoint
 /// </summary>
 /// <param name="packet"></param>
 /// <param name="destination"></param>
 public void SendPacket(Packet packet, EndPoint destination)
 {
     try
     {
         using (var _soc = new Socket(SocketType.Dgram, ProtocolType.Udp))
         {
             _soc.SendTo(packet.Raw, destination);
         }
     }
     catch (Exception ex)
     {
         UdpError?.Invoke(ex);
     }
 }
Exemple #3
0
 /// <summary>
 /// Begins receiving packets asynchronously
 /// </summary>
 /// <param name="source"></param>
 public void BeginReceivePackets(EndPoint source)
 {
     try
     {
         _soc.Dispose();
         _soc = new Socket(SocketType.Dgram, ProtocolType.Udp);
         var _buff = new byte[10000];
         _soc.Bind(source);
         _soc.BeginReceiveFrom(_buff,
                               0,
                               _buff.Length,
                               SocketFlags.None,
                               ref source,
                               OnPacketReceived,
                               new StateObject(source, _buff, _soc));
     }
     catch (Exception ex)
     {
         UdpError?.Invoke(ex);
     }
 }