Beispiel #1
0
        //Recieving information from client
        private void OnReceiveHost(IAsyncResult ar)
        {
            try
            {
                Socket.EndReceive(ar);
                var data = Data;
                Data = new byte[16384];
                Socket.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnReceiveHost), null);

                var newData = new byte[data.Length - 1];
                Array.Copy(data, 1, newData, 0, data.Length - 1);
                switch (data[0])
                {
                case 2:
                    Player2 = Serializer.Deserialize(newData) as PlayerTurn;
                    Gm.ActualizeMap(Player2);
                    Gm.ActualizeCharacters();
                    CheckAndCalculateTurn();
                    break;

                case 3:
                    var message = Encoding.UTF8.GetString(newData);
                    Gm.ShowMessage(message);
                    break;

                default:
                    throw new SystemException("Got bad first byte");
                }
            }
            catch (SocketException)
            {
                Gm.EndOfGame();
            }
        }
        //Recieving information from host
        private void OnReceiveClient(IAsyncResult ar)
        {
            try
            {
                Socket.EndReceive(ar);
                var data = Data;
                Data = new byte[16384];
                Socket.BeginReceive(Data, 0, Data.Length, SocketFlags.None, new AsyncCallback(OnReceiveClient), null);

                var newData = new byte[data.Length - 1];
                Array.Copy(data, 1, newData, 0, data.Length - 1);
                switch (data[0])
                {
                case 0:
                    var results = Serializer.Deserialize(newData) as TurnResults;
                    Gm.ShowResultsFromGc(results);
                    break;

                case 1:
                    var map = Serializer.Deserialize(newData) as Map;
                    Gm.Map = map;
                    Gm.ActualizeCharacters();
                    break;

                case 3:
                    var message = Encoding.UTF8.GetString(newData);
                    Gm.ShowMessage(message);
                    break;

                default:
                    throw new SystemException("Got bad first byte");
                }
            }
            catch (SocketException)
            {
                Gm.EndOfGame();
            }
        }