Example #1
0
        private static void OnWrite(IAsyncResult ar)
        {
            PushAndPullClient state = (PushAndPullClient)ar.AsyncState;

            state.Stream.EndWrite(ar);
            Console.WriteLine("Server.WriteDone");
        }
Example #2
0
        public void OnAsyncMessage(PipeStream pipe, byte[] data, int bytes, object state)
        {
            PushAndPullClient client = (PushAndPullClient)state;

            Console.WriteLine("Server.PingPong({0}): {1}", client.Id, Encoding.ASCII.GetString(data, 0, bytes));
            pipe.BeginWrite(data, 0, bytes, OnWrite, state);
        }
Example #3
0
        public void OnAsyncConnect(PipeStream pipe, out object state)
        {
            var client = new PushAndPullClient();

            client.Stream = pipe;
            client.Id     = Interlocked.Increment(ref clientId);

            state = client;
            Thread thread = new Thread(() => PeriodicPush(client));

            thread.Start();
        }
Example #4
0
        private static void PeriodicPush(PushAndPullClient client)
        {
            int i = 0;

            try
            {
                while (client.Stream.IsConnected)
                {
                    i++;
                    Thread.Sleep(1000);
                    string dataStr = string.Format("{0,5}", i);
                    byte[] data    = Encoding.ASCII.GetBytes(dataStr);
                    client.Stream.BeginWrite(data, 0, data.Length, OnWrite, client);
                }
            }
            catch (Exception ex)
            {
            }
        }