protected virtual void Awake() { #if UNITY_WEBGL && !UNITY_EDITOR // Force to use websocket transport if it's running as webgl if (transportFactory == null || !transportFactory.CanUseWithWebGL) { transportFactory = gameObject.AddComponent <WebSocketTransportFactory>(); } #else if (useWebSocket) { if (transportFactory == null || !transportFactory.CanUseWithWebGL) { transportFactory = gameObject.AddComponent <WebSocketTransportFactory>(); } } else { if (transportFactory == null) { transportFactory = gameObject.AddComponent <LiteNetLibTransportFactory>(); } } #endif transport = TransportFactory.Build(); if (offlineTransport == null) { offlineTransport = new OfflineTransport(); } Client = new LiteNetLibClient(this); Server = new LiteNetLibServer(this); }
protected void InitTransportAndHandlers() { offlineTransport = new OfflineTransport(); Client = new LiteNetLibClient(this); Server = new LiteNetLibServer(this); RegisterMessages(); }
internal void SendUpdate(bool isInitial) { if (!CanSync()) { Logging.LogError(LogTag, "Error while set value, behaviour is empty"); return; } LiteNetLibGameManager manager = Manager; LiteNetLibServer server = manager.Server; LiteNetLibClient client = manager.Client; switch (syncMode) { case SyncMode.ServerToClients: TransportHandler.WritePacket(ServerWriter, isInitial ? GameMsgTypes.InitialSyncField : GameMsgTypes.UpdateSyncField); SerializeForSend(ServerWriter); foreach (long connectionId in manager.GetConnectionIds()) { if (Identity.HasSubscriberOrIsOwning(connectionId)) { server.SendMessage(connectionId, dataChannel, deliveryMethod, ServerWriter); } } break; case SyncMode.ServerToOwnerClient: if (manager.ContainsConnectionId(ConnectionId)) { TransportHandler.WritePacket(ServerWriter, isInitial ? GameMsgTypes.InitialSyncField : GameMsgTypes.UpdateSyncField); SerializeForSend(ServerWriter); server.SendMessage(ConnectionId, dataChannel, deliveryMethod, ServerWriter); } break; case SyncMode.ClientMulticast: if (IsOwnerClient) { TransportHandler.WritePacket(ClientWriter, isInitial ? GameMsgTypes.InitialSyncField : GameMsgTypes.UpdateSyncField); SerializeForSend(ClientWriter); // Client send data to server, then server send to other clients, it should be reliable-ordered client.SendMessage(clientDataChannel, clientDeliveryMethod, ClientWriter); } else if (IsServer) { TransportHandler.WritePacket(ServerWriter, isInitial ? GameMsgTypes.InitialSyncField : GameMsgTypes.UpdateSyncField); SerializeForSend(ServerWriter); foreach (long connectionId in manager.GetConnectionIds()) { if (Identity.HasSubscriberOrIsOwning(connectionId)) { server.SendMessage(connectionId, dataChannel, deliveryMethod, ServerWriter); } } } break; } }
internal void SendUpdate(bool isInitial, long connectionId) { if (!CanSync() || !IsServer) { return; } LiteNetLibGameManager manager = Manager; LiteNetLibServer server = manager.Server; TransportHandler.WritePacket(ServerWriter, isInitial ? GameMsgTypes.InitialSyncField : GameMsgTypes.UpdateSyncField); SerializeForSend(ServerWriter); if (manager.ContainsConnectionId(connectionId)) { server.SendMessage(connectionId, dataChannel, deliveryMethod, ServerWriter); } }
public virtual bool StartServer() { if (Server != null) { return(true); } Server = new LiteNetLibServer(this); RegisterServerMessages(); if (!Server.StartServer(networkPort, maxConnections)) { if (LogError) { Debug.LogError("[" + name + "] LiteNetLibManager::StartServer cannot start server at port: " + networkPort); } Server = null; return(false); } OnStartServer(); return(true); }
protected virtual bool StartServer(bool isOffline) { if (Server != null) { return(true); } Server = new LiteNetLibServer(this); RegisterServerMessages(); bool canStartServer = !isOffline?Server.StartServer(networkPort, maxConnections) : Server.StartServerOffline(); if (!canStartServer) { if (LogError) { Debug.LogError("[" + name + "] LiteNetLibManager::StartServer cannot start server at port: " + networkPort); } Server = null; return(false); } OnStartServer(); return(true); }
protected void SendCall(byte dataChannel, DeliveryMethod deliveryMethod, FunctionReceivers receivers, long targetConnectionId) { LiteNetLibGameManager manager = Manager; LiteNetLibServer server = manager.Server; LiteNetLibClient client = manager.Client; if (manager.IsServer) { switch (receivers) { case FunctionReceivers.Target: if (Identity.HasSubscriberOrIsOwning(targetConnectionId) && manager.ContainsConnectionId(targetConnectionId)) { // Prepare packet TransportHandler.WritePacket(ServerWriter, GameMsgTypes.CallFunction); SerializeForSend(ServerWriter); // Send function call message from server to target client by target connection Id server.SendMessage(targetConnectionId, dataChannel, deliveryMethod, ServerWriter); } break; case FunctionReceivers.All: // Send to all connections foreach (long connectionId in manager.GetConnectionIds()) { if (manager.ClientConnectionId == connectionId) { // This is host's networking oject, so hook callback immediately // Don't have to send message to the client, because it is currently run as both server and client HookCallback(); } else if (Identity.HasSubscriberOrIsOwning(connectionId)) { // Prepare packet TransportHandler.WritePacket(ServerWriter, GameMsgTypes.CallFunction); SerializeForSend(ServerWriter); // Send message to subscribing clients server.SendMessage(connectionId, dataChannel, deliveryMethod, ServerWriter); } } if (!manager.IsClientConnected) { // It's not a host(client+host), it's just a server // So hook callback immediately to do the function at server HookCallback(); } break; case FunctionReceivers.Server: // Call server function at server // So hook callback immediately to do the function at server HookCallback(); break; } } else if (manager.IsClientConnected) { // Prepare packet TransportHandler.WritePacket(ClientWriter, GameMsgTypes.CallFunction); SerializeForClientSend(ClientWriter, receivers, targetConnectionId); // Client send net function call to server // Then the server will hook callback or forward message to other clients client.SendMessage(dataChannel, deliveryMethod, ClientWriter); } }