private void OnConnected(object sender, SocketAsyncEventArgs args)
        {
            //  Queue the first read and write
            CmdQueue.Push(CommandFactory.MakeCommand(DoNextRead, _blob));
            CmdQueue.Push(CommandFactory.MakeCommand(DoPing));

            Start();

            //  We've sent the first request so schedule timer to start one interval later
            _timer = new Timer(TimerPingCallback, null, _refreshRate, _refreshRate);
        }
        private void OnReceive(object sender, SocketAsyncEventArgs args)
        {
            if (args.BytesTransferred > 0)
            {
                //  We received data so push it back onto the queue for processing
                CmdQueue.Push(CommandFactory.MakeCommand(DoProcessData, args.Buffer, args.BytesTransferred));
            }


            //  Queue up next read with new blob (assume that processing of data happens
            // prior to queueing the next read so we can recycle the blob).
            CmdQueue.Push(CommandFactory.MakeCommand(DoNextRead, _blob));
        }
        private void OnReceiveData(IAsyncResult res)
        {
            int data = _incoming.EndReceive(res);

            byte[] blob = res.AsyncState as byte[];

            if (data > 0)
            {
                //  We received data so push it back onto the queue for processing
                CmdQueue.Push(CommandFactory.MakeCommand(DoProcessData, blob, data));
            }


            //  Queue up next read with new blob (assume that processing of data happens
            // prior to queueing the next read so we can recycle the blob).
            CmdQueue.Push(CommandFactory.MakeCommand(DoNextRead, blob));
        }
        public AsyncConnectionDriver(Runtime.Runtime runtime, MemoryStream memStream)
            : base(false)
        {
            try
            {
                _memStream = memStream;
                _runtime   = runtime;

                _runtime.Driver = this;

                IPHostEntry e = Dns.GetHostEntry(HOST);

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

                _log.InfoFormat("Resolved {0} to {1}.", HOST, e.AddressList[0].ToString());

                _log.Info("Connecting...");

#if COMPACT
                EndPoint ep = new IPEndPoint(e.AddressList[0], PORT);
                _incoming.Connect(ep);
                _incoming.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
#else
                _incoming.Connect(e.AddressList, PORT);
                _incoming.ReceiveTimeout = 10000; // 10 seconds because we may not always be receiving data
#endif

                //  Queue the first read and write
                CmdQueue.Push(CommandFactory.MakeCommand(DoNextRead, new byte[BLOB_SIZE]));
                CmdQueue.Push(CommandFactory.MakeCommand(DoPing));

                Start();

                //  We've sent the first request so schedule timer to start one interval later
                _timer = new Timer(TimerPingCallback, null, _refreshRate, _refreshRate);

                _log.Info("Connected.");
            }
            catch (SocketException e)
            {
                throw new ConnectionException(e);
            }
        }
 private void TimerPingCallback(object state)
 {
     // It's time to do another ping
     CmdQueue.Push(CommandFactory.MakeCommand(DoPing));
 }