Esempio n. 1
0
    // Start
    void Start()
    {
        NetworkTransport.Init();

        reliableChannel   = config.AddChannel(QosType.Reliable);
        unreliableChannel = config.AddChannel(QosType.Unreliable);

        var hostTopology = new HostTopology(config, maxConnections);

        hostId = NetworkTransport.AddHost(hostTopology, port);

        receiveBuffer = new byte[bufferSize];

        onConnect += (connectionId) => {
            Debug.Log("Player " + connectionId + " connected.");

            var player = new NetworkTarget(connectionId);
            NetworkPlayers.Add(player);

            // Send connected message
            player.Send(ServerMessage.Connected);
        };

        onDisconnect += (connectionId) => {
            Debug.Log("Player " + connectionId + " disconnected.");
            NetworkPlayers.Remove(NetworkPlayers.Find(connectionId));
        };

        isStarted = true;
    }
Esempio n. 2
0
    // HandleMessage
    public void HandleMessage(byte[] buffer, int connectionId)
    {
        var reader = new NetworkReader(buffer);

        // Message type
        MessageType msgType = reader.ReadByte();

        NetworkCall call;
        bool        exists = functions.TryGetValue(msgType, out call);

        if (!exists)
        {
            Debug.LogError("Unknown message type: " + msgType);
            return;
        }

        var methodParams = call.method.GetParameters();
        var invokeParams = new object[methodParams.Length];

        for (var i = 0; i < methodParams.Length; i++)
        {
            var param     = methodParams[i];
            var paramType = param.ParameterType;

            if (paramType == typeof(NetworkTarget))
            {
                invokeParams[i] = NetworkPlayers.Find(connectionId);
                continue;
            }

            invokeParams[i] = Serialization.Read(reader, paramType);
        }

        // Call the method
        call.method.Invoke(call.obj, invokeParams);
    }