/// <summary>
 /// Broadcast messages to all connected clients.
 /// </summary>
 /// <param name="Custom">Custom bytearray to send to the clients.</param>
 /// <param name="sync">if it is true, will sync players position.</param>
 public void Broadcast(byte[] Custom, bool sync)
 {
     //TODO: FRONTEND TO THE SERVER.
     for (int i = 0; i < users.Count; i++)
     {
         if (users[i].TimeOut >= 10)
         {
             remove(users[i].Id, "Timed Out");
         }
         else
         {
             try
             {
                 broadcast = users[i].EndPoint;
                 byte[] msg;
                 if (sync)
                 {
                     msg = PkgMngr.GenerateMessage(PkgInterf.PING, users[i].GetID());
                     server.Send(msg, msg.Length, broadcast);
                     users[i].TimeOut++;
                 }
                 if (Custom != null)
                 {
                     server.Send(Custom, Custom.Length, broadcast);
                     //Console.WriteLine("Sent:" + PkgMngr.Translate(Custom));
                 }
             }
             catch (SocketException e)
             {
                 remove(users[i].Address, users[i].Port, "Problem: " + e);
             }
         }
     }
 }
 /// <summary>
 /// Sync player's position
 /// </summary>
 public void Sync()
 {
     //HACK: Temporary.
     for (int j = 0; j < users.Count; j++)
     {
         try
         {
             broadcast = users[j].EndPoint;
             for (int i = 0; i < users.Count; i++)
             {
                 if (i != j && users[i] != null)
                 {
                     byte[] msg = PkgMngr.GenerateMessage(PkgInterf.SYNC, users[i].GetPosition(), users[i].GetID());
                     //Console.WriteLine("DEBUG — Sending: " + msg.Length + " Bits.");
                     server.Send(msg, msg.Length, broadcast);
                 }
             }
         }
         catch (Exception e)
         {
             Console.WriteLine("Something happened: " + e.Message);
             continue;
         }
     }
     if (users.Count == 0)
     {
         Thread.Sleep(50);
     }
 }
        /// <summary>
        /// Send a message directly to an ID
        /// </summary>
        /// <param name="signal">Signal token to an server command.</param>
        /// <param name="message">Byte array to be sent to the server.</param>
        /// <param name="ID">ID of the user</param>
        public void sendDirect(byte signal, byte[] message, int ID = -1)
        {
            Console.WriteLine("Testing");
            IPEndPoint EP = Search(ID).EndPoint;

            byte[] msg = PkgMngr.GenerateMessage(signal, message);
            server.Send(msg, msg.Length, EP);
        }
        /// <summary>
        /// Send a message directly to a address
        /// </summary>
        /// <param name="signal">Signal token to an server command.</param>
        /// <param name="message">Byte array to be sent to the server.</param>
        /// <param name="Address">IP Address of the client.</param>
        /// <param name="_port">Port of the server.</param>
        public void sendDirect(byte signal, byte[] message, IPAddress Address, int _port)
        {
            IPEndPoint EP = new IPEndPoint(Address, _port);

            byte[] msg = PkgMngr.GenerateMessage(signal, message);
            server.Send(msg, msg.Length, EP);
            Console.WriteLine("Sending Handshake");
        }
        public void sendDirect(string message, string Address, int _port)
        {
            IPEndPoint EP = new IPEndPoint(IPAddress.Parse(Address), _port);

            byte[] msg = PkgMngr.GetBytes(message);
            server.Send(msg, msg.Length, EP);
            Console.WriteLine("Sending HandShake");
        }
        /// <summary>
        /// Insert a new client to the list
        /// </summary>
        /// <param name="Address">Client's IP Address.</param>
        /// <param name="_port">Client's port</param>
        /// <param name="name">Client's name</param>
        /// <param name="qt_custom">Extra sync vars</param>
        /// <returns>Client object</returns>
        public Client insert(string Address, int _port, string name)
        {
            Client obj = new Client(Address, _port, name, IDcont);

            Console.WriteLine("Welcome! " + name + " with the ID: " + IDcont);
            byte[] advise = PkgMngr.GenerateMessage(PkgInterf.JOIN, BitConverter.GetBytes(IDcont), PkgMngr.GetBytes(name));
            Broadcast(advise, false);
            users.Add(obj);
            IDcont++;
            return(obj);
        }
Exemple #7
0
 public WSServer(UDPnetKode Translator, int port = 8585)
 {
     SocketServer = Translator;
     WSS          = new WebSocketServer("ws://0.0.0.0:" + port);
     WSS.Start(socket =>
     {
         socket.OnOpen    = () => Console.WriteLine("Open!");
         socket.OnClose   = () => Console.WriteLine("Close!");
         socket.OnMessage = message => Translator.sendDirect(PkgMngr.GetBytes(message[0]), PkgMngr.TrimByteArray(1, message.Length, PkgMngr.GetBytes(message)));
     });
 }
 /// <summary>
 /// removes a client from the list
 /// </summary>
 /// <param name="_ID">Client ID</param>
 /// <param name="Why">Reason why it is going to be removed</param>
 public void remove(int _ID = -1, string Why = "")
 {
     Console.WriteLine("Removing a client by: " + Why);
     for (int i = 0; i < users.Count; i++)
     {
         if (users[i].Id == _ID)
         {
             users.RemoveAt(i);
             Broadcast(PkgMngr.GenerateMessage(PkgInterf.GOODBYE, BitConverter.GetBytes(_ID)), false);
         }
     }
 }
        public void SendEverybodyID(IPAddress IP, int Port)
        {
            byte[] pkg   = new byte[4 * users.Count];
            int    index = 0;

            for (int i = 0; i < users.Count; i++)
            {
                byte[] _id = users[i].GetID();
                for (int j = 0; j < 4; j++)
                {
                    pkg[index] = _id[j];
                }
            }
            byte[] final_pkg = PkgMngr.Union(BitConverter.GetBytes(users.Count), pkg);
            sendDirect(PkgInterf.UPDATE, final_pkg, IP, Port);
        }
 /// <summary>
 /// removes a client from the list
 /// </summary>
 /// <param name="ip">Client's IP Address</param>
 /// <param name="port">Client's port</param>
 /// <param name="Why">Reason why it is going to be removed</param>
 public void remove(string ip = "", int port = 8484, string Why = "")
 {
     Console.WriteLine("Removing a client by: " + Why);
     for (int i = 0; i < users.Count; i++)
     {
         if (users[i].Address == ip && users[i].Port == port)
         {
             int    id   = users[i].Id;
             string name = users[i].Name;
             users.RemoveAt(i);
             Console.WriteLine("Disconnected: " + name);
             Broadcast(PkgMngr.GenerateMessage(PkgInterf.GOODBYE, BitConverter.GetBytes(id)), false);
             break;
         }
     }
 }
Exemple #11
0
        private void Wsc_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            Console.WriteLine("Relay: " + e.Message);
            byte[] pkg = PkgMngr.GetBytes(e.Message);
            try
            {
                switch (pkg[0])
                {
                case PkgInterf.HELLO:

                    break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Trying Communication:" + ex.Message);
            }
        }
        /// <summary>
        /// Server receives messages from the clients
        /// </summary>
        public void RecvMessage()
        {
            while (alive)
            {
                try
                {
                    byte[] Pkg = server.Receive(ref clientes);
                    switch (Pkg[0])
                    {
                    case PkgInterf.HELLO:
                        if (Search(clientes.Address.ToString(), clientes.Port) == null)
                        {
                            byte[] idtosend = new byte[4];
                            idtosend = BitConverter.GetBytes(IDcont);
                            insert(clientes.Address.ToString(), clientes.Port, PkgMngr.Translate(PkgMngr.TrimByteArray(1, Pkg.Length, Pkg)));
                            Console.WriteLine("Connection incoming [{0}:{1}]", clientes.Address.ToString(), clientes.Port);
                            sendDirect(PkgInterf.HANDSHAKE, idtosend, clientes.Address, clientes.Port);
                            //SendEverybodyID(clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.GOODBYE:
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            Client temp = Search(clientes.Address.ToString(), clientes.Port);
                            remove(temp.Id, "Disconnected Safely");
                        }
                        else
                        {
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.PING:
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            Search(clientes.Address.ToString(), clientes.Port).TimeOut = 0;
                        }
                        else
                        {
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.SYNC:
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            /*1-XXXX-YYYY-ZZZZ*/
                            Client temp = Search(clientes.Address.ToString(), clientes.Port);
                            temp.SetPositionByByteArray(PkgMngr.TrimByteArray(1, 13, Pkg));
                        }
                        else
                        {
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.INPUT:
                        /*It will send to all players the input to of an player
                         * but syncing his position and rotation before all thing*/
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            /*H-I-P-IDID-XXXX-YYYY-ZZZZ-X-Y-Z
                             * HEADER :     3		    UNSIGNED CHAR/BYTE
                             * INPUTKEY :   0-255       UNSIGNED CHAR/BYTE	BUTTON PRESSED
                             * PWM :        0-255		BOOL/BYTE		    PWM BUTTON ON-OFF
                             * IDID :		2^32		INT32			    PLAYER ID
                             * XXXX :		0.0000		FLOAT			    X Position.
                             * YYYY :		0.0000		FLOAT			    Y Position.
                             * ZZZZ :		0.0000		FLOAT			    Z Position.
                             * X :		    0-255		BYTE			    X Rotation.
                             * Y :		    0-255		BYTE			    Y Rotation.
                             * Z :		    0-255		BYTE			    Z Rotation.*/
                            Client temp = Search(clientes.Address.ToString(), clientes.Port);
                            temp.NewInput(Pkg);
                        }
                        else
                        {
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.CHAT:
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            Client clien = Search(clientes.Address.ToString(), clientes.Port);
                            Console.WriteLine(clien.Name + ": " + PkgMngr.Translate(Pkg));
                        }
                        else
                        {
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;

                    case PkgInterf.INFO:
                        Broadcast(Pkg, false);
                        break;

                    case PkgInterf.REQUEST:
                        Client temp_client = Search(clientes.Address.ToString(), clientes.Port);
                        if (temp_client != null)
                        {
                            byte[] idtosend = new byte[4];
                            idtosend = temp_client.GetID();
                            sendDirect(PkgInterf.HANDSHAKE, idtosend, clientes.Address, clientes.Port);
                        }
                        break;

                    default:
                        if (Search(clientes.Address.ToString(), clientes.Port) != null)
                        {
                            Client clien = Search(clientes.Address.ToString(), clientes.Port);
                            Console.WriteLine(clien.Name + ": " + PkgMngr.Translate(Pkg) + " (Using an unkown header)");
                        }
                        else
                        {
                            Console.WriteLine("Invalid Session!");
                            sendDirect(PkgInterf.ERROR, clientes.Address, clientes.Port);
                        }
                        break;
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("Trying Communication. error: " + e.Message);
                }
            }
        }