예제 #1
0
        public void AcceptCallback(IAsyncResult ar)
        {
            this.resetEvent.Set();
            //Console.WriteLine("Connection received!");

            TcpClient client = ((TcpListener)ar.AsyncState).EndAcceptTcpClient(ar);
            RecievedObject recievedData = new RecievedObject();
            recievedData.client = client;
            client.GetStream().BeginRead(recievedData.Buffer, 0, RecievedObject.BufferSize, new AsyncCallback(handleData), recievedData);
        }
예제 #2
0
        public void handleData(IAsyncResult ar)
        {
            RecievedObject recvData = (RecievedObject)ar.AsyncState;

            int bytesRead = recvData.client.GetStream().EndRead(ar);

            if(bytesRead > 0)
            {
                string[] commands = Encoding.ASCII.GetString(recvData.Buffer, 0, bytesRead).Trim().Split(',');
                //Console.WriteLine(commands[0]);
                switch (commands[0])
                {
                    case "getPlayerID":
                        this.GetPlayerID(recvData.client.GetStream());
                        break;
                    case "getPlayerPosition":
                        string positionString = "null";
                        if(commands.Length > 1)
                        {
                            positionString = this.GetPlayerPosition(ulong.Parse(commands[1]));
                        }
                        this.SendResponse(recvData.client.GetStream(), positionString);
                        break;
                }
            }
            RecievedObject recievedData = new RecievedObject();
            recievedData.client = recvData.client;
            recvData.client.GetStream().BeginRead(recievedData.Buffer, 0, RecievedObject.BufferSize, new AsyncCallback(handleData), recievedData);
        }