Esempio n. 1
0
 private void HandleClient()
 {
     try
     {
         var auth = new AuthenticationServer(_chatserver, _client);
         if (!auth.Authenticate())
         {
             throw new Exception();
         }
         _chatserver.Write(_client.GetStream(),
                           ChatProtocolValues.CONNECTION_MSG(auth.UserId));
         Nickname = auth.UserId;
         Room     = _chatserver.AssignRoom(Nickname);
         Console.WriteLine(Resources.RoomAssigned + Room + Resources.For + Nickname);
         try
         {
             _chatserver.AddConnection(Nickname, _client);
         }
         catch
         {
             // ignored
         }
         _chatserver.Broadcast(ChatProtocolValues.Welcome(Nickname, Room), Room);
         while ((_readdata = _chatserver.Read(_client.GetStream())) != "")
         {
             _readdata = _readdata.Trim();
             Console.WriteLine(Resources.Read + _readdata);
             if (_readdata.ToUpper().Substring(0, 1) == ChatProtocolValues.IsCmd)
             {
                 _action = action_default;
                 if (
                     (_readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GetPicCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_get_pic;
                 }
                 if (
                     (_readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SendPicCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_send_pic;
                 }
                 if (
                     (_readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GetMediaCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_get_media;
                 }
                 if (
                     (_readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SendMediaCmd,
                                                         StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_send_media;
                 }
                 if ((_readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.HelpCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_help;
                 }
                 if ((_readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.QuitCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_quit;
                 }
                 if (
                     (_readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.ChangeRoomCmd,
                                                         StringComparison.Ordinal) == 0)
                 {
                     _action = action_change_room;
                 }
                 if (
                     (_readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.WhichRoomCmd,
                                                         StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_which_room;
                 }
                 if ((_readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.ListCmd, StringComparison.Ordinal) ==
                     0)
                 {
                     _action = action_list;
                 }
                 if (
                     (_readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.PrivateMsgCmd,
                                                         StringComparison.Ordinal) == 0)
                 {
                     _action = action_private_message;
                 }
             }
             else
             {
                 _action = action_send_message;
             }
             _action();
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(Resources.ErrorFromServer);
         Console.WriteLine(Resources.Stars);
         Console.WriteLine(e);
         Console.WriteLine(Resources.Stars);
         Console.WriteLine(Resources.WaitingForConnection);
     }
     finally
     {
         try
         {
             _chatserver.Write(_client.GetStream(), ChatProtocolValues.QuitMsg);
         }
         catch
         {
             // ignored
         }
         if (Room != 0 && Nickname != "")
         {
             _chatserver.RemoveRoomUser(_chatserver.RoomUsers[Room - 1], Nickname);
             _chatserver.Broadcast(ChatProtocolValues.USER_LOG_OUT(Nickname, Room));
         }
         try
         {
             _chatserver.RemoveConnection(Nickname, _client);
         }
         catch
         {
             // ignored
         }
     }
 }
Esempio n. 2
0
        //The main method that handle all the chat communication with the client
        private void HandleClient()
        {
            try
            {
                //Autheticate the user
                AuthenticationServer auth = new AuthenticationServer(chatserver, client);
                if (!auth.Authenticate())
                {
                    throw(new Exception());
                }

                //Send connection message to client
                //returning the user id
                chatserver.Write(client.GetStream(),
                                 ChatProtocolValues.CONNECTION_MSG(auth.UserID));

                //buffer
                byte[] bytes = new byte[256];

                //set nickname as user id which is alraedy unique
                nickname = auth.UserID;

                //Assign a room to connected client
                room = chatserver.AssignRoom(nickname);
                Console.WriteLine("room assigned= " + room + " for " + nickname);

                //Add to Connections
                try
                {
                    chatserver.AddConnection(nickname, client);
                }
                catch {}

                //Broadcast to chat room about the new user
                chatserver.Broadcast(ChatProtocolValues.WELCOME(nickname, room), room);

                //listen to all client data send
                while ((readdata = chatserver.Read(client.GetStream())) != "")
                {
                    readdata = readdata.Trim();

                    //Print out read data in console
                    Console.WriteLine("Read>" + readdata);

                    //Check if the readdata is a command
                    if (readdata.ToUpper().Substring(0, 1) == ChatProtocolValues.IS_CMD)
                    {
                        //Assign a default action
                        action = new SocketHelperAction(action_default);

                        //Reassign action based on format content
                        if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GET_PIC_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_get_pic);
                        }

                        if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SEND_PIC_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_send_pic);
                        }

                        if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.GET_MEDIA_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_get_media);
                        }

                        if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.SEND_MEDIA_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_send_media);
                        }


                        if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.HELP_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_help);
                        }

                        if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.QUIT_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_quit);
                        }

                        if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.CHANGE_ROOM_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_change_room);
                        }

                        if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.WHICH_ROOM_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_which_room);
                        }

                        if ((readdata.ToUpper() + " ").IndexOf(ChatProtocolValues.LIST_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_list);
                        }

                        if ((readdata.ToUpper() + ":").IndexOf(ChatProtocolValues.PRIVATE_MSG_CMD) == 0)
                        {
                            action = new SocketHelperAction(action_private_message);
                        }
                    }
                    else                     //NON COMMAND
                    {
                        //if not a command assign to a mesage sending action
                        action = new SocketHelperAction(action_send_message);
                    }                     //COMMANDS

                    //perform the action
                    action();
                }                //WHILE
            }
            catch (Exception e)
            {
                //Trapped exception
                Console.WriteLine("The following error is trapped by the chat server");
                Console.WriteLine("*************************************************");
                Console.WriteLine(e);
                Console.WriteLine("*************************************************");
                Console.WriteLine("Waiting for Connection...");
            }
            finally
            {
                //while loop ended or when there are some other problems
                //try to inform client to shut down
                try
                {
                    chatserver.Write(client.GetStream(), ChatProtocolValues.QUIT_MSG);
                }
                catch {}

                //if the client had belong to a room
                if ((room != 0) && (nickname != ""))
                {
                    //remove user from room
                    chatserver.RemoveRoomUser(chatserver.RoomUsers[room - 1], nickname);
                    //inform all that the client has logged out
                    chatserver.Broadcast(ChatProtocolValues.USER_LOG_OUT(nickname, room));
                }

                //remove the client connection
                try
                {
                    chatserver.RemoveConnection(nickname, client);
                }
                catch {}
            }
        }