Exemplo n.º 1
0
        // This is called after the byte array segments have been successfully sent to the connected socket
        private void BeginSendCallback(object sender, SocketAsyncEventArgs args)
        {
            try
            {
                if (args.BytesTransferred <= 0)
                {
                    DnlDebugger.LogMessage("0 bytes transferred in the last send operation", true);
                    return;
                }

                // Another temp holder
                DnlUdpSendSegment segment;

                // If theres more segments
                if ((segment = sendSegments.Peek()) != null)
                {
                    // If the current segment has been advanced
                    if (segment.Advance(args.BytesTransferred))
                    {
                        sendSegments.Dequeue();
                    }
                    // Recursively send the data if there's more segments to send
                    if (sendSegments.Count > 0)
                    {
                        BeginSend();
                    }
                    else
                    {
                        // Set this thread's sending state to idle
                        isSending = 0;
                    }
                }
            }
            catch (Exception e)
            {
                DnlDebugger.LogMessage("Caught exception in EndSendCallback: " + e.Message + " | " + e.TargetSite.ToString(), true);
            }
            finally
            {
                args.Dispose();
            }
        }
Exemplo n.º 2
0
        public void Stop()
        {
            if (!IsListening)
            {
                return;
            }

            IsListening = false;

            try
            {
                Socket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception e)
            {
                DnlDebugger.LogMessage("Exception caught when trying to shut down listener: " + e.Message, true);
            }
            finally
            {
                Socket.Close();
            }
        }
Exemplo n.º 3
0
        private void BeginReceiveCallback(object sender, SocketAsyncEventArgs args)
        {
            if (!IsListening)
            {
                return;
            }

            try
            {
                // No bytes been transferred from this async operation
                if (args.BytesTransferred <= 0)
                {
                    DnlDebugger.LogMessage("0 bytes transferred from receive operation: " + args.RemoteEndPoint.ToString() + " | " + args.BytesTransferred, true);
                }
                else
                {
                    ushort protocolId;
                    ushort operationCode;

                    // Parse and validate protocol id and operation code
                    if ((protocolId = unchecked (BitConverter.ToUInt16(args.Buffer, 0))) != ProtocolId)
                    {
                        throw new Exception("Received datagram with different protocol id then expected. Either set your protocol id or ensure network traffic is from known clients.");
                    }

                    if (!OperationCodes.ReceivePacket.ContainsKey(operationCode = unchecked (BitConverter.ToUInt16(args.Buffer, 2))))
                    {
                        throw new Exception("Received datagram with different unregistered operation code. Either register your operation packet handler or ensure network traffic is from known clients.");
                    }

                    int    payloadLength = args.BytesTransferred - (sizeof(ushort) * 2);
                    byte[] payload       = new byte[payloadLength];

                    // Parse payload from datagram
                    Buffer.BlockCopy(args.Buffer, 4, payload, 0, payloadLength);

                    // Parse ip and port from endpoint string
                    string[] systemAddress = !string.IsNullOrEmpty(args.RemoteEndPoint.ToString()) ?
                                             args.RemoteEndPoint.ToString().Split(':') :
                                             throw new Exception("Internal issue trying to parse remote endpoint from incoming datagram. Contact support");

                    // Invoke the on received event
                    OnDatagramReceived?.Invoke(this, new DatagramReceivedEventArgs(
                                                   systemAddress[0],
                                                   int.Parse(systemAddress[1]),
                                                   protocolId,
                                                   operationCode,
                                                   payload,
                                                   payload.Length,
                                                   DateTime.Now));
                }
            }
            catch (Exception e)
            {
                DnlDebugger.LogMessage("Caught exception in EndReceive: " + e.Message + " | " + e.TargetSite.ToString(), true);
            }
            finally
            {
                args.Dispose();
                BeginReceive();
            }
        }