diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/ChatRoom.cpp | 7 | ||||
-rw-r--r-- | src/server/ChatRoom_cmd.cpp | 32 | ||||
-rw-r--r-- | src/server/ChatServer.cpp | 65 | ||||
-rw-r--r-- | src/server/ChatServer.hpp | 6 | ||||
-rw-r--r-- | src/server/Makefile | 2 | ||||
-rw-r--r-- | src/server/UserInfo.cpp | 7 | ||||
-rw-r--r-- | src/server/UserInfo.hpp | 16 | ||||
-rw-r--r-- | src/server/UserInfo_io.cpp | 27 |
8 files changed, 124 insertions, 38 deletions
diff --git a/src/server/ChatRoom.cpp b/src/server/ChatRoom.cpp index 9d99251..f3efe96 100644 --- a/src/server/ChatRoom.cpp +++ b/src/server/ChatRoom.cpp @@ -41,7 +41,7 @@ void ChatRoom::HandleMessage(UserInfo *u, const char *str) char *msg = new char[max_msg_len]; sprintf(msg, "%s: %s", u->GetName(), str); - users->SendAllUsers(msg, 0, usual_msg); + users->SendAllUsers(msg, 0, USUAL_CHAR); delete[] msg; } @@ -58,6 +58,8 @@ const char *ChatRoom::GetSecretPass() void ChatRoom::AddSession(UserInfo *u) { users->AddUser(u); + users->SendAllUsersOnline(RONLINE_CHAR); + users->SendAllUsersName(); if(code == std_id_lobby) return; @@ -92,6 +94,8 @@ void ChatRoom::RemoveSession(UserInfo *u) } users->RemoveUser(u); + users->SendAllUsersOnline(RONLINE_CHAR); + users->SendAllUsersName(); if(code != std_id_lobby && users->GetOnline() < 1) the_server->DeleteRoom(code); } @@ -140,6 +144,7 @@ void ChatRoom::Identification(UserInfo *u, const char *str) } // query to server + users->SendAllUsersName(); delete[] msg; delete ans; } diff --git a/src/server/ChatRoom_cmd.cpp b/src/server/ChatRoom_cmd.cpp index 8b1f6e9..87d6716 100644 --- a/src/server/ChatRoom_cmd.cpp +++ b/src/server/ChatRoom_cmd.cpp @@ -23,16 +23,18 @@ const int cmd_id_create = 1; const int cmd_id_join = 2; const int cmd_id_exit = 3; const int cmd_id_rooms = 4; +const int cmd_id_prev = 5; +const int cmd_id_next = 6; -const int cmd_count = 5; +const int cmd_count = 7; const struct cmd_info cmd[cmd_count] = { {cmd_id_help, "/help", ChatRoom::Hash("/help"), USE_ANYWHERE, 0, "Usage: /help"}, {cmd_id_create, "/create", ChatRoom::Hash("/create"), USE_IN_LOBBY, 0, "Usage: /create [pass-key]"}, {cmd_id_join, "/join", ChatRoom::Hash("/join"), USE_IN_LOBBY, 1, "Usage: /join *id* [pass-key]"}, {cmd_id_exit, "/exit", ChatRoom::Hash("/exit"), USE_IN_ROOM, 0, "Usage: /exit"}, - {cmd_id_rooms, "/rooms", ChatRoom::Hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"} // print all public rooms - - // IDEA: /clear - clear screen + {cmd_id_rooms, "/rooms", ChatRoom::Hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"}, // print all public rooms + {cmd_id_prev, "/prev", ChatRoom::Hash("/prev"), USE_ANYWHERE, 0, "Usage: /prev"}, + {cmd_id_next, "/next", ChatRoom::Hash("/next"), USE_ANYWHERE, 0, "Usage: /next"} }; void ChatRoom::HandleCommand(UserInfo *u, int count, @@ -117,6 +119,20 @@ void ChatRoom::HandleCommand(UserInfo *u, int count, // in development break; } + case cmd_id_prev: { + int user_list = u->GetUserList(); + if(user_list > 1) { + u->SetUserList(user_list-1); + users->SendUsersNameTo(u); + } + break; + } + case cmd_id_next: { + int user_list = u->GetUserList(); + u->SetUserList(user_list+1); + users->SendUsersNameTo(u); + break; + } default: break; } } @@ -141,21 +157,19 @@ char **ChatRoom::ParseToArg(const char *input, int &arrc) int start = 0; for(int i = 0; i < (int) strlen(input)+1; i++) { if(input[i] == ' ' || input[i] == '\0' || input[i] == '\n') { // end - if(start == std_id_lobby) - continue; + /*if(code == std_id_lobby) + continue;*/ int size = i - start; arr[arrc] = new char[size+1]; memcpy(arr[arrc], input + start, sizeof(char) * size); arr[arrc][size] = '\0'; - start = -1; + start = i+1; arrc++; if(arrc == max_argv) break; } - else if(start == -1) - start = i; } arr[arrc] = 0; diff --git a/src/server/ChatServer.cpp b/src/server/ChatServer.cpp index 7b6a9ea..b128756 100644 --- a/src/server/ChatServer.cpp +++ b/src/server/ChatServer.cpp @@ -117,6 +117,7 @@ void ChatServer::GotoLobby(ChatRoom *cur_room, UserInfo *u) { cur_room->RemoveSession(u); lobby->AddSession(u); + u->SetUserList(1); u->SetRoom(lobby); } @@ -146,6 +147,9 @@ void ChatServer::CloseConnection(UserInfo *u) talkers->RemoveUser(u); the_selector->Remove(u); delete u; + + talkers->SendAllUsersOnline(GONLINE_CHAR); + talkers->SendAllUsersName(); } void ChatServer::Handle(bool r, bool w) @@ -160,9 +164,10 @@ void ChatServer::Handle(bool r, bool w) return; UserInfo *u = new UserInfo(lobby, sd); - talkers->AddUser(u); - lobby->AddSession(u); the_selector->Add(u); + lobby->AddSession(u); // call SendAllUsersName in lobby + talkers->AddUser(u); + talkers->SendAllUsersOnline(GONLINE_CHAR); u->Send("Welcome to WantChat! What is your name?"); } @@ -179,13 +184,63 @@ StorageOfUsers::~StorageOfUsers() } void StorageOfUsers::SendAllUsers(const char *msg, UserInfo *except, - const int spec_msg) + const char spec_ch) { - CONSOLE_LOG("Send message all: %s\n", msg); + //CONSOLE_LOG("Send message all: %s\n", msg); item *p; for(p = first; p; p = p->next) if(p->u != except) - p->u->Send(msg, spec_msg); + p->u->Send(msg, spec_ch); +} + +void StorageOfUsers::SendAllUsersOnline(const char spec_ch) +{ + if(spec_ch != GONLINE_CHAR && spec_ch != RONLINE_CHAR) + return; + + char *temp = new char[6]; + sprintf(temp, "%d", online); + SendAllUsers(temp, 0, spec_ch); + delete[] temp; +} + +void StorageOfUsers::SendAllUsersName() +{ + item *p; + for(p = first; p; p = p->next) + SendUsersNameTo(p->u); +} + +void StorageOfUsers::SendUsersNameTo(UserInfo *u) +{ + int num = u->GetUserList()*9 - 9; + while(num > online) { + num -= 9; + u->SetUserList(num/9); + } + + char *buff_name = new char[(max_name_len+1)*9+1]; + strcpy(buff_name, ""); + + item *s; + int jmp = 1, n = 1; + for(s = first; s; s = s->next) { + if(jmp <= num) { + jmp++; + continue; + } + if(n > 9) + break; + + const char *name = s->u->GetName(); + if(strcmp(name, STANDARD_USERNAME) != 0) { + strcat(buff_name, name); + strcat(buff_name, ";"); + n++; + } + } + u->Send(buff_name, USERS_CHAR); + delete[] buff_name; } UserInfo *StorageOfUsers::SearchUserByName(const char *name) diff --git a/src/server/ChatServer.hpp b/src/server/ChatServer.hpp index f966e84..247a1bc 100644 --- a/src/server/ChatServer.hpp +++ b/src/server/ChatServer.hpp @@ -29,7 +29,11 @@ public: ~StorageOfUsers(); void SendAllUsers(const char *msg, UserInfo *except = 0, - const int spec_msg = system_msg); + const char spec_ch = SYSTEM_CHAR); + void SendAllUsersOnline(const char spec_ch); + + void SendAllUsersName(); + void SendUsersNameTo(UserInfo *u); UserInfo *SearchUserByName(const char *name); void AddUser(UserInfo *u); diff --git a/src/server/Makefile b/src/server/Makefile index 42d8361..607a7ed 100644 --- a/src/server/Makefile +++ b/src/server/Makefile @@ -1,5 +1,5 @@ CPP = g++ -CPPFLAGS = -Wall -g $(shell mariadb_config --cflags --libs) +CPPFLAGS = -Wall -g3 $(shell mariadb_config --cflags --libs) SOURCES = main.cpp sockets.cpp ChatServer.cpp UserInfo.cpp UserInfo_io.cpp ChatRoom.cpp ChatRoom_cmd.cpp database.cpp OBJECTS = $(SOURCES:.cpp=.o) EXECUTABLE = server diff --git a/src/server/UserInfo.cpp b/src/server/UserInfo.cpp index e0239eb..27a6aa2 100644 --- a/src/server/UserInfo.cpp +++ b/src/server/UserInfo.cpp @@ -2,7 +2,7 @@ void UserInfo::SetRoom(ChatRoom *new_master) { - this->the_master = new_master; + the_master = new_master; } const char *UserInfo::GetName() const @@ -29,3 +29,8 @@ void UserInfo::SetStatus(const enum_status n_status) { state = n_status; } + +void UserInfo::SetUserList(int list) +{ + u_list = list; +} diff --git a/src/server/UserInfo.hpp b/src/server/UserInfo.hpp index efe6d5e..b9f851b 100644 --- a/src/server/UserInfo.hpp +++ b/src/server/UserInfo.hpp @@ -7,6 +7,9 @@ #include "ChatRoom.hpp" #include "../const_vars.hpp" +#define STANDARD_USERNAME "0" +#define STANDARD_USERPASS "0" + class ChatServer; enum enum_status { @@ -26,13 +29,15 @@ class UserInfo : FdHandler { char buffer[max_msg_len]; int buf_used; bool ignoring; + int u_list; // user list for show players (see cmd: /prev /next) enum_status state; ChatRoom *the_master; - UserInfo(ChatRoom *i_master, int i_fd) : FdHandler(i_fd), name("0"), - password("0"), buf_used(0), ignoring(false), state(wait_name), - the_master(i_master) {} + UserInfo(ChatRoom *i_master, int i_fd) : FdHandler(i_fd), + name(STANDARD_USERNAME), password(STANDARD_USERPASS), + buf_used(0), ignoring(false), u_list(1), + state(wait_name), the_master(i_master) {} ~UserInfo() {} void SetRoom(ChatRoom *new_master); @@ -43,7 +48,7 @@ class UserInfo : FdHandler { void ReadAndCheck(); void CheckLines(); public: - void Send(const char *msg, const int spec_msg = system_msg); + void Send(const char *msg, const char spec_ch = SYSTEM_CHAR); // === realization in UserInfo_io.cpp === const char *GetName() const; @@ -54,6 +59,9 @@ public: enum_status GetStatus() const { return state; } void SetStatus(const enum_status n_status); + + int GetUserList() const { return u_list; } + void SetUserList(int list); }; #endif
\ No newline at end of file diff --git a/src/server/UserInfo_io.cpp b/src/server/UserInfo_io.cpp index d4d308a..b2f6acd 100644 --- a/src/server/UserInfo_io.cpp +++ b/src/server/UserInfo_io.cpp @@ -13,7 +13,7 @@ void UserInfo::Handle(bool r, bool w) ignoring = true; } if(ignoring) { - CONSOLE_LOG("Ignore the message, it's so big\n"); + //CONSOLE_LOG("Ignore the message, it's so big\n"); ReadAndIgnore(); } else @@ -23,7 +23,7 @@ void UserInfo::Handle(bool r, bool w) void UserInfo::ReadAndIgnore() { int rc = read(GetFd(), buffer, sizeof(buffer)); - CONSOLE_LOG("readI return %d bytes\n", rc); + //CONSOLE_LOG("readI return %d bytes\n", rc); if(rc < 1) { the_master->CloseSession(this); return; @@ -32,7 +32,7 @@ void UserInfo::ReadAndIgnore() for(int i = 0; i < rc; i++) if(buffer[i] == '\n') { // stop ignoring! - CONSOLE_LOG("ReadAndIgnore: find \\n\n"); + //CONSOLE_LOG("ReadAndIgnore: find \\n\n"); int rest = rc - i - 1; if(rest > 0) memmove(buffer, buffer + i + 1, rest); @@ -61,39 +61,34 @@ void UserInfo::CheckLines() for(int i = 0; i < buf_used; i++) { if(buffer[i] == '\n') { - CONSOLE_LOG("[CheckLines] buffer[i] == \\n i = %d\n", i); + //CONSOLE_LOG("[CheckLines] buffer[i] == \\n i = %d\n", i); buffer[i] = 0; if(i > 0 && buffer[i-1] == '\r') buffer[i-1] = 0; - CONSOLE_LOG("printed: %s\n", buffer); + //CONSOLE_LOG("printed: %s\n", buffer); the_master->HandleMessage(this, buffer); int rest = buf_used - i - 1; memmove(buffer, buffer + i + 1, rest); buf_used = rest; - CONSOLE_LOG("[CheckLines] new buf_used = %d\n", buf_used); + //CONSOLE_LOG("[CheckLines] new buf_used = %d\n", buf_used); CheckLines(); return; } } } -void UserInfo::Send(const char *msg, const int spec_msg) +void UserInfo::Send(const char *msg, const char spec_ch) { - int len = strlen(msg); - char *tmp_msg = new char[len+1+2]; // for spec_symb + \n + char *tmp_msg = new char[1+strlen(msg)+2]; // for spec_symb + \n - if(spec_msg == usual_msg) + if(spec_ch == USUAL_CHAR) sprintf(tmp_msg, "%s\n", msg); else - { - char spec; - if(spec_msg == system_msg) spec = system_char; - sprintf(tmp_msg, "%c%s\n", spec, msg); - } + sprintf(tmp_msg, "%c%s\n", spec_ch, msg); - CONSOLE_LOG("%s", tmp_msg); + CONSOLE_LOG("send: %s", tmp_msg); write(GetFd(), tmp_msg, strlen(tmp_msg)); delete[] tmp_msg; |