Exemplo n.º 1
0
 private void SendString(string msg, short connectionID = -1, bool reliable = true)
 {
     if (mNetwork == null || mConnections.Count == 0)
     {
         Debug.Log("No connection. Can't send message.");
     }
     else
     {
         byte[] msgData = Encoding.UTF8.GetBytes(msg);
         if (connectionID == -1)
         {
             Debug.Log("Sending message to all.");
             foreach (ConnectionId id in mConnections)
             {
                 mNetwork.SendData(id, msgData, 0, msgData.Length, reliable);
             }
         }
         else
         {
             foreach (ConnectionId id in mConnections)
             {
                 if (id.id == connectionID)
                 {
                     Debug.Log("Sending message just to " + connectionID);
                     mNetwork.SendData(id, msgData, 0, msgData.Length, reliable);
                     return;
                 }
             }
             Debug.Log("Could not send message to connectionID: " + connectionID);
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Sends a packet over the network
        /// </summary>
        /// <param name="packet">The <see cref="Packet"/> instance that is to be sent</param>
        /// <param name="reliable">Whether the transmission is reliable (slow) or unreliable (fast)</param>
        /// <returns>Whether message can be sent or not</returns>
        public bool Send(Packet packet, bool reliable = false)
        {
            if (m_Network == null || ConnectionIds == null || ConnectionIds.Count == 0)
            {
                return(false);
            }

            List <ConnectionId> recipients = new List <ConnectionId>();

            if (packet.Recipients.Length != 0)
            {
                recipients = ConnectionIds.Select(x => x)
                             .Where(x => packet.Recipients.Contains(x.id))
                             .ToList();
            }
            else
            {
                recipients = ConnectionIds.ToList();
            }

            var bytes = packet.Serialize();

            foreach (var cid in recipients)
            {
                m_Network.SendData(cid, bytes, 0, bytes.Length, reliable);
            }

            return(true);
        }
Exemplo n.º 3
0
 protected override void SendReliable(PureP2PPeer connection, ArraySegment <byte> packet)
 {
     if (connection.IsLocalLoopback)
     {
         _network.LoopbackToClient(packet);
     }
     else
     {
         _webrtcNetwork.SendData(connection.ConnectionId, packet.Array, packet.Offset, packet.Count, true);
     }
 }
Exemplo n.º 4
0
 public void SendMessage(ConnectionId connectionId, byte[] data, int dataOffset, int dataLen, bool isReliable)
 {
     if (mNetwork != null && peers.ContainsKey(connectionId.ToString()))
     {
         mNetwork.SendData(connectionId, data, dataOffset, dataLen, isReliable);
     }
     else if (mNetwork == null)
     {
         PrintDebug("Can't send message, network isn't initialized");
     }
     else
     {
         PrintDebug("Can't send message, connection id " + connectionId.ToString() + " is not a currently connected peer");
     }
 }
Exemplo n.º 5
0
    public void CloseServer()
    {
        if (mIsServer)
        {
            foreach (KeyValuePair <ConnectionId, ConnectionStatus> p in mConnections)
            {
                ConnectionId id        = p.Key;
                byte[]       closeData = { 0xFF, 0x00 };
                mNetwork.SendData(id, closeData, 0, closeData.Length, true);
            }

            mNetwork.StopServer();
            Reset();
        }
    }
Exemplo n.º 6
0
    // Función para enviar datos de tipo texto. Usado para enviar los mensajes del chat.
    private void SendString(string msg)
    {
        if (mNetwork == null || mConnections.Count == 0)
        {
            Append("No hay nadie conectado a tu sala.");
        }
        else
        {
            // Se coge el mensaje que se haya puesto en el chat, y se guarda en la variable msg, la cual se convertirá a bytes mediante Encoding.UTF8
            byte[] msgData = Encoding.UTF8.GetBytes(msg);

            // Recorrerá un vector interno en donde se almacenan las conexiones que hay dentro de una sala de streaming. Mediante mConnections se identifica a quien enviar los mensajes.
            foreach (ConnectionId id in mConnections)
            {
                //Mediante la función SendData se envia los bytes del msg a la id especificada por el canal UNREALIABLE que proporciona el asset.
                mNetwork.SendData(id, msgData, 0, msgData.Length, false);
            }
        }
    }
Exemplo n.º 7
0
    public void Send(string message, bool isReliable = true)
    {
        if (IsConnected())
        {
            byte[] data = Encoding.UTF8.GetBytes(message);

            foreach (ConnectionId id in connections)
            {
                network.SendData(id, data, 0, data.Length, isReliable);
            }
        }
    }
Exemplo n.º 8
0
 /// <summary>
 /// Sends a string as UTF8 byte array to all connections
 /// </summary>
 /// <param name="msg">String containing the message to send</param>
 /// <param name="reliable">false to use unreliable messages / true to use reliable messages</param>
 private void SendString(string msg, bool reliable = true)
 {
     if (mNetwork == null || mConnections.Count == 0)
     {
         Append("No connection. Can't send message.");
     }
     else
     {
         byte[] msgData = Encoding.UTF8.GetBytes(msg);
         foreach (ConnectionId id in mConnections)
         {
             mNetwork.SendData(id, msgData, 0, msgData.Length, reliable);
         }
     }
 }
Exemplo n.º 9
0
 /// <summary>
 /// Sends data over a connection
 /// </summary>
 /// <param name="id">The Id of the connection to send data to</param>
 /// <param name="data">The data to be sent</param>
 /// <param name="offset">Offset from the byte array data</param>
 /// <param name="len">Length of the data starting</param>
 /// <param name="reliable">Whether data is sent UDP/TCP style</param>
 public void SendData
     (ConnectionId id, byte[] data, int offset, int len, bool reliable) =>
 network.SendData(id, data, offset, len, reliable);
Exemplo n.º 10
0
 private void Send(ConnectionId connection, ArraySegment <byte> packet, bool reliable)
 {
     _webrtcNetwork.SendData(connection, packet.Array, packet.Offset, packet.Count, reliable);
 }