// note: original HLAPI HandleBytes function handled >1 message in a while loop, but this wasn't necessary // anymore because NetworkServer/NetworkClient Update both use while loops to handle >1 data events per // frame already. // -> in other words, we always receive 1 message per Receive call, never two. // -> can be tested easily with a 1000ms send delay and then logging amount received in while loops here // and in NetworkServer/Client Update. HandleBytes already takes exactly one. /// <summary> /// This function allows custom network connection classes to process data from the network before it is passed to the application. /// </summary> /// <param name="buffer">The data received.</param> internal void TransportReceive(ArraySegment <byte> buffer, int channelId) { // unpack message using (PooledNetworkReader networkReader = NetworkReaderPool.GetReader(buffer)) { try { int msgType = MessagePacker.UnpackId(networkReader); if (msgType == MessagePacker.GetId <NotifyPacket>()) { // this is a notify message, send to the notify receive NotifyPacket notifyPacket = networkReader.ReadNotifyPacket(); ReceiveNotify(notifyPacket, networkReader, channelId); } else { // try to invoke the handler for that message InvokeHandler(msgType, networkReader, channelId); } } catch (InvalidDataException ex) { logger.Log(ex.ToString()); } catch (Exception ex) { logger.LogError("Closed connection: " + this + ". Invalid message " + ex); Connection?.Disconnect(); } } }
/// <summary> /// Register a handler for a particular message type. /// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para> /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="handler">Function handler which will be invoked for when this message type is received.</param> /// <param name="requireAuthentication">True if the message requires an authenticated connection</param> public void RegisterHandler <T>(Action <INetworkPlayer, T> handler) { int msgType = MessagePacker.GetId <T>(); if (logger.filterLogType == LogType.Log && messageHandlers.ContainsKey(msgType)) { logger.Log("NetworkServer.RegisterHandler replacing " + msgType); } messageHandlers[msgType] = MessageHandler(handler); }
/// <summary> /// Register a handler for a particular message type. /// <para>There are several system message types which you can add handlers for. You can also add your own message types.</para> /// </summary> /// <typeparam name="T">Message type</typeparam> /// <param name="handler">Function handler which will be invoked for when this message type is received.</param> public void RegisterHandler <T>(MessageDelegateWithPlayer <T> handler) { int msgType = MessagePacker.GetId <T>(); if (logger.filterLogType == LogType.Log && messageHandlers.ContainsKey(msgType)) { logger.Log($"RegisterHandler replacing {msgType}"); } messageHandlers[msgType] = MessageWrapper(handler); }
/// <summary> /// Unregisters a handler for a particular message type. /// </summary> /// <typeparam name="T">Message type</typeparam> public void UnregisterHandler <T>() { int msgType = MessagePacker.GetId <T>(); messageHandlers.Remove(msgType); }