void Start()
    {
        m_Driver     = new UdpCNetworkDriver(new INetworkParameter[0]);
        m_Connection = default(NetworkConnection);

        var endpoint = new IPEndPoint(IPAddress.Loopback, 9000);

        m_Connection = m_Driver.Connect(endpoint);
    }
Example #2
0
    private void Start()
    {
        Driver     = new UdpCNetworkDriver(new INetworkParameter[0]);
        Connection = default;

        NetworkEndPoint endpoint = NetworkEndPoint.Parse("127.0.0.1", 9000);

        Connection = Driver.Connect(endpoint);
    }
Example #3
0
    void FixedUpdate()
    {
        // Update the ping client UI with the ping statistics computed by teh job scheduled previous frame since that
        // is now guaranteed to have completed
        PingClientUIBehaviour.UpdateStats(m_numPingsSent, m_lastPingTime);

        // Update the NetworkDriver. It schedules a job so we must wait for that job with Complete
        m_ClientDriver.ScheduleUpdate().Complete();

        // If the client ui indicates we should be sending pings but we do not have an active connection we create one
        if (PingClientUIBehaviour.ServerEndPoint.IsValid && !m_clientToServerConnection.IsCreated)
        {
            m_clientToServerConnection = m_ClientDriver.Connect(PingClientUIBehaviour.ServerEndPoint);
        }
        // If the client ui indicates we should not be sending pings but we do have a connection we close that connection
        if (!PingClientUIBehaviour.ServerEndPoint.IsValid && m_clientToServerConnection.IsCreated)
        {
            m_clientToServerConnection.Disconnect(m_ClientDriver);
            m_clientToServerConnection = default(NetworkConnection);
        }

        DataStreamReader strm;

        NetworkEvent.Type cmd;
        // Process all events on the connection. If the connection is invalid it will return Empty immediately
        while ((cmd = m_clientToServerConnection.PopEvent(m_ClientDriver, out strm)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Connect)
            {
                // When we get the connect message we can start sending data to the server
                // Set the ping id to a sequence number for the new ping we are about to send
                m_pendingPing = new PendingPing {
                    id = m_numPingsSent, time = Time.fixedTime
                };
                // Create a 4 byte data stream which we can store our ping sequence number in
                var pingData = new DataStreamWriter(4, Allocator.Temp);
                pingData.Write(m_numPingsSent);
                m_clientToServerConnection.Send(m_ClientDriver, pingData);
                pingData.Dispose();
                // Update the number of sent pings
                ++m_numPingsSent;
            }
            else if (cmd == NetworkEvent.Type.Data)
            {
                // When the pong message is received we calculate the ping time and disconnect
                m_lastPingTime = (int)((Time.fixedTime - m_pendingPing.time) * 1000);
                m_clientToServerConnection.Disconnect(m_ClientDriver);
                m_clientToServerConnection = default(NetworkConnection);
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                // If the server disconnected us we clear out connection
                m_clientToServerConnection = default(NetworkConnection);
            }
        }
    }
Example #4
0
    private void Update()
    {
        Driver.ScheduleUpdate().Complete();

        if (!Connection.IsCreated)
        {
            return;
        }

        NetworkEvent.Type cmd;

        while ((cmd = Connection.PopEvent(Driver, out DataStreamReader stream)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Connect)
            {
                Debug.Log("We are now connected to the server");

                //using (DataStreamWriter writer = new DataStreamWriter(4, Allocator.Temp)) {
                //    Connection.Send(Driver, writer);
                //}
            }
            else if (cmd == NetworkEvent.Type.Data)
            {
                DataStreamReader.Context context = default;
                float   x     = stream.ReadFloat(ref context);
                float   y     = stream.ReadFloat(ref context);
                float   z     = stream.ReadFloat(ref context);
                Vector3 point = new Vector3(x, y, z);
                if (Physics.Raycast(Camera.main.ScreenPointToRay(point), out RaycastHit hit, 1 << LayerMask.NameToLayer("Cell")))
                {
                    Cell cell = hit.collider.GetComponent <Cell>();
                    if (cell == null)
                    {
                        return;
                    }
                    cell.Reveal(Color.blue);
                }
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Client got disconnected from server");
                Connection = default;
            }
        }

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            using (DataStreamWriter writer = new DataStreamWriter(12, Allocator.Temp)) {
                writer.Write(Input.mousePosition.x);
                writer.Write(Input.mousePosition.y);
                writer.Write(Input.mousePosition.z);
                Driver.Send(NetworkPipeline.Null, Connection, writer);
            }
        }
    }
    // Update is called once per frame
    private void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        if (!m_Connection.IsCreated)
        {
            if (!Done)
            {
                Debug.Log("Something went wrong during connection");
            }

            return;
        }

        NetworkEvent.Type cmd;

        while ((cmd = m_Connection.PopEvent(m_Driver, out DataStreamReader stream)) != NetworkEvent.Type.Empty)
        {
            switch (cmd)
            {
            case NetworkEvent.Type.Connect:
                Debug.Log("We are now connected to the server.");
                var value = 1;

                using (var writer = new DataStreamWriter(4, Allocator.Temp))
                {
                    writer.Write(value);
                    m_Connection.Send(m_Driver, writer);
                }

                break;

            case NetworkEvent.Type.Data:
                var  readerCtx   = default(DataStreamReader.Context);
                uint streamValue = stream.ReadUInt(ref readerCtx);

                Debug.Log("Got the value = " + streamValue + " back from the server.");

                Done = true;
                m_Connection.Disconnect(m_Driver);
                m_Connection = default;

                break;

            case NetworkEvent.Type.Disconnect:
                Debug.Log("Client got disconnected from the server.");
                m_Connection = default;

                break;

            case NetworkEvent.Type.Empty:
                break;
            }
        }
    }
 //return -1 if not found
 int GetIndexForConnection(NetworkConnection c)
 {
     foreach (KeyValuePair <uint, NetworkConnection> pair in playerIndices)
     {
         if (c == pair.Value)
         {
             return((int)pair.Key);
         }
     }
     return(-1);
 }
    internal void BroadcastToClientsExcluding(NetworkConnection source, DataStreamWriter writer)
    {
        for (int i = 0; i < m_Connections.Length; ++i)
        {
            //skip broadcast for source client
            if (m_Connections[i] == source)
            {
                continue;
            }

            m_Connections[i].Send(m_Driver, writer);
        }
    }
    // Start is called before the first frame update
    private void Start()
    {
        m_Driver = new UdpNetworkDriver(new SimulatorUtility.Parameters {
            MaxPacketSize = 256, MaxPacketCount = 30, PacketDelayMs = 100
        });
        m_Pipeline   = m_Driver.CreatePipeline(typeof(UnreliableSequencedPipelineStage), typeof(SimulatorPipelineStage));
        m_Connection = default;

        m_Endpoint = new NetworkEndPoint();
        m_Endpoint = NetworkEndPoint.Parse("127.0.0.1", 9000);

        m_Connection = m_Driver.Connect(m_Endpoint);
    }
    internal uint AddNextAvailablePlayerIndex(NetworkConnection c)
    {
        uint i = 0;

        while (playerIndices.ContainsKey(i))
        {
            i++;
        }

        playerIndices[i] = c;

        //initialize PlayerData for this player
        playerData[i] = new PlayerData(this, i);

        return(i);
    }
    void Start()
    {
        t = transform;

        if (isLocalPlayer)
        {
            m_Driver     = new UdpCNetworkDriver(new INetworkParameter[0]);
            m_Connection = default(NetworkConnection);

            var endpoint = new IPEndPoint(SelectConnection.address, 9000);
            m_Connection = m_Driver.Connect(endpoint);

            inputState = new ClientInputState();

            repository = new ObjectRepository();
        }
    }
    internal void SendExistingPlayersTo(NetworkConnection source)
    {
        for (int i = 0; i < m_Connections.Length; ++i)
        {
            //skip message for source client
            if (m_Connections[i] == source)
            {
                continue;
            }

            int index = GetIndexForConnection(m_Connections[i]);
            if (index != -1)
            {
                using (var writer = new DataStreamWriter(8, Allocator.Temp)) {
                    writer.Write((uint)GameEvent.PLAYER_JOINED);
                    writer.Write((uint)index);
                    source.Send(m_Driver, writer);
                }
            }
        }
    }
 void HandleConnection()
 {
     if (!m_Connection.IsCreated)
     {
         if (!Done)
         {
             Debug.Log("Something went wrong during connect");
         }
         return;
     }
     else if (!connected)
     {
         if (Time.frameCount % 180 == 0)     //give it a few seconds...
         {
             Debug.Log("No connect event, retrying");
             //We haven't received a connected event, retry connection
             var endpoint = new IPEndPoint(IPAddress.Loopback, 9000);
             m_Connection = m_Driver.Connect(endpoint);
         }
     }
 }
Example #13
0
    static NetworkConnection ProcessSingleConnection(UdpCNetworkDriver.Concurrent driver, NetworkConnection connection)
    {
        DataStreamReader strm;

        NetworkEvent.Type cmd;
        // Pop all events for the connection
        while ((cmd = driver.PopEventForConnection(connection, out strm)) != NetworkEvent.Type.Empty)
        {
            if (cmd == NetworkEvent.Type.Data)
            {
                // For ping requests we reply with a pong message
                // A DataStreamReader.Context is required to keep track of current read position since
                // DataStreamReader is immutable
                var readerCtx = default(DataStreamReader.Context);
                int id        = strm.ReadInt(ref readerCtx);
                // Create a temporary DataStreamWriter to keep our serialized pong message
                var pongData = new DataStreamWriter(4, Allocator.Temp);
                pongData.Write(id);
                // Send the pong message with the same id as the ping
                driver.Send(connection, pongData);
                pongData.Dispose();
            }
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                // When disconnected we make sure the connection return false to IsCreated so the next frames
                // DriverUpdateJob will remove it
                return(default(NetworkConnection));
            }
        }

        return(connection);
    }
 void HandleDisconnectEvent(DataStreamReader stream)
 {
     Debug.Log("Client got disconnected from server");
     m_Connection = default(NetworkConnection);
 }
Example #15
0
 public bool Equals(NetworkConnection o)
 {
     return(this == o);
 }