private void Send(object data)
    {
        if (!m_isConnected || !m_Connection.IsCreated || !m_Driver.IsCreated)
        {
            Debug.LogError("NotReady this client");
            return;
        }

        if (data is string s)
        {
            Debug.Log("Signaling: Sending WS data: " + s);
            var writer = m_Driver.BeginSend(m_Connection);
            var array  = s.SubstringAtCount(NativeString64.MaxLength);
            foreach (var t in array)
            {
                writer.WriteString(t);
            }

            m_Driver.EndSend(writer);
        }
        else
        {
            string str = JsonUtility.ToJson(data);
            Debug.Log("Signaling: Sending WS data: " + str);
            var writer = m_Driver.BeginSend(m_Connection);
            var array  = str.SubstringAtCount(NativeString64.MaxLength);
            foreach (var t in array)
            {
                writer.WriteString(t);
            }

            m_Driver.EndSend(writer);
        }
    }
Ejemplo n.º 2
0
    public static void HelloReceived(NetworkConnection connection)
    {
        var writer = driver.BeginSend(NetworkPipeline.Null, connection);

        writer.WriteUInt((uint)ServerPackets.helloReceived);

        writer.WriteString("I (the server) welcome you!");

        driver.EndSend(writer);
    }
Ejemplo n.º 3
0
    public static void Login(string name)
    {
        var writer = Driver.BeginSend(Connection);

        byte[] buffer = new byte[1 + name.Length];
        buffer[0] = 1;

        Encoding.ASCII.GetBytes(name).CopyTo(buffer, 1);

        var array = new NativeArray <byte>(buffer, Allocator.Temp);

        writer.WriteBytes(array);
        Driver.EndSend(writer);
    }
Ejemplo n.º 4
0
    public void SendActionToClients(uint pAction)
    {
        for (int i = 0; i < connections.Length; i++)
        {
            // Skip a connection if it is stale.
            if (!connections[i].IsCreated)
            {
                continue;
            }

            var writer = Driver.BeginSend(connections[i]);
            writer.WriteUInt(pAction);
            Driver.EndSend(writer);
        }
    }
Ejemplo n.º 5
0
        public static void SendPing(NetworkDriver driver, NetworkConnection connection)
        {
            var writer = driver.BeginSend(connection);

            writer.WriteByte(0);
            driver.EndSend(writer);
        }
    public virtual void SendNetMessage(ref NetworkDriver _driver, NetworkConnection _connection, MessageBase _msg)
    {
        DataStreamWriter _writer = _driver.BeginSend(_connection);

        WriteMessage(ref _writer, _msg);
        _driver.EndSend(_writer);
    }
Ejemplo n.º 7
0
    void ReceiveMessages()
    {
        int total = 0;

        for (int i = 0; i < m_Connections.Length; i++)
        {
            if (!m_Connections.IsCreated) //Check connection state
            {
                continue;
            }

            NetworkEvent.Type cmd;
            while ((cmd = m_Driver.PopEventForConnection(m_Connections[i], out DataStreamReader stream)) != NetworkEvent.Type.Empty)
            {
                //Receiving data from client
                if (cmd == NetworkEvent.Type.Data)
                {
                    int num = stream.ReadInt();
                    Debug.Log("Numero recebido: " + num);

                    total += num;

                    var writer = m_Driver.BeginSend(NetworkPipeline.Null, m_Connections[i]);
                    writer.WriteInt(total);
                    m_Driver.EndSend(writer);
                }
                //Check if the message of client is a disconnection
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    Debug.Log("Cliente " + i + " desconectou");
                    m_Connections[i] = default(NetworkConnection);
                }
            }
        }
    }
Ejemplo n.º 8
0
    public static void SendMessage(NetworkDriver networkDriver, MessageHeader message, NetworkConnection id)
    {
        var writer = networkDriver.BeginSend(id);

        message.SerializeObject(ref writer);
        networkDriver.EndSend(writer);
    }
Ejemplo n.º 9
0
    public void Send(NetworkConnection connection, INetworkMessage message)
    {
        var writer = driver.BeginSend(reliablePipeline, connection);

        message.Serialize(ref writer);
        driver.EndSend(writer);
    }
Ejemplo n.º 10
0
        public void Execute(Entity entity, int index, ref PingClientConnectionComponentData connection)
        {
            if (!serverEP.IsValid)
            {
                connection.connection.Disconnect(driver);
                commandBuffer.DestroyEntity(entity);
                return;
            }
            DataStreamReader strm;

            NetworkEvent.Type cmd;
            while ((cmd = connection.connection.PopEvent(driver, out strm)) != NetworkEvent.Type.Empty)
            {
                if (cmd == NetworkEvent.Type.Connect)
                {
                    pendingPings[0] = new PendingPing {
                        id = pingStats[0], time = frameTime
                    };
                    var pingData = driver.BeginSend(connection.connection);
                    pingData.WriteInt(pingStats[0]);
                    driver.EndSend(pingData);
                    pingStats[0] = pingStats[0] + 1;
                }
                else if (cmd == NetworkEvent.Type.Data)
                {
                    pingStats[1] = (int)((frameTime - pendingPings[0].time) * 1000);
                    connection.connection.Disconnect(driver);
                    commandBuffer.DestroyEntity(entity);
                }
                else if (cmd == NetworkEvent.Type.Disconnect)
                {
                    commandBuffer.DestroyEntity(entity);
                }
            }
        }
Ejemplo n.º 11
0
    private void Update()
    {
        m_Driver.ScheduleUpdate().Complete();

        //Não está conectado
        if (!m_Connection.IsCreated)
        {
            if (!Done)
            {
                return;
            }
        }

        while ((cmd = m_Driver.PopEvent(out m_Connection, out DataStreamReader reader)) != NetworkEvent.Type.Empty)
        {
            //Receiving data from client
            if (cmd == NetworkEvent.Type.Data)
            {
                int num = reader.ReadInt();
                Debug.Log("Numero recebido: " + num);

                total += num;

                var writer = m_Driver.BeginSend(NetworkPipeline.Null, m_Connections[i]);
                writer.WriteInt(total);
                m_Driver.EndSend(writer);
            }
            //Check if the message of client is a disconnection
            else if (cmd == NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Cliente " + i + " desconectou");
                m_Connections[i] = default(NetworkConnection);
            }
        }
    }
    public void SendMessage(MessageHeader message, NetworkConnection connection)
    {
        var writer = networkDriver.BeginSend(connection);

        message.SerializeObject(ref writer);
        networkDriver.EndSend(writer);
    }
        public void SendToServer(NetMessage message)
        {
            DataStreamWriter writer;

            driver.BeginSend(default(NetworkPipeline), server, out writer);
            message.Serialize(ref writer);
            driver.EndSend(writer);
        }
    void SendToClient(string message, NetworkConnection c)
    {
        var writer = m_Driver.BeginSend(NetworkPipeline.Null, c);
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);
    }
Ejemplo n.º 15
0
    void SendToServer(string message)   //WHAT TO USE THE SEND THE CLIENT UPDATE TO THE SERVER
    {
        var writer = m_Driver.BeginSend(m_Connection);
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);
    }
Ejemplo n.º 16
0
    void SendToServer(string message)
    {
        var writer = m_Driver.BeginSend(m_Connection);
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);
    }
Ejemplo n.º 17
0
    private void SendData(object data)
    {
        var writer = m_Driver.BeginSend(m_Connection);
        NativeArray <byte> sendBytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(JsonUtility.ToJson(data)), Allocator.Temp);

        writer.WriteBytes(sendBytes);
        m_Driver.EndSend(writer);
    }
Ejemplo n.º 18
0
 public void SendMessage(MessageHeader message)
 {
     if (connection != default)
     {
         var writer = networkDriver.BeginSend(connection);
         message.SerializeObject(ref writer);
         networkDriver.EndSend(writer);
     }
 }
Ejemplo n.º 19
0
    void Update()
    {
        driver.ScheduleUpdate().Complete();

        if (!connection.IsCreated)
        {
            //Debug.Log( "Client | Something went wrong during connect" );
            return;
        }

        DataStreamReader stream;

        Unity.Networking.Transport.NetworkEvent.Type cmd;
        while ((cmd = connection.PopEvent(driver, out stream)) != Unity.Networking.Transport.NetworkEvent.Type.Empty)
        {
            if (cmd == Unity.Networking.Transport.NetworkEvent.Type.Connect)
            {
                isConnected.Value = true;
                Debug.Log("Client | We are now connected to the server");
                eventsToSend.Add(updatePlayerNameEvent);
            }
            else if (cmd == Unity.Networking.Transport.NetworkEvent.Type.Data)
            {
                int packetID = stream.ReadInt();
                Debug.Log("Client | Recieved packet: " + allEvents.Items[packetID].displayName);

                //Handle recieved events.
                allEvents.Items[packetID].ReadPacket(stream);
                lastRecievedPacket.Value = allEvents.Items[packetID].displayName;
            }
            else if (cmd == Unity.Networking.Transport.NetworkEvent.Type.Disconnect)
            {
                Debug.Log("Client | Got disconnected from server");
                isConnected.Value = false;
                SceneManager.LoadSceneAsync(sceneToLoadOnDisconnect);
                if (GameObject.Find("ClientBehaviour"))
                {
                    Destroy(GameObject.Find("ClientBehaviour"));
                }
                if (GameObject.Find("HostBehaviour"))
                {
                    Destroy(GameObject.Find("HostBehaviour"));
                }
                connection = default(NetworkConnection);
            }
        }

        //Send queued events.
        for (int i = 0; i < eventsToSend.Items.Count; i++)
        {
            Debug.Log("Client | Sending packet: " + eventsToSend.Items[i].displayName);
            var writer = driver.BeginSend(NetworkPipeline.Null, connection);
            writer = eventsToSend.Items[i].WritePacket(writer);
            driver.EndSend(writer);
        }
        eventsToSend.Items.Clear();
    }
    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
                if (m_ClientDriver.BeginSend(m_clientToServerConnection, out var pingData) == 0)
                {
                    pingData.WriteInt(m_numPingsSent);
                    m_ClientDriver.EndSend(pingData);
                }
                // 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);
            }
        }
    }
    public void SendMessage(MessageHeader message)
    {
        networkJobHandle.Complete();

        var writer = networkDriver.BeginSend(connection);

        message.SerializeObject(ref writer);
        networkDriver.EndSend(writer);
    }
    private void SendMessage(Message sendMessage)
    {
        var writer = networkDriver.BeginSend(connection);

        sendMessage.SerializeObject(ref writer);
        networkDriver.EndSend(writer);
        lastSendTime = Time.time;

        Debug.Log("Client Sending Message: " + sendMessage.Type + " to Host");
    }
Ejemplo n.º 23
0
    void SendBattleMessage(PlayerMessage enemy, bool turn, NetworkConnection c)
    {
        Assert.IsTrue(c.IsCreated);

        var bMsg = new BattleMessage();

        bMsg.turn        = turn;
        bMsg.enemyID     = enemy.connectionID;
        bMsg.playerName  = enemy.playerName;
        bMsg.pokemonName = enemy.pokemonName;
        bMsg.hp          = enemy.hp;
        bMsg.Lvl         = enemy.Lvl;

        string             message = JsonUtility.ToJson(bMsg);
        var                writer  = m_Driver.BeginSend(NetworkPipeline.Null, c);
        NativeArray <byte> bytes   = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);
    }
        public IEnumerator ServerAndClient_PingPong_Successfully()
        {
            SetupServerAndClientAndConnectThem(0);

            //send data from client
            DataStreamWriter m_OutStream = client_driver.BeginSend(clientToServerConnection);

            m_OutStream.Clear();
            m_OutStream.WriteBytes(new NativeArray <byte>(SharedConstants.ping, Allocator.Temp));
            client_driver.EndSend(m_OutStream);
            client_driver.ScheduleFlushSend(default).Complete();
    void SendToServer(string message)
    {
        Debug.Log("[Client] Send message to server : " + message);
        var writer = m_Driver.BeginSend(m_Connection);
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);

        lastTimestamp = Time.time;
    }
Ejemplo n.º 26
0
    void SendToServer(string message)
    {
        //When you establish a connection between the client and the server,
        //you send a data. The use of the BeginSend / EndSend pattern together with the DataStreamWriter,
        //write data into the stream, and finally send it out on the network.

        var writer = m_Driver.BeginSend(m_Connection);
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        writer.WriteBytes(bytes);
        m_Driver.EndSend(writer);
    }
    void SendToClient(string message, NetworkConnection c)
    {
        //Create writer
        var writer = m_Driver.BeginSend(NetworkPipeline.Null, c);
        //Create array of bytes
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        //Write bytes to writer
        writer.WriteBytes(bytes);
        //Send writer
        m_Driver.EndSend(writer);
    }
    void SendToServer(string message)
    {
        //Create writer
        var writer = m_Driver.BeginSend(m_Connection);
        //Create array of bytes
        NativeArray <byte> bytes = new NativeArray <byte>(Encoding.ASCII.GetBytes(message), Allocator.Temp);

        //Write bytes to writer
        writer.WriteBytes(bytes);
        //Send message (writer)
        m_Driver.EndSend(writer);
    }
Ejemplo n.º 29
0
    public void SendActionToServer(uint pAction)
    {
        var writer = Driver.BeginSend(Connection);

        writer.WriteUInt(pAction);
        Driver.EndSend(writer);
    }
Ejemplo n.º 30
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());
        }