Пример #1
0
    /**
     * Procesa todos los paquetes de cierto tipo en el thread principal, lo cual
     * permite que cada endpoint adquiera acceso al API de Unity.
     */
    protected void HandleAllRequest(PacketType type)
    {
        Queue <Packet> packets = input.ReadAll(type);

        foreach (Packet request in packets)
        {
            int    id       = request.Reset(6).GetInteger();
            Packet response = Dispatch(request.Reset());
            if (0 <= id)
            {
                outputs[id].Write(response);
            }
        }
    }
Пример #2
0
    /**
     * Bucle principal del cliente. Recibe paquetes de tipo ACK y SNAPSHOT,
     * exclusivamente.
     */
    public void FrameHandler()
    {
        // Procesar ACKs:
        Packet ackResponse = input.Read(PacketType.ACK);

        if (ackResponse != null)
        {
            Endpoint endpoint = ackResponse.Reset(2).GetEndpoint();
            int      sequence = ackResponse.GetInteger();
            ackResponse.Reset();
            //Debug.Log("ACK received for " + endpoint + ". Sequence: " + sequence);
            packets = new SortedDictionary <int, Packet>(packets
                                                         .Where(x => x.Key > sequence)
                                                         .ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
            timedPackets = new SortedDictionary <int, Packet>(timedPackets
                                                              .Where(x => x.Key > sequence)
                                                              .ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
            //Debug.Log("Packets Remaining: " + packets.Count);
            switch (endpoint)
            {
            case Endpoint.JOIN: {
                HandleJoin(ackResponse);
                break;
            }
            }
        }
        // Procesar SNAPSHOTs:
        Queue <Packet> snapshots = input.ReadAll(PacketType.SNAPSHOT);

        interpolator.AddPackets(snapshots).Update();
        // Predecir (si es necesario):
        if (config.usePrediction && player != null)
        {
            predictor.Validate();
        }
        // Procesar 'reliability':
        output.WriteAll(packets.Values);
        float curTime = Time.unscaledTime;

        if (curTime >= lastTime + timeout)
        {
            lastTime = curTime;
            output.WriteAll(timedPackets.Values);
        }
    }