Esempio n. 1
0
        public void ListenForMoves()
        {
            MoveStateObject state  = null;
            IAsyncResult    result = null;

            while (!stop)
            {
                done.Reset();
                state  = new MoveStateObject();
                result = server.BeginReceive(state.buffer, 0, MoveStateObject.BufferSize, 0, new AsyncCallback(ReadCallBack), state);
                done.WaitOne();
            }
            Console.WriteLine("Broke out of while");
            server.Close();            // Stop waiting for another move, and
            speaker.MakeNewSocket();
        }
Esempio n. 2
0
        private void ReadCallBack(IAsyncResult ar)
        {
            Console.WriteLine("Enters here again");
            MoveStateObject obj  = (MoveStateObject)ar.AsyncState;
            int             recv = 0;

            try
            {
                recv = server.EndReceive(ar);
            } catch (Exception e)
            {
                Console.WriteLine("After Close: " + e.ToString());
            }
            if (recv > 0)
            {
                string received = Encoding.ASCII.GetString(obj.buffer, 0, recv);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                MoveData             md         = serializer.Deserialize <MoveData>(received);
                speaker.MoveOccured(md);
            }
            done.Set();
        }