Esempio n. 1
0
        public void Send(IMessage message)
        {
            lock (sendLock)
            {
                if (message == null)
                    throw new NullReferenceException("Attempt to send a null message");

                try
                {
                    using (StreamWrapper wrapper = new StreamWrapper(Transport.Stream))
                    {
                        Envelope.Serialize(message, wrapper);
                    }

                    string endpoint = Transport.RemoteEndPoint == null ? "Unknown" : Transport.RemoteEndPoint.ToString();
                    logger.Logger.Log(null, Level.Finer, string.Format("Sent message {0} to {1}", message.DescriptorForType.FullName, endpoint), null);
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    client.Disconnect(true);
                }
            }
        }
Esempio n. 2
0
        private void ProcessMessages()
        {
            running = true;

            try
            {
                while (running)
                {
                    using (StreamWrapper wrapper = new StreamWrapper(client.Stream))
                    {
                        IMessage message = Envelope.Deserialize(wrapper) as IMessage;

                        MessageEventArgs args = new MessageEventArgs(this, message, wrapper.GetInputBytes());
                        OnMessageReceived(args);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                client.Disconnect(true);
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            Thread.Sleep(2000);
            Console.Write("Connecting to ChannelListener...");

            ClientConnection client = new ClientConnection(new PlainEnvelope(), new SocketTransport());

            //TcpClient client = new TcpClient();
            //client.Connect("localhost", port);
            client.ClientClosed += new EventHandler<DisconnectedArgs>(client_ClientClosed);
            client.MessageReceived += new EventHandler<MessageEventArgs>(client_MessageReceived);

            client.Connect("localhost", port);

            Console.WriteLine("connected!");

            DateTime connectTime = DateTime.Now;
            Console.WriteLine(string.Format("Connected at {0}", connectTime));

            int count = 1;

            while (true)
            {
                Dictionary<string, string> headers = new Dictionary<string, string>();
                string command = "";

                while (true)
                {
                    command = Console.ReadLine();
                    headers.Clear();

                    while (true)
                    {
                        string line = Console.ReadLine();

                        // check for termination
                        if (line.Length == 0)
                            break;

                        string[] pieces = line.Split(':');
                        headers.Add(pieces[0].ToUpper(), pieces[1].ToUpper());
                    }

                    break;
                }

                MessageOne msg = new MessageOne();
                msg.MessageID = "MessageOne";
                msg.Value = 23;

                //scm.ControlMode = ControlMode.ChannelController;

                try
                {
                    var env = client.Envelope;
                    using (StreamWrapper wrapper = new StreamWrapper(client.Transport.Stream))
                    {
                        env.Serialize(msg, wrapper);
                        //((SocketTransport)client.Transport)
                        //IMessage response = client.Send<Message>(msg);
                        //Console.WriteLine(msg.Name);
                    }
                }
                catch (Exception pex)
                {
                    Console.WriteLine(pex.Message);
                }

                //CustomMessage message = new CustomMessage(command);
                //foreach (string key in headers.Keys)
                //    message.AddKeyValue(key, headers[key]);

                //Message response;
                //{
                //    try
                //    {
                //        response = client.SendAndReceive(message);
                //        Console.WriteLine("Message received: " + response.Name);
                //        Console.WriteLine(response.ToString());
                //    }
                //    catch (Exception ex)
                //    {
                //        Console.WriteLine(ex.Message + "\r\n");
                //        Console.WriteLine("Reconnecting...");
                //        Console.WriteLine(client.Connect());
                //        connectTime = DateTime.Now;
                //        Console.WriteLine(string.Format("Connected at {0}", connectTime));
                //    }
                //}

                TimeSpan ts = DateTime.Now - connectTime;
                Console.WriteLine(string.Format("  connection open for {0:D2}:{1:D2}:{2:D2} [{3} messages processed]\r\n", ts.Hours, ts.Minutes, ts.Seconds, count++));
            }
        }