Beispiel #1
0
        private void SetupNetwork()
        {
            netDefs = new NetDefinitions();
            server  = new AsteroidServer(netDefs, Resolution.ToVector2(), 11002);

            client = new NetworkClient(new UDPConnection(
                                           System.Net.IPAddress.Parse((server != null) ? "127.0.0.1" : "122.58.99.13"),
                                           11002, // Change this destination to 12002 to connect to a running NetProxy.
                                           (server != null) ? 11003 : 11002
                                           ));
            client.Connection.Compression = AsteroidServer.NetworkCompression;
            client.Connection.Stats.ByteAggregationPeriodMilliseconds = 100;

            // Note, when using NetProxy with this setup, the Open command should be:
            // open 12002 11003 12003 11002

            incomingPool = new IncomingSyncPool(netDefs, 0);
            outgoingPool = new OutgoingSyncPool(netDefs, 0);
            client.Attach(incomingPool);
            client.Attach(outgoingPool);
            controlVector = new PlayerControl()
            {
                Ready      = true,
                PlayerName = System.Environment.MachineName
            };
            outgoingPool.AddEntity(controlVector);

            client.SetState(NetworkClient.ConnectionState.Open);
        }
Beispiel #2
0
 public AirlockHostedClient(NetDefinitions netDefs, NetworkClient client, OutgoingSyncPool worldContent)
 {
     Network       = client;
     ReturnContent = new IncomingSyncPool(netDefs, (ushort)AirlockServer.SyncPoolID.ReturnContent);
     ClientContent = new OutgoingSyncPool(netDefs, (ushort)AirlockServer.SyncPoolID.ClientContent);
     ClientContent.LinkedPools.Add(worldContent);
     Network.Attach(ReturnContent);
     Network.Attach(ClientContent);
     Network.SetState(NetworkClient.ConnectionState.Open);
     ClientInfo = new ClientInfo();
     ClientContent.AddEntity(ClientInfo);
 }
Beispiel #3
0
        public static void AcceptSocketCallback(IAsyncResult ar)
        {
            try
            {
                Socket socket = Listener.EndAcceptSocket(ar);
                try
                {
                    if (socket != null)
                    {
                        socket.NoDelay             = true;
                        socket.LingerState.Enabled = false;
                        NetworkClient client = null;

                        (System.Windows.Application.Current).Dispatcher.Invoke((Action) delegate()
                        {
                            try
                            {
                                client = new NetworkClient(socket);
                            }
                            catch (Exception ex)
                            {
                                client = null;
                                Debug.WriteLine(ex.Message);
                                Debug.WriteLine(ex.StackTrace);
                            }
                        });

                        if (client != null)
                        {
                            Clients.Add(client);
                            OnClientAccepted(client);
                            client.Attach();
                        }
                    }
                }
                catch (Exception lex)
                {
                    Debug.WriteLine(lex.Message);
                    Debug.WriteLine(lex.StackTrace);
                }
                finally
                {
                    Listener.BeginAcceptSocket(AcceptSocketCallback, null);
                }
            }
            catch (Exception ex)
            {
                IsListening = false;
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }
        }
Beispiel #4
0
        public AirlockClient(IPAddress address, int destport, int srcport)
        {
            Network = new NetworkClient(new UDPConnection(address, destport, srcport));

            NetDefinitions netDefs = new NetDefinitions();

            netDefs.LoadEntityTypes();

            WorldContent  = new IncomingSyncPool(netDefs, (ushort)AirlockServer.SyncPoolID.WorldContent);
            ReturnContent = new OutgoingSyncPool(netDefs, (ushort)AirlockServer.SyncPoolID.ReturnContent);
            ClientContent = new IncomingSyncPool(netDefs, (ushort)AirlockServer.SyncPoolID.ClientContent);
            ClientContent.LinkedPools.Add(WorldContent);
            Network.Attach(WorldContent);
            Network.Attach(ReturnContent);
            Network.Attach(ClientContent);

            Network.SetState(NetworkClient.ConnectionState.Open);
            LocalPlayer = new LocalPlayer();
            ReturnContent.AddEntity(LocalPlayer);

            Inputs = new ClientInputs();

            Grid = new MapGrid();
        }
Beispiel #5
0
        public static void AcceptSocketCallback(IAsyncResult ar)
        {
            try
            {
                Socket socket = Listener.EndAcceptSocket(ar);
                try
                {
                    if (socket != null)
                    {
                        socket.NoDelay             = true;
                        socket.LingerState.Enabled = false;
                        NetworkClient client = null;

                        (System.Windows.Application.Current).Dispatcher.Invoke((Action) delegate()
                        {
                            try
                            {
                                client = new NetworkClient(socket, Guid.NewGuid().ToString());
                            }
                            catch (Exception ex)
                            {
                                client = null;
                                ex.Log();
                            }
                        });

                        if (client != null)
                        {
                            Clients.Add(client);
                            OnClientAccepted(client);
                            client.Attach();
                        }
                    }
                }
                catch (Exception)
                {
                }
                finally
                {
                    Listener.BeginAcceptSocket(AcceptSocketCallback, null);
                }
            }
            catch (Exception)
            {
                IsListening = false;
            }
        }
Beispiel #6
0
        public void Update()
        {
            if (LastTimestamp == 0)
            {
                LastTimestamp = NetTime.Seconds() - 0.016;
                foreach (Physical phys in Physicals)
                {
                    phys.RequestMotionUpdate();
                }
            }

            double seconds = NetTime.Seconds();
            float  delta   = (float)(seconds - LastTimestamp);

            LastTimestamp = seconds;

            NetworkConnection connection = Server.RecieveConnection();

            if (connection != null)
            {
                NetworkClient client = new NetworkClient(connection);
                client.SetState(NetworkClient.ConnectionState.Open);
                client.Attach(OutgoingPool);
                IncomingSyncPool incoming = new IncomingSyncPool(NetDefs, 0);
                client.Attach(incoming);

                RemoteClient newClient = new RemoteClient()
                {
                    Client   = client,
                    Incoming = incoming,
                    Player   = null
                };

                Clients.Add(newClient);
            }
            UpdateEntitites(delta);

            syncCounter += delta;
            if (syncCounter >= TransmitRate)
            {
                syncCounter -= TransmitRate;

                OutgoingPool.Synchronise();
            }

            foreach (RemoteClient client in Clients)
            {
                client.Client.Update();
                client.Incoming.Synchronise();

                foreach (SyncEvent evt in client.Incoming.Events)
                {
                    if (evt.Obj is PlayerRequest request)
                    {
                        switch (request.Request)
                        {
                        case PlayerRequest.RequestType.FireMissile:
                            client.Player?.FireMissile();
                            break;

                        case PlayerRequest.RequestType.FireMultiMissile:
                            client.Player?.FireMultiMissile();
                            break;
                        }
                    }
                    evt.Clear();
                }

                foreach (SyncHandle handle in client.Incoming.Handles)
                {
                    if (handle.Obj is PlayerControl control)
                    {
                        if (control.Ready)
                        {
                            if (client.Player == null)
                            {
                                client.Player = NewPlayer(RandomNiceColor());
                                AddEntity(client.Player);
                                client.PlayerName = control.PlayerName;
                                serverReport.Clients.Add(control.PlayerName);
                                serverReport.Ships.Add(client.Player);
                            }
                            client.Player.Control(control.Thrust, control.Torque, control.Firing);
                        }

                        break;
                    }
                }
            }

            for (int i = Clients.Count - 1; i >= 0; i--)
            {
                RemoteClient client = Clients[i];
                if (client.Client.State == NetworkClient.ConnectionState.Closed)
                {
                    Clients.RemoveAt(i);
                    client.Client.Destroy();
                    if (client.Player != null)
                    {
                        RemoveEntity(client.Player);
                    }

                    int k = serverReport.Ships.IndexOf(client.Player);
                    serverReport.Ships.RemoveAt(k);
                    serverReport.Clients.RemoveAt(k);
                }
            }
        }