Esempio n. 1
0
        protected void Run()
        {
            UdpClient  socket       = new UdpClient(new IPEndPoint(IPAddress.Any, LocalPort));
            IPEndPoint recvEndpoint = new IPEndPoint(IPAddress.Any, LocalPort);

            socket.Client.ReceiveTimeout = 2000;

            try
            {
                while (ContinueThread)
                {
                    if (socket.Available > 0)
                    {
                        byte[] bytesReceived = socket.Receive(ref recvEndpoint);
                        if (bytesReceived.Length > 0)
                        {
                            Envelope tempEnvelope = new Envelope(recvEndpoint, Message.Decode(bytesReceived));
                            if (tempEnvelope.Message != null)
                            {
                                InboundQueue.Enqueue(tempEnvelope);
                            }
                        }
                    }
                    else if (!OutboundQueue.IsEmpty)
                    {
                        if (OutboundQueue.TryDequeue(out Envelope outboundEnvelope))
                        {
                            byte[] bytesToSend = outboundEnvelope.Message.Encode();
                            if (bytesToSend.Length > 0)
                            {
                                socket.Send(bytesToSend, bytesToSend.Length, outboundEnvelope.Address);
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(25);
                    }
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine("Testing UDP socket exception : " + exc.Message);
            }

            socket.Close();
        }
Esempio n. 2
0
        private void ReadCallback(IAsyncResult ar)
        {
            var state     = ar.AsyncState as StateObject;
            var handler   = state.workSocket;
            var bytesRead = 0;

            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException)
            {
                // client socket has gone away for "reasons".
            }

            if (bytesRead > 0)
            {
                var dataReceived = state.buffer.TrimBytes();
                var webRequest   = Encoding.UTF8.GetString(dataReceived);

                if (webRequest.Contains("Expect: 100-continue") || dataReceived.Length == state.buffer.Length)
                {
                    if (state.data != null)
                    {
                        var tmp = state.data;
                        state.data = new byte[tmp.Length + dataReceived.Length];
                        Buffer.BlockCopy(tmp, 0, state.data, 0, tmp.Length);
                        Buffer.BlockCopy(dataReceived, 0, state.data, tmp.Length, dataReceived.Length);
                    }
                    else
                    {
                        state.data = new byte[dataReceived.Length];
                        Buffer.BlockCopy(dataReceived, 0, state.data, 0, dataReceived.Length);
                    }

                    Array.Clear(state.buffer, 0, state.buffer.Length);
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
                }
                else
                {
                    byte[] final;
                    if (state.data != null)
                    {
                        final = new byte[state.data.Length + dataReceived.Length];
                        Buffer.BlockCopy(state.data, 0, final, 0, state.data.Length);
                        Buffer.BlockCopy(dataReceived, 0, final, state.data.Length, dataReceived.Length);
                    }
                    else
                    {
                        final = new byte[dataReceived.Length];
                        Buffer.BlockCopy(dataReceived, 0, final, 0, dataReceived.Length);
                    }

                    var finalRequest = Encoding.UTF8.GetString(final);

                    var agentMetadata = ExtractAgentMetadata(finalRequest);
                    if (agentMetadata != null)
                    {
                        var agentMessages = ExtractAgentMessage(finalRequest);

                        if (agentMessages != null)
                        {
                            var tuple = new Tuple <AgentMetadata, List <AgentMessage> >(agentMetadata, agentMessages);
                            InboundQueue.Enqueue(tuple);

                            var agentTasks = GetAgentTasks(agentMetadata.AgentID);
                            SendData(handler, agentTasks);
                        }
                    }
                    else
                    {
                        GenerateWebLog(webRequest, handler.RemoteEndPoint as IPEndPoint);
                    }
                }
            }
        }
Esempio n. 3
0
 protected void HandleReceivedMessage(Envelope envelope)
 {
     InboundQueue.Enqueue(envelope);
     OnMessageReceived?.Invoke(envelope);
 }