public bool TrySendLocal(Message message)
        {
            if (!message.TargetSilo.Equals(MyAddress))
            {
                return(false);
            }

            if (log.IsEnabled(LogLevel.Trace))
            {
                log.Trace("Message has been looped back to this silo: {0}", message);
            }
            MessagingStatisticsGroup.LocalMessagesSent.Increment();
            var localHandler = localMessageHandlers[(int)message.Category];

            if (localHandler != null)
            {
                localHandler(message);
            }
            else
            {
                InboundQueue.PostMessage(message);
            }

            return(true);
        }
Example #2
0
 internal void SendRejection(Message msg, Message.RejectionTypes rejectionType, string reason)
 {
     MessagingStatisticsGroup.OnRejectedMessage(msg);
     if (string.IsNullOrEmpty(reason)) reason = string.Format("Rejection from silo {0} - Unknown reason.", MyAddress);
     Message error = this.messageFactory.CreateRejectionResponse(msg, rejectionType, reason);
     // rejection msgs are always originated in the local silo, they are never remote.
     InboundQueue.PostMessage(error);
 }
Example #3
0
        public bool RecvData(out Tuple <AgentMetadata, List <AgentMessage> > data)
        {
            if (InboundQueue.Count > 0)
            {
                data = InboundQueue.Dequeue();
                return(true);
            }

            data = null;
            return(false);
        }
Example #4
0
        public void Dispose()
        {
            if (ima != null)
            {
                ima.Dispose();
                ima = null;
            }

            InboundQueue?.Dispose();
            OutboundQueue?.Dispose();

            GC.SuppressFinalize(this);
        }
Example #5
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();
        }
Example #6
0
 public Message WaitMessage(Message.Categories type, CancellationToken ct)
 {
     return(InboundQueue.WaitMessage(type));
 }
Example #7
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);
                    }
                }
            }
        }
Example #8
0
 public bool Receive(out Envelope envelope)
 {
     return(InboundQueue.TryDequeue(out envelope));
 }
Example #9
0
 protected void HandleReceivedMessage(Envelope envelope)
 {
     InboundQueue.Enqueue(envelope);
     OnMessageReceived?.Invoke(envelope);
 }