Exemple #1
0
        public void Run()
        {
            Running = true;

            var ipHost        = Dns.GetHostEntry(Dns.GetHostName());
            var ipAddress     = ipHost.AddressList[0];
            var localEndPoint = new IPEndPoint(ipAddress, listening_port);

            var listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try {
                listener.Bind(localEndPoint);
                listener.Listen(10);

                while (Running)
                {
                    // logger.Info("Waiting for a message...");
                    var handler = listener.Accept();
                    data = null;

                    while (true)
                    {
                        buffer = new byte [BUFFER_SIZE];
                        int bytesRec = handler.Receive(buffer);
                        data += Encoding.ASCII.GetString(buffer, 0, bytesRec);
                        if (data.IndexOf("<EOF>", StringComparison.Ordinal) > -1)
                        {
                            break;
                        }
                    }

                    //logger.Info("Text received: " + data.Remove(data.Length - 5));

                    var message = Message.FromXML(data.Remove(data.Length - 5));
                    // logger.Info("Message type: " + message.GetType ());
                    // logger.Info("Message ID: " + message.MessageId);
                    // logger.Info("Message {0} received ({1})", message.MessageId, message.GetType ());

                    var ack     = new MessageAck(message.MessageId);
                    var ack_str = Message.GetXML(ack) + "<EOF>";

                    byte[] msg = Encoding.ASCII.GetBytes(ack_str);
                    handler.Send(msg);

                    processor.AddToProcessingQueue(message);

                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
            } catch (Exception e) {
                logger.Error(e.ToString());
            }
        }