Beispiel #1
0
        /// <summary>
        /// Message receiving thread method
        /// </summary>
        /// <param name="socketObject">receiving socket</param>
        private void Receiver(object socketObject)
        {
            Socket  socket  = socketObject as Socket;
            Address address = new Address(socket.RemoteEndPoint.ToString());

            Trace.WriteLine(String.Format("Starting Receiver thread for socket {0}", socket.RemoteEndPoint.ToString()));

            // Receive buffer.
            byte[] buffer = new byte[Message.BUFFER_SIZE];
            int    offset = 0;

            while (socket.Connected)
            {
                try
                {
                    int missingBytesLength = Message.BUFFER_SIZE - offset;
                    int length             = offset > 0 ? missingBytesLength : Message.BUFFER_SIZE;

                    int receivedBytesLength = socket.Receive(buffer, offset, length, 0);
                    if (receivedBytesLength == Message.BUFFER_SIZE || receivedBytesLength + offset == Message.BUFFER_SIZE)
                    {
                        string paddedSerializedMessage    = Encoding.UTF8.GetString(buffer, 0, Message.BUFFER_SIZE);
                        string cleanedUpSerializedMessage = JsonHelpers.CleanUpPaddedSerializedMessage(paddedSerializedMessage);

                        Message receivedMessage = JsonConvert.DeserializeObject <Message>(cleanedUpSerializedMessage);

                        //HACK : we pass the ip:port address in the Property argument
                        if (receivedMessage.Name.Equals(ControlActions.REGISTER))
                        {
                            receivedMessage.Content = socket.RemoteEndPoint.ToString();
                        }
//#if DEBUG
//                        Trace.WriteLine(String.Format("Received  : {0}", receivedMessage));
//#endif
                        messagesReceived.Enqueue(receivedMessage);

                        offset = 0;
                    }
                    else
                    {
                        offset = receivedBytesLength;
                    }

                    //While there are bytes in the socket's buffer, we get them, read the messages and add them to the queue
                    //This usually only happens during the registration/subscription phase
                    if (socket.Available < Message.BUFFER_SIZE)
                    {
                        processEvent.Set();
                    }
                }
                catch (Exception exception)
                {
                    string content = String.Format("Receiver error : {0}", exception);
                    LogEvent(content);

                    CleanUp(address);
                    break;
                }
            }
        }
Beispiel #2
0
        private void Receiver()
        {
            //Trace.WriteLine("Starting Receiver() thread.");

            byte[] buffer = new byte[Message.BUFFER_SIZE];
            int    offset = 0;

            while (socket.Connected)
            {
                try
                {
                    int missingBytesLength = Message.BUFFER_SIZE - offset;
                    int length             = offset > 0 ? missingBytesLength : Message.BUFFER_SIZE;

                    int receivedBytesLength = socket.Receive(buffer, offset, length, 0);

                    if (receivedBytesLength == Message.BUFFER_SIZE || receivedBytesLength + offset == Message.BUFFER_SIZE)
                    {
                        string paddedSerializedData = Encoding.UTF8.GetString(buffer, 0, Message.BUFFER_SIZE);
                        string serializedMessage    = JsonHelpers.CleanUpPaddedSerializedMessage(paddedSerializedData);

                        Message message = JsonConvert.DeserializeObject <Message>(serializedMessage);
//#if DEBUG
//                        Trace.WriteLine(String.Format("Received    : {0}", message));
//                        Console.WriteLine(String.Format("Received    : {0}", message));
//#endif
                        if (message != null)
                        {
                            if (message.ContentType.Equals(ContentTypes.COMMAND))
                            {
                                OnCommandReceived(new MessageEventArgs(message));
                            }
                            else if (message.ContentType.Equals(ContentTypes.DATA))
                            {
                                OnDataReceived(new MessageEventArgs(message));
                            }
                            else if (message.ContentType.Equals(ContentTypes.HEARTBEAT))
                            {
                                HandleHeartbeatMessage(message);
                            }
                        }

                        offset = 0;
                    }
                    else
                    {
                        offset = receivedBytesLength;
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }
        }
Beispiel #3
0
        private void Receiver()
        {
            Thread.Sleep(1000);

            Log("Starting Receiver() thread.");

            while (true)
            {
                try
                {
                    missingBytesLength = Message.BUFFER_SIZE - offset;
                    length             = offset > 0 ? missingBytesLength : Message.BUFFER_SIZE;

                    // Begin receiving the data from the remote device.
                    receivedBytesLength = socket.Receive(buffer, offset, length, 0);

                    //HACK : for some unexplained reason, receiving "messages bursts" behaves in a better way when the thread gets some sleeping time
                    Thread.Sleep(RECEIVER_WAITING_TIME);

                    if (receivedBytesLength == Message.BUFFER_SIZE || receivedBytesLength + offset == Message.BUFFER_SIZE)
                    {
                        messageChars            = Encoding.UTF8.GetChars(buffer, 0, Message.BUFFER_SIZE);
                        paddedSerializedMessage = new string(messageChars);

                        if (paddedSerializedMessage != null)
                        {
                            serializedMessage = JsonHelpers.CleanUpPaddedSerializedMessage(paddedSerializedMessage);

                            deserializedObject = JsonSerializer.DeserializeString(serializedMessage);
                            hashTable          = deserializedObject as Hashtable;

                            message = new Message()
                            {
                                SendingDevice   = (string)hashTable["SendingDevice"],
                                ReceivingDevice = (string)hashTable["ReceivingDevice"],
                                FromDevice      = (string)hashTable["FromDevice"],
                                ToDevice        = (string)hashTable["ToDevice"],
                                ContentType     = Convert.ToInt32(hashTable["ContentType"].ToString()),
                                Name            = (string)hashTable["Name"],
                                Parameter       = hashTable.Contains("Parameter") && hashTable["Parameter"] != null ? (string)hashTable["Parameter"] : String.Empty,
                                Content         = hashTable.Contains("Content") && hashTable["Content"] != null ? (string)hashTable["Content"] : String.Empty
                            };

                            if (message != null)
                            {
                                Log("Received  : " + message.ToString());

                                lock (messagesReceived)
                                {
                                    messagesReceived.Enqueue(new Message(message));
                                }

                                receivedEvent.Set();
                            }
                        }

                        offset = 0;
                    }
                    else
                    {
                        offset = receivedBytesLength;
                    }
                }
                catch (Exception exception)
                {
                    Log("Receiver() : " + exception.ToString());
                    //throw;
                }
            }
        }