Esempio n. 1
0
 public void SendAll(ArraySegment <byte> data)
 {
     foreach (var c in _server.clients.Keys)
     {
         _server.Send(c, data);
     }
 }
        public void SendPlayerPositions()
        {
            try
            {
                Dictionary <long, NetworkPlayer> sendPosDict = new Dictionary <long, NetworkPlayer>(networkPlayersDictionary);

                foreach (var sendToPlayer in sendPosDict)
                {
                    if (sendToPlayer.Value == null)
                    {
                        continue;
                    }

                    var message = new NetworkMessage();

                    message.AddTagPacket(NetworkTagPacket.PlayerPositionsArray);
                    message.AddUInt32((uint)sendPosDict.Count(c => c.Key != sendToPlayer.Key && c.Value.Moved));

                    int amountPlayersMoved = 0;

                    foreach (var posPlayers in sendPosDict)
                    {
                        if (sendToPlayer.Key == posPlayers.Key)
                        {
                            continue;
                        }

                        if (!posPlayers.Value.Moved)
                        {
                            continue;
                        }

                        message.AddUInt32((uint)posPlayers.Key);

                        message.AddFloat(posPlayers.Value.X);
                        message.AddFloat(posPlayers.Value.Y);
                        message.AddFloat(posPlayers.Value.Z);

                        amountPlayersMoved++;
                    }

                    if (amountPlayersMoved > 0)
                    {
                        server.Send(sendToPlayer.Value.ConnectionID, message.Buffer);
                    }
                }

                foreach (var player in networkPlayersDictionary)
                {
                    player.Value.Moved = false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
Esempio n. 3
0
 void SendClientConnected()
 {
     for (int i = 0; i < Players.Count; i++)
     {
         for (int o = 0; o < Players.Count; o++)
         {
             server.Send(Players[i].connectionId, SendPlayerWhoConnectedToClients(o));
         }
     }
 }
Esempio n. 4
0
    public void OnClientConnected(int connectionID)
    {
        byte[] bytesArray = DataParser.ObjecttoByteArray <FileObject>(fileObject);
        server.Send(connectionID, bytesArray);
        SetModel model = new SetModel();

        model.index = currentServerIndex;
        byte[] firstData = DataParser.ObjecttoByteArray <SetModel>(model);
        server.Send(connectionID, firstData);
        byte[] secondData = DataParser.ObjecttoByteArray <SetPosition>(currentPosition);
        server.Send(connectionID, secondData);
    }
Esempio n. 5
0
    void Update()
    {
        // client
        if (client.Connected)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.Send(new byte[] { 0x1 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (client.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log("Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log("Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log("Disconnected");
                    break;
                }
            }
        }

        // server
        if (server.Active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                server.Send(0, new byte[] { 0x2 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log(msg.connectionId + " Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log(msg.connectionId + " Disconnected");
                    break;
                }
            }
        }
    }
Esempio n. 6
0
 private void Update()
 {
     if (telepathyServer != null)
     {
         while (telepathyServer.GetNextMessage(out Telepathy.Message message))
         {
             if (message.eventType == Telepathy.EventType.Data)
             {
                 telepathyServer.Send(message.connectionId, message.data);
             }
         }
     }
     else if (saeaServer != null)
     {
         while (saeaServer.GetNextMessage(out Mirror.Saea.Message message))
         {
             if (message.eventType == Mirror.Saea.EventType.Data)
             {
                 saeaServer.Send(message.connectionId, message.data);
             }
         }
     }
     else if (asyncServer != null)
     {
         asyncServer.Flush();
     }
 }
    public void SendMessage(int connectionId, BADNetworkMessage networkMessage)
    {
        var data          = JsonConvert.SerializeObject(networkMessage);
        var encoded       = Encoding.UTF8.GetBytes(data);
        var asWriteBuffer = new ArraySegment <Byte>(encoded, 0, encoded.Length);

        Debug.Log("send message to playerSessionId: " + networkMessage._playerSessionId + ", with connId: " + connectionId);
        _server.Send(connectionId, asWriteBuffer);
    }
Esempio n. 8
0
        public override void ServerSend(int connectionId, int channelId, ArraySegment <byte> segment)
        {
            // telepathy doesn't support allocation-free sends yet.
            // previously we allocated in Mirror. now we do it here.
            byte[] data = new byte[segment.Count];
            Array.Copy(segment.Array, segment.Offset, data, 0, segment.Count);

            // send
            server.Send(connectionId, data);
        }
Esempio n. 9
0
    static void Main(string[] args)
    {
        server = new Telepathy.Server();
        server.Start(1337);

        Task.Run(() =>
        {
            while (server.Active)
            {
                Telepathy.Message msg;
                while (server.GetNextMessage(out msg))
                {
                    switch (msg.eventType)
                    {
                    case Telepathy.EventType.Connected:
                        // msg.connectionIdはクライアント接続時に+1される数値。1始まり
                        Console.WriteLine("Telepathy.EventType.Connected : msg.connectionId=" + msg.connectionId);
                        break;

                    case Telepathy.EventType.Data:
                        Console.WriteLine("Telepathy.EventType.Data : msg.connectionId=" + msg.connectionId + ", msg.data" + System.Text.Encoding.UTF8.GetString(msg.data));

                        // reply
                        Console.WriteLine("send massage from server...");
                        server.Send(msg.connectionId, System.Text.Encoding.UTF8.GetBytes("send message from server..."));
                        break;

                    case Telepathy.EventType.Disconnected:
                        Console.WriteLine("Telepathy.EventType.Disconnected : msg.connectionId=" + msg.connectionId);
                        break;
                    }
                }
            }
        });

        bool break_flag = false;

        while (break_flag == false)
        {
            ConsoleKey k = Console.ReadKey().Key;
            switch (k)
            {
            case ConsoleKey.L:
                break;

            case ConsoleKey.Escape:
                break_flag = true;
                break;
            }
        }

        server.Stop();
    }
Esempio n. 10
0
    private void Update()
    {
        if (server == null)
        {
            return;
        }

        Telepathy.Message message;

        while (server.GetNextMessage(out message))
        {
            if (message.eventType == Telepathy.EventType.Data)
            {
                server.Send(message.connectionId, message.data);
            }
        }
    }
        public void Run()
        {
            try
            {
                networkPlayersDictionary = new Dictionary <long, NetworkPlayer>();

                server = new Telepathy.Server();
                server.Start(1337);

                while (server.Active)
                {
                    // reply to each incoming message
                    Message msg;
                    while (server.GetNextMessage(out msg))
                    {
                        if (msg.eventType == EventType.Connected)
                        {
                            OnClientConnected(msg.connectionId);
                        }
                        else if (msg.eventType == EventType.Data)
                        {
                            server.Send(msg.connectionId, msg.data);
                            OnMessageReceived(new NetworkMessage(msg.data), msg.connectionId);
                        }
                        else if (msg.eventType == EventType.Disconnected)
                        {
                            OnClientDisconnected(msg.connectionId);
                        }
                    }

                    SendPlayerPositions();

                    System.Threading.Thread.Sleep(15);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
        }
Esempio n. 12
0
 private void SendNetworkMessageToClient(int _clientID, SNetworkMessage _message)
 {
     server.Send(_clientID, Encoding.UTF8.GetBytes(JsonUtility.ToJson(_message)));
 }
Esempio n. 13
0
 protected override void SendBytes(KNetConnection target, byte[] bytes)
 {
     server.Send(target.Id, bytes);
 }
Esempio n. 14
0
    void Update()
    {
        // client
        if (client.Connected)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                client.Send(new byte[] { 0x1 });

                //client.Send(Telepathy.Utils.StringToBytes(GameObject.Find("PlayerObject(Clone)").name)); // sends the players name as a byte[]
                //byte[] bytesvalue = Telepathy.Utils.StringToBytes(GameObject.Find("PlayerObject").name);
            }
            if (Input.GetKeyDown(KeyCode.LeftAlt))
            {
                GameObject.Instantiate(SquarePrefab, Vector3.zero, Quaternion.identity);
            }
            // show all new messages
            Telepathy.Message msg;
            while (client.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log("Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log("Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log("Disconnected");
                    break;
                }
            }
        }

        // server
        if (server.Active)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                server.Send(0, new byte[] { 0x2 });
            }

            // show all new messages
            Telepathy.Message msg;
            while (server.GetNextMessage(out msg))
            {
                switch (msg.eventType)
                {
                case Telepathy.EventType.Connected:
                    Debug.Log(msg.connectionId + " Connected");
                    break;

                case Telepathy.EventType.Data:
                    Debug.Log(msg.connectionId + " Data: " + BitConverter.ToString(msg.data));
                    break;

                case Telepathy.EventType.Disconnected:
                    Debug.Log(msg.connectionId + " Disconnected");
                    break;
                }
            }
        }
    }