/// <summary> /// This is a hook that is invoked when the client is started. /// </summary> /// <param name="client"></param> public virtual void OnStartClient(LiteNetLibClient client) { if (LogInfo) { Logging.Log(LogTag, "OnStartClient"); } }
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); }
/// <summary> /// This is a hook that is invoked when the client is started. /// </summary> /// <param name="client"></param> public virtual void OnStartClient(LiteNetLibClient client) { if (LogInfo) { Debug.Log("[" + name + "] LiteNetLibManager::OnStartClient"); } }
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; } }
public virtual LiteNetLibClient StartClient(string networkAddress, int networkPort) { if (Client != null) { return(Client); } this.networkAddress = networkAddress; this.networkPort = networkPort; if (LogDev) { Debug.Log("Client connecting to " + networkAddress + ":" + networkPort); } Client = new LiteNetLibClient(this); RegisterClientMessages(); Client.StartClient(networkAddress, networkPort); OnStartClient(Client); return(Client); }
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); } }