Esempio n. 1
0
 /// <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);
             }
         }
     }
 }
Esempio n. 2
0
 /// <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);
     }
 }
Esempio n. 3
0
        /// <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);
        }
Esempio n. 4
0
        /// <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");
        }
Esempio n. 5
0
        /// <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);
        }
Esempio n. 6
0
 /// <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);
         }
     }
 }
Esempio n. 7
0
 /// <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;
         }
     }
 }