Example #1
0
            public static async Task <bool> SendNewPlayerRequest(string serverIP)
            {
                Server.Host = new Player(0, null, new IPEndPoint(IPAddress.Parse(serverIP), (int)NetworkConnection.Ports.ServerListener));

                NetworkConnection networkConnection = NetworkConnection.Current;

                using (networkConnection.GetUdp(NetworkConnection.Ports.ClientTalker))
                {
                    await networkConnection.Send("NewPlayerRequest", Server.Host);
                }

                using (networkConnection.GetUdp(NetworkConnection.Ports.ClientListener))
                {
                    Task <UdpReceiveResult> receiveTask = networkConnection.Receive();
                    var resultTask = await Task.WhenAny(receiveTask, Task.Delay(5000));

                    if (resultTask is Task <UdpReceiveResult> )
                    {
                        if (NetworkUtility.Decode((resultTask as Task <UdpReceiveResult>).Result.Buffer) == "NewPlayerReply")
                        {
                            Player = new Player(0, null, null);
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
Example #2
0
            public static async Task <bool> ListenNewPlayerRequest()
            {
                NetworkConnection networkConnection = NetworkConnection.Current;
                UdpReceiveResult  result;

                using (networkConnection.GetUdp(NetworkConnection.Ports.ServerListener))
                {
                    result = await networkConnection.Receive();
                }

                if (NetworkUtility.Decode(result.Buffer) == "NewPlayerRequest")
                {
                    GameSession.Current.Players.Add(new Player(GameSession.Current.Players.Count, result.RemoteEndPoint, new IPEndPoint(result.RemoteEndPoint.Address, (int)NetworkConnection.Ports.ClientListener)));
                    await Task.Delay(TimeSpan.FromMilliseconds(50));

                    using (networkConnection.GetUdp(NetworkConnection.Ports.ServerTalker))
                    {
                        await networkConnection.Send("NewPlayerReply", GameSession.Current.Players[GameSession.Current.Players.Count - 1]);
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
Example #3
0
            private static void UpdateClientData(UdpReceiveResult result)
            {
                object clientData = NetworkUtility.Deserialize(result.Buffer);

                if (clientData is ClientData)
                {
                    Data = clientData as ClientData;
                }
            }
Example #4
0
            private static void UpdateUserInput(UdpReceiveResult result)
            {
                Player player  = GameSession.Current.Players.Find(p => p.IPFrom != null && p.IPFrom.Port == result.RemoteEndPoint.Port);
                object actions = NetworkUtility.Deserialize(result.Buffer);

                if (actions is UserActions)
                {
                    player.Actions = NetworkUtility.Deserialize(result.Buffer) as UserActions;
                }
            }
Example #5
0
            public static async Task <bool> ListenStartGameCommand()
            {
                NetworkConnection networkConnection = NetworkConnection.Current;
                UdpReceiveResult  result;

                using (networkConnection.GetUdp(NetworkConnection.Ports.ClientListener))
                {
                    result = await networkConnection.Receive();
                }

                if (NetworkUtility.Decode(result.Buffer) == "StartGameCommand")
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
 internal async Task Send(object data, List <Player> players)
 {
     await Send(NetworkUtility.Serialize(data), players);
 }
 internal async Task Send(object data, Player player)
 {
     await Send(NetworkUtility.Serialize(data), player);
 }
 internal async Task Send(string data, List <Player> players)
 {
     await Send(NetworkUtility.Encode(data), players);
 }
 internal async Task Send(string data, Player player)
 {
     await Send(NetworkUtility.Encode(data), player);
 }