/// <summary>
 /// Broadcast clients number changed
 /// </summary>
 /// <param name="clientSocket"></param>
 /// <param name="userName"></param>
 /// <param name="changedUser"></param>
 /// <param name="state"></param>
 public void SendUsersList(Socket clientSocket, string userName, string changedUser, string state)
 {
     var data = new Data
     {
         Command = Command.Broadcast,
         To = userName,
         Message = string.Format("{0}|{1}|{2}|{3}",
             string.Join(",", clients.Select(u => u.UserName).Where(name => name != userName)), changedUser, state,
             serverName)
     };
     clientSocket.Send(data.ToByte());
 }
 /// <summary>
 /// Parse client request
 /// </summary>
 /// <param name="state"></param>
 /// <param name="handlerSocket"></param>
 private void ParseRequest(ChatHelper.StateObject state, Socket handlerSocket)
 {
     var data = new Data(state.Buffer);
     if (data.Command == Command.Disconnect)
     {
         DisconnectClient(state.WorkSocket);
         return;
     }
     var clientStr = clients.FirstOrDefault(cl => cl.UserName == data.To);
     if (clientStr == null)
         return;
     clientStr.Connection.Send(data.ToByte());
     handlerSocket.BeginReceive(state.Buffer, 0, ChatHelper.StateObject.BUFFER_SIZE, 0,
       OnReceive, state);
 }
        private bool ReceiveUsersList()
        {
            var bytes = new byte[1024];
            server.Client.Receive(bytes);
            var data = new Data(bytes);

            var serverMessage = data.Message.Split(new[] { '|' }, StringSplitOptions.None);

            if (data.Command == Command.NameExist)
            {
                MessageBox.Show(string.Format("Name \"{0}\" already exist on server", serverMessage[1]));
                return false;
            }

            ServerName = serverMessage[3];

            OnUserListReceived(serverMessage);

            return true;
        }
 /// <summary>
 /// Notify connecting user that specified name already exist
 /// </summary>
 /// <param name="clientSocket"></param>
 /// <param name="name"></param>
 public void SendNameAlreadyExist(Socket clientSocket, string name)
 {
     var data = new Data {Command = Command.NameExist, To = name};
     clientSocket.Send(data.ToByte());
 }
 /// <summary>
 /// Send message
 /// </summary>
 /// <param name="message"></param>
 /// <param name="recipient"></param>
 public void SendMessage(string message,string recipient)
 {
     var data = new Data {Command = Command.SendMessage, To = recipient, From = UserName, Message = message};
     server.Client.Send(data.ToByte());
 }
 /// <summary>
 /// Send call response to caller
 /// </summary>
 /// <param name="response"></param>
 public void SendResponse(Command response)
 {
     var data = new Data {Command = response, To = udpSubscriber, From = UserName, ClientAddress = ClientAddress};
     server.Client.Send(data.ToByte());
 }
 public void SendFile(byte[] file, string recipient, string fileName)
 {
     var data = new Data {Command = Command.SendFile, File = file, Message = fileName, To = recipient, From = UserName};
     server.Client.Send(data.ToByte());
 }
 /// <summary>
 /// Call to user
 /// </summary>
 /// <param name="recipient"></param>
 public void SendChatRequest(string recipient)
 {
     var data = new Data {Command = Command.Call, To = recipient, From = UserName, ClientAddress = ClientAddress};
     server.Client.Send(data.ToByte());
 }
 /// <summary>
 /// Parse received message
 /// </summary>
 /// <param name="data"></param>
 public void ParseMessage(Data data)
 {
     switch (data.Command)
     {
         case Command.SendMessage:
             OnMessageReceived(data.Message, data.From);
         break;
         case Command.SendFile:
             OnFileRecieved(data.File, data.From, data.Message);
         break;
         case Command.Broadcast:
             OnUserListReceived(data.Message.Split('|'));
         break;
         case Command.Call:
             if (!udpConnectionActive)
                 OnCallRecieved(data.From, data.ClientAddress);
             SendResponse(Command.Busy);
         break;
         case Command.AcceptCall:
         case Command.CancelCall:
         case Command.EndCall:
         case Command.Busy:
             ParseResponse(data.From, data.Command, data.ClientAddress);
             OnCallResponseReceived(data.Command);
         break;
     }
 }
        /// <summary>
        /// Closes server connection
        /// </summary>
        public void CloseConnection()
        {
            IsConnected = false;

            var data = new Data {Command = Command.Disconnect};
            if (server.Client.Connected)
                server.Client.Send(data.ToByte());
        }
 public void AnswerIncomingCall(string caller, string address, Command answer)
 {
     var data = new Data {Command = answer, From = UserName, To = caller, ClientAddress = ClientAddress};
     if (answer == Command.AcceptCall)
     {
         udpSubscriber = caller;
         StartVoiceChat(address);
     }
     server.Client.Send(data.ToByte());
 }