コード例 #1
0
    void Update()
    {   //Ready to process events
        m_Driver.ScheduleUpdate().Complete();

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                playersIds.RemoveAtSwapBack(i);
                --i;
            }
        }
        //Send data to clients
        SendDataToClients();

        // AcceptNewConnections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);

            // Check if there is another new connection
            c = m_Driver.Accept();
        }

        // Read Incoming Messages
        ReadIncomingMessages();
    }
コード例 #2
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                playersManager.RemovePlayer(m_Connections[i].InternalId);
                --i;
            }
        }

        // AcceptNewConnections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);

            // Check if there is another new connection
            c = m_Driver.Accept();
        }

        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, m_Connections[i].InternalId);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }

        if (playersManager.IsDirtyFlag)
        {
            for (int i = 0; i < m_Connections.Length; i++)
            {
                ServerUpdateMsg m = new ServerUpdateMsg(playersManager.GetPlayers());
                SendToClient(JsonUtility.ToJson(m), m_Connections[i]);
            }
            playersManager.ClearDirtyFlag();
        }
    }
コード例 #3
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        // AcceptNewConnections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);

            // Check if there is another new connection
            c = m_Driver.Accept();
        }

        //remove Inactive players
        for (int i = 0; i < m_Players.Count; i++)
        {
            if (Time.time - m_Players[i].timeOfLastMsg > 5.0f)
            {
                OnDisconnect(i);
            }
        }

        // Read Incoming Messages
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
    }
コード例 #4
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();


        // AcceptNewConnections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);

            // Check if there is another new connection
            c = m_Driver.Accept();
        }

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        GameLoop();

        // Read Incoming Messages
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
            //Debug.Log("Players Time: " + ConnectedList.GameState[i].pulse + " Server Time: " + DateTime.Now +" Player Time In Seconds: " + ConnectedList.GameState[i].pulse.Second);
            //Debug.Log("Time Difference: " + (DateTime.Now - ConnectedList.GameState[i].pulse) + " Time Span Amount: " + TimeSpan.FromSeconds(5));
            if ((DateTime.Now - ConnectedList.GameState[i].pulse) > TimeSpan.FromSeconds(5))
            {
                OnDisconnect(i);
            }
        }
    }
コード例 #5
0
    private void AcceptNewConnections()
    {
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))   //As long as it's not a default network connection
        {
            OnConnect(c);

            // Check if there is another new connection. If none, c will return a default connection and end the loop
            c = m_Driver.Accept();
        }
    }
コード例 #6
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete(); // Tell the driver we are ready to listen for next event

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        // AcceptNewConnections // Check if there is any new connection
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c); // Take the connection that just happeened and send to OnConnect

            c = m_Driver.Accept();
        }


        // Read Incoming Messages
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated); // Make sure a connection is valid (Should not be a default connection)

            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty) // As long as the event type is not empty
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
    }
コード例 #7
0
        public void CreateAndConnect_NetworkConnection_ToRemoteEndPoint()
        {
            var connection = Driver.Connect(RemoteDriver.LocalEndPoint());

            Assert.That(connection.IsCreated);
            Driver.ScheduleUpdate().Complete();

            RemoteDriver.ScheduleUpdate().Complete();
            Assert.That(RemoteDriver.Accept().IsCreated);

            Driver.ScheduleUpdate().Complete();
            DataStreamReader reader;

            Assert.That(connection.PopEvent(Driver, out reader) == NetworkEvent.Type.Connect);
        }
コード例 #8
0
        public void Accept()
        {
            Driver.Bind(NetworkEndPoint.LoopbackIpv4);
            Driver.Listen();
            Assert.That(Driver.Listening);

            // create connection to test to connect.
            /*var remote =*/ RemoteDriver.Connect(Driver.LocalEndPoint());

            NetworkConnection id;
            DataStreamReader  reader;
            const int         maximumIterations = 10;
            int  count     = 0;
            bool connected = false;

            while (count++ < maximumIterations)
            {
                // Clear pending events
                Driver.PopEvent(out id, out reader);
                RemoteDriver.PopEvent(out id, out reader);

                Driver.ScheduleUpdate().Complete();
                RemoteDriver.ScheduleUpdate().Complete();
                var connection = Driver.Accept();
                if (connection != default(NetworkConnection))
                {
                    connected = true;
                }
            }

            Assert.That(connected);
        }
コード例 #9
0
        public void SetupServerAndClientAndConnectThem(int bufferSize)
        {
            //setup server
            server_driver = NetworkDriver.Create(new NetworkDataStreamParameter {
                size = bufferSize
            });
            NetworkEndPoint server_endpoint = NetworkEndPoint.LoopbackIpv4;

            server_endpoint.Port = 1337;
            server_driver.Bind(server_endpoint);
            server_driver.Listen();

            //setup client
            client_driver = NetworkDriver.Create(new NetworkDataStreamParameter {
                size = bufferSize
            });
            clientToServerConnection = client_driver.Connect(server_endpoint);

            //update drivers
            client_driver.ScheduleUpdate().Complete();
            server_driver.ScheduleUpdate().Complete();

            //accept connection
            connectionToClient = server_driver.Accept();

            server_driver.ScheduleUpdate().Complete();
            ev = server_driver.PopEventForConnection(connectionToClient, out stream);
            Assert.IsTrue(ev == NetworkEvent.Type.Empty, "Not empty NetworkEvent on the server appeared");

            client_driver.ScheduleUpdate().Complete();
            ev = clientToServerConnection.PopEvent(client_driver, out stream);
            Assert.IsTrue(ev == NetworkEvent.Type.Connect, "NetworkEvent should have Type.Connect on the client");
        }
コード例 #10
0
        public void Execute()
        {
            // Remove connections which have been destroyed from the list of active connections
            for (int i = 0; i < connections.Length; ++i)
            {
                if (!connections[i].IsCreated)
                {
                    connections.RemoveAtSwapBack(i);
                    // Index i is a new connection since we did a swap back, check it again
                    --i;
                }
            }

            // Accept all new connections
            while (true)
            {
                var con = driver.Accept();
                // "Nothing more to accept" is signaled by returning an invalid connection from accept
                if (!con.IsCreated)
                {
                    break;
                }
                connections.Add(con);
            }
        }
コード例 #11
0
    public void Execute()
    {
        // Clean up connections
        for (int i = 0; i < Connections.Length; i++)
        {
            if (!Connections[i].IsCreated)
            {
                Connections.RemoveAtSwapBack(i);
                --i;
            }
        }
        // Accept new connections
        NetworkConnection c;

        while ((c = Driver.Accept()) != default(NetworkConnection))
        {
            Connections.Add(c);
            var d = new RawNetworkMessage()
            {
                Length = 0, Type = (uint)MLAPINetworkEvent.Connect, Id = c.InternalId
            };
            PacketData.Enqueue(d);
            Debug.Log("Accepted a connection");
        }
    }
コード例 #12
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        //connection cleanup
        for (int i = 0; i < m_Connections.Length; ++i)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        //accepting new connections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);
            c = m_Driver.Accept();
        }

        //Incoming messages reading
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; ++i)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type eventType;
            eventType = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (eventType != NetworkEvent.Type.Empty)
            {
                if (eventType == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (eventType == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }
                eventType = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
    }
コード例 #13
0
 public void Tick()
 {
     //NetworkConnection connection;
     while ((/*connection =*/ m_Driver.Accept()) != default(NetworkConnection))
     {
         //m_Connections.Add(connection);
     }
 }
コード例 #14
0
    public virtual void Update()
    {
        //FORCE THE NETDRIVER TO USE THE NORMAL UPDATE LOOP
        m_Driver.ScheduleUpdate().Complete();

        //CLEAN UP CONNECTIONS
        //if not made
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        //ACCEPT NEW CONNECTIONS
        NetworkConnection c;

        //asks for any new connection
        while ((c = m_Driver.Accept()) != default)
        {
            //if it's not null
            //ADD THE NEW CONECTION
            m_Connections.Add(c);
        }

        //START CHECKING FOR EVENT WHICH MIGHT HAVE HAPPENED
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            //if the connection is not created
            if (!m_Connections[i].IsCreated)
            {
                continue; //skip it
            }
            //if we have a connection
            //check for an event
            NetworkEvent.Type cmd;
            while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
            {
                //start filtering events
                //DATA
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnDataMessage(stream, m_Connections[i]);
                }
                //DISCONNECT
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log(LogPrefix + "Client asked to disconnect");
                    m_Connections[i] = default;
                }
            }
        }
    }
コード例 #15
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);
            c = m_Driver.Accept();
        }

        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);
            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i, m_Connections[i]);

                    heartbeat[m_Connections[i].InternalId.ToString()] = Time.time;
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
        AssertHeartbeatTime();
    }
コード例 #16
0
ファイル: ServerController.cs プロジェクト: LeoVC1/Unity-UDP
    void AcceptConnections()
    {
        NetworkConnection connection;

        while ((connection = m_Driver.Accept()) != default(NetworkConnection)) // Check new connection
        {
            m_Connections.Add(connection);                                     //Aceppt new connection
            Debug.Log("Conexao Criada!");
        }
    }
コード例 #17
0
    void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        // CleanUpConnections
        CleanClients();

        // AcceptNewConnections
        NetworkConnection c = m_Driver.Accept();

        while (c != default(NetworkConnection))
        {
            OnConnect(c);

            // Check if there is another new connection
            c = m_Driver.Accept();
        }


        // Read Incoming Messages
        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type cmd;
            cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            while (cmd != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    OnDisconnect(i);
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
    }
コード例 #18
0
    private void Update()
    {
        driver.ScheduleUpdate().Complete();

        //Clean up old connections
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated)
            {
                connections.RemoveAtSwapBack(i);
                --i; //Go back because we removed an index
            }
        }

        //Accept new connection - returns an up-to-date connection list
        NetworkConnection connection;

        while ((connection = driver.Accept()) != default(NetworkConnection))
        {
            connections.Add(connection);
            Debug.Log("Accepted a connection");

            ServerSend.Welcome(connection, connection.InternalId, availableHexColors[connection.InternalId].ColorToUInt());
        }


        //Start querying driver events that might have happened since the last update
        DataStreamReader reader;

        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated)
            {
                continue;
            }

            NetworkEvent.Type cmd;
            while ((cmd = driver.PopEventForConnection(connections[i], out reader)) != NetworkEvent.Type.Empty)
            {
                //Process events
                if (cmd == NetworkEvent.Type.Data)
                {
                    packetHandlers[reader.ReadUShort()](reader, connections[i]);

                    //Handle disconnections
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Client disconnected from server");
                    ServerSend.PlayerLeft(connections[i].InternalId);
                    connections[i] = default(NetworkConnection);
                }
            }
        }
    }
コード例 #19
0
        private void AcceptNewConnections()
        {
            NetworkConnection c;

            while ((c = driver.Accept()) != default(NetworkConnection))
            {
                connections.Add(c);
                NewClientConnect(c);
                //Logs.Print($"New connection arrived from client");
            }
        }
コード例 #20
0
        public void NetworkPipeline_Fragmentation_SendRecvOnce()
        {
            var clientPipe = m_ClientDriver.CreatePipeline(typeof(FragmentationPipelineStage));
            var serverPipe = m_ServerDriver.CreatePipeline(typeof(FragmentationPipelineStage));

            // Connect to server
            var clientToServer = m_ClientDriver.Connect(m_ServerDriver.LocalEndPoint());

            Assert.AreNotEqual(default(NetworkConnection), clientToServer);
            m_ClientDriver.ScheduleUpdate().Complete();

            // Handle incoming connection from client
            m_ServerDriver.ScheduleUpdate().Complete();
            var serverToClient = m_ServerDriver.Accept();

            Assert.AreNotEqual(default(NetworkConnection), serverToClient);

            // Send message to client
            if (m_ServerDriver.BeginSend(serverPipe, serverToClient, out var strm) == 0)
            {
                strm.WriteInt(42);
                m_ServerDriver.EndSend(strm);
            }
            m_ServerDriver.ScheduleUpdate().Complete();

            // Receive incoming message from server
            m_ClientDriver.ScheduleUpdate().Complete();
            DataStreamReader readStrm;

            Assert.AreEqual(NetworkEvent.Type.Connect, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(NetworkEvent.Type.Data, clientToServer.PopEvent(m_ClientDriver, out readStrm));
            Assert.AreEqual(sizeof(int), readStrm.Length);
            Assert.AreEqual(42, readStrm.ReadInt());
        }
コード例 #21
0
    void FixedUpdate()
    {
        localDriver.ScheduleUpdate().Complete();

        // CleanUpConnections
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated)
            {
                connections.RemoveAtSwapBack(i);
                --i;
            }
        }
        // AcceptNewConnections
        NetworkConnection c;

        while ((c = localDriver.Accept()) != default(NetworkConnection))
        {
            connections.Add(c);
            Debug.Log("Accepted a connection");
        }

        DataStreamReader stream;

        for (int i = 0; i < connections.Length; i++)
        {
            Assert.IsTrue(connections[i].IsCreated);

            NetworkEvent.Type cmd;
            while ((cmd = localDriver.PopEventForConnection(connections[i], out stream)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    var json   = stream.ReadString();
                    var writer = localDriver.BeginSend(NetworkPipeline.Null, connections[i]);
                    writer.WriteString(json);
                    localDriver.EndSend(writer);
                    //TODO for test
                    //if (Random.Range(0, 99) % 9 == 0)
                    //{
                    //    localDriver.Disconnect(connections[i]);
                    //}
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Client disconnected from server");
                    connections[i] = default(NetworkConnection);
                }
            }
        }
    }
コード例 #22
0
    void ServerUpdate()
    {
        // Update the NetworkDriver. It schedules a job so we must wait for that job with Complete
        m_ServerDriver.ScheduleUpdate().Complete();

        // Accept all new connections
        while (true)
        {
            var con = m_ServerDriver.Accept();
            // "Nothing more to accept" is signaled by returning an invalid connection from accept
            if (!con.IsCreated)
            {
                break;
            }
            m_serverConnections.Add(con);
        }

        for (int i = 0; i < m_serverConnections.Length; ++i)
        {
            DataStreamReader  strm;
            NetworkEvent.Type cmd;
            // Pop all events for the connection
            while ((cmd = m_ServerDriver.PopEventForConnection(m_serverConnections[i], out strm)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    // For ping requests we reply with a pong message
                    int id = strm.ReadInt();
                    // Create a temporary DataStreamWriter to keep our serialized pong message
                    if (m_ServerDriver.BeginSend(m_serverConnections[i], out var pongData) == 0)
                    {
                        pongData.WriteInt(id);
                        // Send the pong message with the same id as the ping
                        m_ServerDriver.EndSend(pongData);
                    }
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    // This connection no longer exist, remove it from the list
                    // The next iteration will operate on the new connection we swapped in so as long as it exist the
                    // loop can continue
                    m_serverConnections.RemoveAtSwapBack(i);
                    if (i >= m_serverConnections.Length)
                    {
                        break;
                    }
                }
            }
        }
    }
    public void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        // CleanUpConnections
        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                m_Connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        // AcceptNewConnections
        NetworkConnection c;

        while ((c = m_Driver.Accept()) != default(NetworkConnection))
        {
            m_Connections.Add(c);
            Debug.Log("Accepted a connection");
        }

        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            Assert.IsTrue(m_Connections[i].IsCreated);

            NetworkEvent.Type cmd;
            while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Data)
                {
                    var str = "";
                    while (stream.Length > stream.GetBytesRead())
                    {
                        str += stream.ReadString();
                    }

                    ProcessMessage(str);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Client disconnected from server");
                    m_Connections[i] = default(NetworkConnection);
                }
            }
        }
    }
コード例 #24
0
            public void Execute()
            {
                NetworkConnection con;

                while ((con = driver.Accept()) != default(NetworkConnection))
                {
                    // New connection can never have any events, if this one does - just close it
                    DataStreamReader reader;
                    if (con.PopEvent(driver, out reader) != NetworkEvent.Type.Empty)
                    {
                        con.Disconnect(driver);
                        continue;
                    }

                    // create an entity for the new connection
                    var ent = commandBuffer.CreateEntity();
                    commandBuffer.AddComponent(ent, new NetworkStreamConnection {
                        Value = con
                    });
                    commandBuffer.AddComponent(ent, new NetworkSnapshotAckComponent());
                    commandBuffer.AddComponent(ent, new CommandTargetComponent());
                    commandBuffer.AddBuffer <IncomingRpcDataStreamBufferComponent>(ent);
                    var rpcBuffer = commandBuffer.AddBuffer <OutgoingRpcDataStreamBufferComponent>(ent);
                    commandBuffer.AddBuffer <IncomingCommandDataStreamBufferComponent>(ent);
                    commandBuffer.AddBuffer <IncomingSnapshotDataStreamBufferComponent>(ent);

                    RpcSystem.SendProtocolVersion(rpcBuffer, protocolVersion);

                    // Send RPC - assign network id
                    int nid;
                    if (!freeNetworkIds.TryDequeue(out nid))
                    {
                        // Avoid using 0
                        nid             = numNetworkId[0] + 1;
                        numNetworkId[0] = nid;
                    }

                    commandBuffer.AddComponent(ent, new NetworkIdComponent {
                        Value = nid
                    });
                    rpcQueue.Schedule(rpcBuffer, new RpcSetNetworkId
                    {
                        nid         = nid,
                        netTickRate = tickRate.NetworkTickRate,
                        simMaxSteps = tickRate.MaxSimulationStepsPerFrame,
                        simTickRate = tickRate.SimulationTickRate
                    });
                }
            }
コード例 #25
0
        public void SetupServerAndClientAndConnectThem(int bufferSize)
        {
            //setup server
            server_driver = NetworkDriver.Create(new NetworkDataStreamParameter {
                size = bufferSize
            });
            NetworkEndPoint server_endpoint = NetworkEndPoint.LoopbackIpv4;

            server_endpoint.Port = 1337;
            var ret      = server_driver.Bind(server_endpoint);
            int maxRetry = 10;

            while (ret != 0 && --maxRetry > 0)
            {
                server_endpoint.Port += 17;
                ret = server_driver.Bind(server_endpoint);
            }
            ;
            Assert.AreEqual(ret, 0, "Failed to bind the socket");
            server_driver.Listen();

            //setup client
            client_driver = NetworkDriver.Create(new NetworkDataStreamParameter {
                size = bufferSize
            });
            clientToServerConnection = client_driver.Connect(server_endpoint);

            //update drivers
            client_driver.ScheduleUpdate().Complete();

            for (int i = 0; i < 10; ++i)
            {
                server_driver.ScheduleUpdate().Complete();
                //accept connection
                connectionToClient = server_driver.Accept();
                if (connectionToClient.IsCreated)
                {
                    break;
                }
            }
            Assert.IsTrue(connectionToClient.IsCreated, "Failed to accept the connection");

            ev = PollDriverAndFindDataEvent(ref server_driver, connectionToClient, NetworkEvent.Type.Empty);
            Assert.IsTrue(ev == NetworkEvent.Type.Empty, $"Not empty NetworkEvent on the server appeared, got {ev} using {backend}");

            ev = PollDriverAndFindDataEvent(ref client_driver, clientToServerConnection, NetworkEvent.Type.Connect);
            Assert.IsTrue(ev == NetworkEvent.Type.Connect, $"NetworkEvent should have Type.Connect on the client, but got {ev} using {backend}");
        }
コード例 #26
0
 public void Execute()
 {
     // Accept all connections and create entities for the new connections using an entity command buffer
     while (true)
     {
         var con = driver.Accept();
         if (!con.IsCreated)
         {
             break;
         }
         var ent = commandBuffer.CreateEntity();
         commandBuffer.AddComponent(ent, new PingServerConnectionComponentData {
             connection = con
         });
     }
 }
コード例 #27
0
ファイル: Server.cs プロジェクト: Casey-Hofland/KernmoduleIV
    private void UpdateConnections()
    {
        // Clean up Connections.
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated)
            {
                playerInfoByID.Remove(connections[i].InternalId);
                connections.RemoveAt(i);
                i--;
            }
        }

        // Accept new Connections.
        NetworkConnection connection;

        while ((connection = driver.Accept()) != default)
        {
            if (connectionsLength >= maxConnections)
            {
                driver.Disconnect(connection);
            }
            else
            {
                // Send PlayerMessages and a WelcomeMessage to the new connection.
                var playerInfo = new PlayerInfo
                {
                    color = Random.ColorHSV(),
                    name  = "",
                };
                playerInfoByID.Add(connection.InternalId, playerInfo);

                for (int i = 0; i < connectionsLength; i++)
                {
                    var otherConnection  = connections[i];
                    var otherPlayerInfo  = playerInfoByID[otherConnection.InternalId];
                    var newPlayerMessage = new NewPlayerMessage(MessageID.nextID, otherConnection.InternalId, otherPlayerInfo.color, otherPlayerInfo._name);
                    Send(connection, newPlayerMessage);
                }

                var welcomeMessage = new WelcomeMessage(MessageID.nextID, connection.InternalId, playerInfo.color);
                Send(connection, welcomeMessage);

                connections.AddNoResize(connection);
            }
        }
    }
コード例 #28
0
        public void Execute()
        {
            // Clean up connections
            for (int i = 0; i < connections.Length; i++)
            {
                if (!connections[i].IsCreated)
                {
                    connections.RemoveAtSwapBack(i);
                    --i;
                }
            }
            // Accept new connections
            NetworkConnection c;

            while ((c = driver.Accept()) != default(NetworkConnection))
            {
                connections.Add(c);
            }
        }
コード例 #29
0
    private void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        // CleanUp the Connections
        CleanupClients();

        // Accept hte New Connections
        NetworkConnection c;

        while ((c = m_Driver.Accept()) != default(NetworkConnection))
        {
            OnConnect(c);
        }

        DataStreamReader stream;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections[i].IsCreated)
            {
                Assert.IsTrue(true);
            }

            NetworkEvent.Type cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);

            while (cmd != NetworkEvent.Type.Empty)
            {
                //Receiving Data from Client
                if (cmd == NetworkEvent.Type.Data)
                {
                    OnData(stream, i);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    m_Connections[i] = default(NetworkConnection);
                    continue;
                }

                cmd = m_Driver.PopEventForConnection(m_Connections[i], out stream);
            }
        }
    }
コード例 #30
0
ファイル: SoakServerJobs.cs プロジェクト: za10222/multiplayer
    public void Execute()
    {
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].Connection.IsCreated)
            {
                connections.RemoveAtSwapBack(i);
                --i;
            }
        }

        NetworkConnection c;

        while ((c = driver.Accept()) != default(NetworkConnection))
        {
            connections.Add(new SoakClientCtx {
                Connection = c, NextSequenceId = now
            });
        }
    }