Exemplo n.º 1
0
 private void BroadCastCommand(Command cmd)
 {
     foreach (var mngr in _clients)
         if (!mngr.IP.Equals(cmd.SenderIP))
         {
             mngr.SendCommand(cmd);
         }
 }
Exemplo n.º 2
0
 private void SendCommandToTarget(Command cmd)
 {
     foreach (var mngr in _clients)
         if (mngr.IP.Equals(cmd.Target))
         {
             mngr.SendCommand(cmd);
             return;
         }
 }
Exemplo n.º 3
0
 private void SendClientListToNewClient(IPAddress newClientIp)
 {
     foreach (var mngr in _clients)
     {
         if (mngr.Connected && !mngr.IP.Equals(newClientIp))
         {
             var cmd = new Command(CommandType.SendClientList, newClientIp)
             {
                 MetaData = mngr.IP + ":" + mngr.ClientName,
                 SenderIP = _serverIp,
                 SenderName = "Server"
             };
             SendCommandToTarget(cmd);
         }
     }
 }
Exemplo n.º 4
0
        private bool RemoveClientManager(IPAddress ip)
        {
            lock (this)
            {
                var index = IndexOfClient(ip);
                if (index != -1)
                {
                    var name = _clients[index].ClientName;
                    _clients.RemoveAt(index);

                    //Inform all clients that a client had been disconnected.
                    var cmd = new Command(CommandType.ClientLogOffInform, IPAddress.Broadcast) {SenderName = name, SenderIP = ip};
                    BroadCastCommand(cmd);
                    return true;
                }
                return false;
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Creates an instance of CommandEventArgs class.
 /// </summary>
 /// <param name="cmd">The received command.</param>
 public CommandEventArgs(Command cmd)
 {
     Command = cmd;
 }
Exemplo n.º 6
0
        private void StartReceive(object sender, DoWorkEventArgs e)
        {
            while (_socket.Connected)
            {
                //Read the command's Type.
                var buffer = new byte[4];
                var readBytes = _networkStream.Read(buffer, 0, 4);
                if (readBytes == 0)
                    break;
                var cmdType = (CommandType) (BitConverter.ToInt32(buffer, 0));

                //Read the command's target size.
                buffer = new byte[4];
                readBytes = _networkStream.Read(buffer, 0, 4);
                if (readBytes == 0)
                    break;
                var ipSize = BitConverter.ToInt32(buffer, 0);

                //Read the command's target.
                buffer = new byte[ipSize];
                readBytes = _networkStream.Read(buffer, 0, ipSize);
                if (readBytes == 0)
                    break;
                var cmdTarget = Encoding.ASCII.GetString(buffer);

                //Read the command's MetaData size.
                buffer = new byte[4];
                readBytes = _networkStream.Read(buffer, 0, 4);
                if (readBytes == 0)
                    break;
                var metaDataSize = BitConverter.ToInt32(buffer, 0);

                //Read the command's Meta data.
                buffer = new byte[metaDataSize];
                readBytes = _networkStream.Read(buffer, 0, metaDataSize);
                if (readBytes == 0)
                    break;
                var cmdMetaData = Encoding.Unicode.GetString(buffer);

                var cmd = new Command(cmdType, IPAddress.Parse(cmdTarget), cmdMetaData) {SenderIP = IP};
                cmd.SenderName = cmd.CommandType == CommandType.ClientLoginInform ? cmd.MetaData.Split(new[] {':'})[1] : ClientName;
                OnCommandReceived(new CommandEventArgs(cmd));
            }
            OnDisconnected(new ClientEventArgs(_socket));
            Disconnect();
        }
Exemplo n.º 7
0
        private bool SendCommandToClient(Command cmd)
        {
            try
            {
                _semaphor.WaitOne();
                //Type
                var buffer = BitConverter.GetBytes((int) cmd.CommandType);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();

                //Sender IP
                var senderIpBuffer = Encoding.ASCII.GetBytes(cmd.SenderIP.ToString());
                buffer = BitConverter.GetBytes(senderIpBuffer.Length);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();
                _networkStream.Write(senderIpBuffer, 0, senderIpBuffer.Length);
                _networkStream.Flush();

                //Sender Name
                var senderNameBuffer = Encoding.Unicode.GetBytes(cmd.SenderName);
                buffer = BitConverter.GetBytes(senderNameBuffer.Length);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();
                _networkStream.Write(senderNameBuffer, 0, senderNameBuffer.Length);
                _networkStream.Flush();

                //Target
                var ipBuffer = Encoding.ASCII.GetBytes(cmd.Target.ToString());
                buffer = BitConverter.GetBytes(ipBuffer.Length);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();
                _networkStream.Write(ipBuffer, 0, ipBuffer.Length);
                _networkStream.Flush();

                //Meta Data.
                if (string.IsNullOrEmpty(cmd.MetaData))
                    cmd.MetaData = "\n";

                var metaBuffer = Encoding.Unicode.GetBytes(cmd.MetaData);
                buffer = BitConverter.GetBytes(metaBuffer.Length);
                _networkStream.Write(buffer, 0, 4);
                _networkStream.Flush();
                _networkStream.Write(metaBuffer, 0, metaBuffer.Length);
                _networkStream.Flush();

                _semaphor.Release();
                return true;
            }
            catch
            {
                _semaphor.Release();
                return false;
            }
        }
Exemplo n.º 8
0
        private bool RemoveClientManager(IPAddress ip)
        {
            lock ( this )
            {
                int index = this.IndexOfClient(ip);
                if ( index != -1 )
                {
                    string name = this.clients [index].ClientName;
                    this.clients.RemoveAt(index);

                    //Inform all clients that a client had been disconnected.
                    Command cmd = new Command(CommandType.ClientLogOffInform , IPAddress.Broadcast);
                    cmd.SenderName = name;
                    cmd.SenderIP = ip;
                    this.BroadCastCommand(cmd);
                    return true;
                }
                return false;
            }
        }
Exemplo n.º 9
0
        private void StartReceive(object sender , DoWorkEventArgs e)
        {
            while ( this.socket.Connected )
            {
                //Read the command's Type.
                byte [] buffer = new byte [4];
                int readBytes = this.networkStream.Read(buffer , 0 , 4);
                if ( readBytes == 0 )
                    break;
                CommandType cmdType = (CommandType)( BitConverter.ToInt32(buffer , 0) );

                //Read the command's target size.
                string cmdTarget = "";
                buffer = new byte [4];
                readBytes = this.networkStream.Read(buffer , 0 , 4);
                if ( readBytes == 0 )
                    break;
                int ipSize = BitConverter.ToInt32(buffer , 0);

                //Read the command's target.
                buffer = new byte [ipSize];
                readBytes = this.networkStream.Read(buffer , 0 , ipSize);
                if ( readBytes == 0 )
                    break;
                cmdTarget = System.Text.Encoding.ASCII.GetString(buffer);

                //Read the command's MetaData size.
                string cmdMetaData = "";
                buffer = new byte [4];
                readBytes = this.networkStream.Read(buffer , 0 , 4);
                if ( readBytes == 0 )
                    break;
                int metaDataSize = BitConverter.ToInt32(buffer , 0);

                //Read the command's Meta data.
                buffer = new byte [metaDataSize];
                readBytes = this.networkStream.Read(buffer , 0 , metaDataSize);
                if ( readBytes == 0 )
                    break;
                cmdMetaData = System.Text.Encoding.Unicode.GetString(buffer);

                Command cmd = new Command(cmdType , IPAddress.Parse(cmdTarget) , cmdMetaData);
                cmd.SenderIP = this.IP;
                if ( cmd.CommandType == CommandType.ClientLoginInform )
                    cmd.SenderName = cmd.MetaData.Split(new char [] { ':' }) [1];
                else
                    cmd.SenderName = this.ClientName;
                this.OnCommandReceived(new CommandEventArgs(cmd));
            }
            this.OnDisconnected(new ClientEventArgs(this.socket));
            this.Disconnect();
        }
Exemplo n.º 10
0
        private bool SendCommandToClient(Command cmd)
        {
            try
            {
                semaphor.WaitOne();
                //Type
                byte [] buffer = new byte [4];
                buffer = BitConverter.GetBytes((int)cmd.CommandType);
                this.networkStream.Write(buffer , 0 , 4);
                this.networkStream.Flush();

                //Sender IP
                byte [] senderIPBuffer = Encoding.ASCII.GetBytes(cmd.SenderIP.ToString());
                buffer = new byte [4];
                buffer = BitConverter.GetBytes(senderIPBuffer.Length);
                this.networkStream.Write(buffer , 0 , 4);
                this.networkStream.Flush();
                this.networkStream.Write(senderIPBuffer , 0 , senderIPBuffer.Length);
                this.networkStream.Flush();

                //Sender Name
                byte [] senderNameBuffer = Encoding.Unicode.GetBytes(cmd.SenderName.ToString());
                buffer = new byte [4];
                buffer = BitConverter.GetBytes(senderNameBuffer.Length);
                this.networkStream.Write(buffer , 0 , 4);
                this.networkStream.Flush();
                this.networkStream.Write(senderNameBuffer , 0 , senderNameBuffer.Length);
                this.networkStream.Flush();

                //Target
                byte [] ipBuffer = Encoding.ASCII.GetBytes(cmd.Target.ToString());
                buffer = new byte [4];
                buffer = BitConverter.GetBytes(ipBuffer.Length);
                this.networkStream.Write(buffer , 0 , 4);
                this.networkStream.Flush();
                this.networkStream.Write(ipBuffer , 0 , ipBuffer.Length);
                this.networkStream.Flush();

                //Meta Data.
                if ( cmd.MetaData == null || cmd.MetaData == "" )
                    cmd.MetaData = "\n";

                byte [] metaBuffer = Encoding.Unicode.GetBytes(cmd.MetaData);
                buffer = new byte [4];
                buffer = BitConverter.GetBytes(metaBuffer.Length);
                this.networkStream.Write(buffer , 0 , 4);
                this.networkStream.Flush();
                this.networkStream.Write(metaBuffer , 0 , metaBuffer.Length);
                this.networkStream.Flush();

                semaphor.Release();
                return true;
            }
            catch
            {
                semaphor.Release();
                return false;
            }
        }
Exemplo n.º 11
0
 /// <summary>
 /// Sends a command to the remote client if the connection is alive.
 /// </summary>
 /// <param name="cmd">The command to send.</param>
 public void SendCommand(Command cmd)
 {
     if ( this.socket != null && this.socket.Connected )
     {
         BackgroundWorker bwSender = new BackgroundWorker();
         bwSender.DoWork += new DoWorkEventHandler(bwSender_DoWork);
         bwSender.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwSender_RunWorkerCompleted);
         bwSender.RunWorkerAsync(cmd);
     }
     else
         this.OnCommandFailed(new EventArgs());
 }
Exemplo n.º 12
0
 private void BroadCastCommand(Command cmd)
 {
     foreach ( ClientManager mngr in this.clients )
         if ( !mngr.IP.Equals(cmd.SenderIP) )
             mngr.SendCommand(cmd);
 }
Exemplo n.º 13
0
 private void SendExistanceCommand(IPAddress targetIP , bool isExists)
 {
     Command cmdExistance = new Command(CommandType.IsNameExists , targetIP , isExists.ToString());
     cmdExistance.SenderIP = this.serverIP;
     cmdExistance.SenderName = "Server";
     this.SendCommandToTarget(cmdExistance);
 }
Exemplo n.º 14
0
 private void SendCommandToTarget(Command cmd)
 {
     foreach ( ClientManager mngr in this.clients )
         if ( mngr.IP.Equals(cmd.Target) )
         {
             mngr.SendCommand(cmd);
             return;
         }
 }
Exemplo n.º 15
0
 private void SendClientListToNewClient(IPAddress newClientIP)
 {
     foreach ( ClientManager mngr in this.clients )
     {
         if ( mngr.Connected && !mngr.IP.Equals(newClientIP) )
         {
             Command cmd = new Command(CommandType.SendClientList , newClientIP);
             cmd.MetaData = mngr.IP.ToString() + ":" + mngr.ClientName;
             cmd.SenderIP = this.serverIP;
             cmd.SenderName = "Server";
             this.SendCommandToTarget(cmd);
         }
     }
 }
Exemplo n.º 16
0
 private void SendExistanceCommand(IPAddress targetIP, bool isExists)
 {
     var cmdExistance = new Command(CommandType.IsNameExists, targetIP, isExists.ToString())
     {
         SenderIP = _serverIp,
         SenderName = "Server"
     };
     SendCommandToTarget(cmdExistance);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Sends a command to the remote client if the connection is alive.
 /// </summary>
 /// <param name="cmd">The command to send.</param>
 public void SendCommand(Command cmd)
 {
     if (_socket != null && _socket.Connected)
     {
         var bwSender = new BackgroundWorker();
         bwSender.DoWork += BwSenderDoWork;
         bwSender.RunWorkerCompleted += BwSenderRunWorkerCompleted;
         bwSender.RunWorkerAsync(cmd);
     }
     else
     {
         OnCommandFailed(new EventArgs());
     }
 }
Exemplo n.º 18
0
 /// <summary>
 /// Creates an instance of CommandEventArgs class.
 /// </summary>
 /// <param name="cmd">The received command.</param>
 public CommandEventArgs(Command cmd)
 {
     this.command = cmd;
 }