Exemplo n.º 1
0
    protected override void Execute(List <NetworkEntity> entities)
    {
        var connDict = new Dictionary <IPEndPoint, ServerConnectionComponent>();

        foreach (var sg in serverConnectionGroup.GetEntities())
        {
            var comp = sg.serverConnection;
            connDict.Add(comp.clientEP, comp);
        }

        var connInfo = connInfoGroup.GetSingleEntity().serverConnectionInfo;

        foreach (var entity in entities)
        {
            var comp = entity.recvPacket;
            if (connDict.ContainsKey(comp.source))
            {
                continue;
            }

            ServerConnectionComponent serverConn;
            connDict.TryGetValue(comp.source, out serverConn);

            // Start new ServerConnection
            if (serverConn == null)
            {
                serverConn                = new ServerConnectionComponent();
                serverConn.clientEP       = comp.source;
                serverConn.playerMetadata = new PlayerMetadata();

                using (var reader = new BinaryReader(
                           new MemoryStream(comp.packet.data))) {
                    serverConn.playerMetadata.ReadBytes(reader);
                }

                ctx.CreateEntity()
                .AddComponent(NetworkComponentsLookup.ServerConnection, serverConn);

                var e = ctx.CreateEntity();
                e.AddConnectionStart(serverConn);
                e.isCleanup = true;
            }

            var playerID = connInfo.currentPlayerID++;

            // Send SyncAck packet to client
            ctx.CreateEntity()
            .AddComponent(NetworkComponentsLookup.SendPacket, new SendPacketComponent {
                target = comp.source,
                packet = new Packet(PacketType.SyncAck, BitConverter.GetBytes(playerID))
            });
        }
    }
Exemplo n.º 2
0
    public void Initialize()
    {
        var ent = ctx.CreateEntity();

        ent.AddServerConnectionInfo(0);
        connectionInfo = ent.serverConnectionInfo;
    }
    public void Execute()
    {
        foreach (var ent in groupPings.GetEntities())
        {
            var data = ent.pingTest;

            data.cooldown -= Time.deltaTime;
            if (data.cooldown <= 0)
            {
                data.cooldown = data.testIntervalMsec / 1000.0f;

                ctx.CreateEntity()
                .AddSendPacket(data.target, new Packet(PacketType.Ping, new byte[128]));
                Debug.Log("Ping Test to " + data.target);
            }
        }
    }
    public void Execute()
    {
        var framePacket = BuildFramePacket();

        byte[] packetData;
        using (var stream = new MemoryStream()) {
            var writer = new BinaryWriter(stream);
            framePacket.Write(writer);
            packetData = stream.ToArray();
        }

        var packet = new Packet(PacketType.Frame, packetData);

        foreach (var connEntity in serverConnectionGroup.GetEntities())
        {
            var conn = connEntity.serverConnection;
            if (conn.state == ServerConnectionComponent.State.Connected)
            {
                networkCtx.CreateEntity()
                .AddSendPacket(conn.clientEP, packet);
            }
        }
    }
    public void Execute()
    {
        var entity = connectionGroup.GetSingleEntity();
        var conn   = entity.clientConnection;

        conn.packetUnreceivedTime += Time.deltaTime;
        conn.stateTime            += Time.deltaTime;
        conn.timeoutCounter       += Time.deltaTime;
        conn.keepAliveCounter     += Time.deltaTime;

        if (recvPacketGroup.count != 0)
        {
            conn.packetUnreceivedTime = 0;
        }

        var myPackets = recvPacketGroup.GetEntities()
                        .Select(e => e.recvPacket);

        switch (conn.state)
        {
        case State.Disconnected: {
            if (startConnectionGroup.count != 0)
            {
                var startConnection = startConnectionGroup.GetSingleEntity();
                var info            = startConnection.clientStartConnection;

                conn.serverEP       = info.serverEP;
                conn.playerMetadata = info.playerMetadata;
                conn.state          = State.Initalize;

                SendSyncPacket(conn);

                startConnection.Destroy();
            }
        } break;

        case State.Initalize: {
            // Resend Sync
            if (conn.timeoutCounter > HandshakeTimeout)
            {
                if (conn.retryTime > MaxInitializeRetry)
                {
                    conn.state = State.Disconnected;
                }
                else
                {
                    SendSyncPacket(conn);
                    ++conn.retryTime;
                }

                conn.timeoutCounter = 0;
            }

            // React to SyncAck packet
            var syncAckPacket = myPackets
                                .Where(e => e.packet.packetType == PacketType.SyncAck)
                                .ElementAtOrDefault(0);

            if (syncAckPacket != null)
            {
                conn.state = State.Connected;
                var playerID = BitConverter.ToUInt16(syncAckPacket.packet.data, 0);

                // Rewrite player ID
                conn.playerMetadata.playerID = playerID;

                // Send Ack
                SendAckPacket(conn, playerID);
            }
        } break;

        case State.Connected: {
            // Handle time-outs
            if (conn.packetUnreceivedTime > ConnectionTimeOut)
            {
                StartTearDown(conn);
            }

            // KeepAlive
            if (conn.keepAliveCounter > KeepAliveTimeOut)
            {
                conn.keepAliveCounter = 0;
                ctx.CreateEntity()
                .AddSendPacket(conn.serverEP, new Packet(PacketType.KeepAlive, new byte[0]));
            }

            // Handle Fin
            var finPackets = myPackets
                             .Where(recv => recv.packet.packetType == PacketType.Fin)
                             .Count();
            if (finPackets > 0)
            {
                ctx.CreateEntity()
                .AddSendPacket(conn.serverEP, new Packet(PacketType.FinAck, new byte[0]));
                conn.state = State.Disconnected;
            }
        } break;

        case State.TearDown: {
            // Resend Fin
            if (conn.timeoutCounter > TearDownTimeOut)
            {
                if (conn.retryTime > MaxTearDownRetry)
                {
                    conn.state = State.Disconnected;
                }
                else
                {
                    ++conn.retryTime;
                    conn.timeoutCounter = 0;
                    SendFinPacket(conn);
                }
            }

            // Handle FinAck
            var finAckPackets = myPackets.Where(recv => recv.packet.packetType == PacketType.FinAck).Count();

            if (finAckPackets > 0)
            {
                conn.state = State.Disconnected;
            }
        } break;
        }
    }