예제 #1
0
        /// <summary>
        /// Parse client request
        /// </summary>
        /// <param name="state"></param>
        /// <param name="handlerSocket"></param>
        private void ParseRequest(ChatHelper.StateObject state, StreamSocket handlerSocket)
        {
            var data = new Data(state.Buffer);
            if (data.Command == Command.Disconnect)
            {
                DisconnectClient(handlerSocket);
                return;
            }

            var clientStr = clients.FirstOrDefault(cl => cl.UserName == data.To);
            if (clientStr == null) 
                return;

            var writer = new DataWriter(clientStr.Connection.OutputStream);
            writer.WriteBytes(data.ToByte());

        }
예제 #2
0
 /// <summary>
 /// Notify connecting user that specified name already exist
 /// </summary>
 /// <param name="clientSocket"></param>
 /// <param name="name"></param>
 public void SendNameAlreadyExist(StreamSocket clientSocket, string name)
 {
     var data = new Data {Command = Command.NameExist, To = name};
     var dataWriter = new DataWriter(clientSocket.OutputStream);
     dataWriter.WriteBytes(data.ToByte());
 }
예제 #3
0
        /// <summary>
        /// Broadcast clients number changed
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="userName"></param>
        /// <param name="changedUser"></param>
        /// <param name="state"></param>
        public async void SendUsersList(StreamSocket 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)
            };
            var dataWriter = new DataWriter(clientSocket.OutputStream);
            var bytes = data.ToByte();

            dataWriter.WriteInt32(bytes.Length);
            dataWriter.WriteBytes(bytes);
            await dataWriter.StoreAsync();
        }
예제 #4
0
 /// <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());
 }
예제 #5
0
 /// <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.InputStream);
         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);
 }
예제 #6
0
 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);
     }
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #7
0
 /// <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());
 }
예제 #8
0
 /// <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};
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #9
0
 /// <summary>
 /// Closes server connection
 /// </summary>
 public void CloseConnection()
 {
     IsConnected = false;
     var data = new Data {Command = Command.Disconnect};
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #10
0
 /// <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};
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #11
0
 /// <summary>
 /// Send file to recipient
 /// </summary>
 /// <param name="file"></param>
 /// <param name="recipient"></param>
 /// <param name="fileName"></param>
 public void SendFile(byte[] file, string recipient, string fileName)
 {
     var data = new Data {Command = Command.SendFile, File = file, Message = fileName, To = recipient, From = UserName};
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #12
0
 /// <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};
     serverWriter.WriteBytes(data.ToByte());
 }
예제 #13
0
 /// <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;
     }
 }
예제 #14
0
        private async Task ReceiveUsersList()
        {
            string[] serverMessage = null;

            while (true)
            {

                uint sizeFieldCount = await serverReader.LoadAsync(sizeof(int));

                if (sizeFieldCount != sizeof(int))
                {
                    return;
                }

                var bytesLength = serverReader.ReadInt32();
                uint actualBytesLength = await serverReader.LoadAsync((uint)bytesLength);
                if (bytesLength != actualBytesLength)
                {
                    // The underlying socket was closed before we were able to read the whole data.
                    return;
                }

                var bytes = new byte[serverReader.UnconsumedBufferLength];
                serverReader.ReadBytes(bytes);
                var data = new Data(bytes);

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

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

                break;
            }

            if (serverMessage == null)
                return;

            ServerName = serverMessage[3];
            
            OnUserListReceived(serverMessage);

        }