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); }
public void Attach(IncomingSyncPool syncPool) { if (IncomingPools.ContainsKey(syncPool.PoolID)) { throw new NetcodeItemcountException(string.Format("An IncomingSyncPool with PoolID of {0} has already been attached to this NetworkClient", syncPool.PoolID)); } IncomingPools[syncPool.PoolID] = syncPool; }
public override void OnReception(NetworkClient client) { IncomingSyncPool destination = client.GetSyncPool(PoolID); if (destination != null) { long offset = client.Connection.Stats.NetTimeOffset; destination.UnpackRevisionDatagram(this, offset); } }
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); }
public override void OnReception(NetworkClient client) { IncomingSyncPool destination = client.GetSyncPool(PoolID); if (destination != null) { foreach (ushort entityID in EntityIDs) { if (destination.HandleExists(entityID)) { destination.RemoveEntity(entityID, Revision); } } } }
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(); }
public void Detach(IncomingSyncPool syncPool) { IncomingPools.Remove(syncPool.PoolID); }
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); } } }