summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2020-12-02 17:59:46 +0000
committerJoursoir <chat@joursoir.net>2020-12-02 17:59:46 +0000
commitf5a7c51c89567abc9b9c6b974a65e24ea616e1f4 (patch)
tree07d4aeacdeb80447c85a29f168fb1ddb48fea41c
parentad0a2dc909fbb772d42ab6faf13bba74fbc2483d (diff)
downloadwant-chat-f5a7c51c89567abc9b9c6b974a65e24ea616e1f4.tar.gz
want-chat-f5a7c51c89567abc9b9c6b974a65e24ea616e1f4.tar.bz2
want-chat-f5a7c51c89567abc9b9c6b974a65e24ea616e1f4.zip
refactor code; add class storing users
-rw-r--r--src/server/ChatRoom.cpp76
-rw-r--r--src/server/ChatRoom.hpp22
-rw-r--r--src/server/ChatServer.cpp73
-rw-r--r--src/server/ChatServer.hpp35
4 files changed, 129 insertions, 77 deletions
diff --git a/src/server/ChatRoom.cpp b/src/server/ChatRoom.cpp
index a78670c..2d368e3 100644
--- a/src/server/ChatRoom.cpp
+++ b/src/server/ChatRoom.cpp
@@ -11,10 +11,6 @@
#define USE_IN_LOBBY 1
#define USE_ANYWHERE 2
-static char **ParseToArg(const char *input, int &arrc);
-static unsigned long hash(const char *str);
-static bool checkForbiddenSymbols(const char *str);
-
struct cmd_info {
int id; // never -1 !!!
char name[15];
@@ -32,18 +28,19 @@ const int cmd_id_rooms = 4;
const int cmd_count = 5;
const struct cmd_info cmd[cmd_count] = {
- {cmd_id_help, "/help", hash("/help"), USE_ANYWHERE, 0, "Usage: /help"},
- {cmd_id_create, "/create", hash("/create"), USE_IN_LOBBY, 0, "Usage: /create [pass-key]"},
- {cmd_id_join, "/join", hash("/join"), USE_IN_LOBBY, 1, "Usage: /join *id* [pass-key]"},
- {cmd_id_exit, "/exit", hash("/exit"), USE_IN_ROOM, 0, "Usage: /exit"},
- {cmd_id_rooms, "/rooms", hash("/rooms"), USE_IN_LOBBY, 0, "Usage: /rooms"} // print all public rooms
+ {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
};
ChatRoom::ChatRoom(ChatServer *i_server, int id, char *pass)
- : the_server(i_server), code(id), first(0)
+ : the_server(i_server), code(id)
{
+ users = new StorageOfUsers();
if(pass)
strcpy(secret_pass, pass);
else secret_pass[0] = 0;
@@ -51,22 +48,11 @@ ChatRoom::ChatRoom(ChatServer *i_server, int id, char *pass)
ChatRoom::~ChatRoom()
{
- while(first) {
- item *tmp = first;
- first = first->next;
- the_server->CloseConnection(tmp->u);
- delete tmp;
- }
-}
-
-void ChatRoom::SendAll(const char *msg, UserInfo *except,
- const int spec_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);
+ UserInfo *u = 0;
+ while((u = users->Disconnect())) {
+ the_server->CloseConnection(u);
+ }
+ delete users;
}
void ChatRoom::HandleMessage(UserInfo *u, const char *str)
@@ -141,7 +127,7 @@ void ChatRoom::HandleMessage(UserInfo *u, const char *str)
char *msg = new char[max_msg_len];
sprintf(msg, "%s: %s", u->GetName(), str);
- this->SendAll(msg, 0, usual_msg);
+ users->SendAllUsers(msg, 0, usual_msg);
delete[] msg;
}
@@ -215,10 +201,11 @@ void ChatRoom::HandleCommand(UserInfo *u, int count,
else if(h_status == enter_uncorrect_pass)
return u->Send("Oops, this password is not valid");
- u->Send("You has left the room");
+ u->Send("You has entered the room");
break;
}
case cmd_id_exit: {
+ u->Send("You has left the room");
the_server->GotoLobby(this, u);
break;
}
@@ -239,11 +226,7 @@ const char *ChatRoom::GetSecretPass()
void ChatRoom::AddSession(UserInfo *u)
{
- item *p = new item;
- p->next = first;
- p->u = u;
- first = p;
-
+ users->AddUser(u);
if(code == std_id_lobby)
return;
@@ -259,7 +242,7 @@ void ChatRoom::AddSession(UserInfo *u)
char *emsg = new char[strlen(name) + sizeof(entered_msg)];
sprintf(emsg, "%s%s", name, entered_msg);
- this->SendAll(emsg, u);
+ users->SendAllUsers(emsg, u);
delete[] emsg;
}
@@ -273,24 +256,13 @@ void ChatRoom::RemoveSession(UserInfo *u)
int len = strlen(name);
char *lmsg = new char[len + sizeof(left_msg) + 2];
sprintf(lmsg, "%s%s", name, left_msg);
- this->SendAll(lmsg, u);
+ users->SendAllUsers(lmsg, u);
delete[] lmsg;
}
- item **p;
- for(p = &first; *p; p = &((*p)->next)) {
- if( ((*p)->u) == u ) {
- item *tmp = *p;
- *p = tmp->next;
- // not delete UserInfo!
- delete tmp;
-
- if(code != std_id_lobby && !first)
- the_server->DeleteRoom(code);
-
- return;
- }
- }
+ users->RemoveUser(u);
+ if(code != std_id_lobby && users->GetOnline() < 1)
+ the_server->DeleteRoom(code);
}
void ChatRoom::CloseSession(UserInfo *u)
@@ -302,7 +274,7 @@ void ChatRoom::CloseSession(UserInfo *u)
//////////////////////////////////////////////////////////////
-static char **ParseToArg(const char *input, int &arrc)
+char **ChatRoom::ParseToArg(const char *input, int &arrc)
{
int max_argv = 5;
arrc = 0;
@@ -332,7 +304,7 @@ static char **ParseToArg(const char *input, int &arrc)
return arr;
}
-static unsigned long hash(const char *str)
+unsigned long ChatRoom::hash(const char *str)
{
unsigned long hash = 5381;
char c;
@@ -343,7 +315,7 @@ static unsigned long hash(const char *str)
return hash;
}
-static bool checkForbiddenSymbols(const char *str)
+bool ChatRoom::checkForbiddenSymbols(const char *str)
{
char banned_symbols[] = {'!', '@', '#', '\'',
'\"', '\\', '/', '^', '&', '*', ';', ','};
diff --git a/src/server/ChatRoom.hpp b/src/server/ChatRoom.hpp
index a9e2492..eac97c5 100644
--- a/src/server/ChatRoom.hpp
+++ b/src/server/ChatRoom.hpp
@@ -1,5 +1,5 @@
-#ifndef ROOMREALIZATION_H
-#define ROOMREALIZATION_H
+#ifndef WC_CHATROOM_H
+#define WC_CHATROOM_H
#include "../const_vars.hpp"
@@ -9,25 +9,18 @@ const int max_player_lenpass = 24;
class ChatServer;
class UserInfo;
+class StorageOfUsers;
class ChatRoom {
ChatServer *the_server;
- const int code;
- // if code == std_id_lobby then it's lobby
+ const int code; // if code == std_id_lobby then it's lobby
char secret_pass[max_room_lenpass];
- struct item {
- UserInfo *u;
- item *next;
- };
- item *first;
+ StorageOfUsers *users;
public:
ChatRoom(ChatServer *i_server, int id, char *pass);
~ChatRoom();
- void SendAll(const char *msg, UserInfo *except = 0,
- const int spec_msg = system_msg);
-
void HandleMessage(UserInfo *u, const char *str);
void HandleCommand(UserInfo *u, int cmd_counter,
char **commands);
@@ -37,6 +30,11 @@ public:
void AddSession(UserInfo *u);
void RemoveSession(UserInfo *u);
void CloseSession(UserInfo *u);
+
+ static unsigned long hash(const char *str);
+private:
+ static char **ParseToArg(const char *input, int &arrc);
+ static bool checkForbiddenSymbols(const char *str);
};
#endif \ No newline at end of file
diff --git a/src/server/ChatServer.cpp b/src/server/ChatServer.cpp
index 6256e55..0800ee6 100644
--- a/src/server/ChatServer.cpp
+++ b/src/server/ChatServer.cpp
@@ -16,14 +16,23 @@ ChatServer::ChatServer(EventSelector *sel, DatabaseManager *db, int fd)
{
the_selector->Add(this);
lobby = new ChatRoom(this, std_id_lobby, 0);
+ talkers = new StorageOfUsers();
}
ChatServer::~ChatServer()
{
- if(room)
- delete[] room;
+ // right delete this stuff?
+ if(room) {
+ for(int i = 0; i < room_len; i++)
+ DeleteRoom(i);
+ delete[] room;
+ }
the_selector->Remove(this);
+
+ while(talkers->Disconnect())
+ ;
+ delete talkers;
}
ChatServer *ChatServer::Start(EventSelector *sel, DatabaseManager *db, int port)
@@ -134,6 +143,7 @@ handle_room_enter ChatServer::ChangeSessionRoom(ChatRoom *cur_room,
void ChatServer::CloseConnection(UserInfo *u)
{
+ talkers->RemoveUser(u);
the_selector->Remove(u);
delete u;
}
@@ -150,6 +160,65 @@ void ChatServer::Handle(bool r, bool w)
return;
UserInfo *u = new UserInfo(lobby, sd);
+ talkers->AddUser(u);
lobby->AddSession(u);
the_selector->Add(u);
+}
+
+///////////////////////////////////////////////////////////////
+
+StorageOfUsers::~StorageOfUsers()
+{
+ while(first) {
+ item *tmp = first;
+ first = first->next;
+ delete tmp;
+ }
+}
+
+void StorageOfUsers::SendAllUsers(const char *msg, UserInfo *except,
+ const int spec_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);
+}
+
+void StorageOfUsers::AddUser(UserInfo *u)
+{
+ item *p = new item;
+ p->next = first;
+ p->u = u;
+ first = p;
+ online++;
+}
+
+void StorageOfUsers::RemoveUser(UserInfo *u)
+{
+ item **p;
+ for(p = &first; *p; p = &((*p)->next)) {
+ if( ((*p)->u) == u ) {
+ item *tmp = *p;
+ *p = tmp->next;
+ // not delete UserInfo!
+ online--;
+ delete tmp;
+ return;
+ }
+ }
+}
+
+UserInfo *StorageOfUsers::Disconnect()
+{
+ if(first) {
+ item *tmp = first;
+ first = first->next;
+ UserInfo *rn = tmp->u;
+ online--;
+ delete tmp;
+ return rn;
+ }
+ return 0;
} \ No newline at end of file
diff --git a/src/server/ChatServer.hpp b/src/server/ChatServer.hpp
index e2ef154..4fa90e5 100644
--- a/src/server/ChatServer.hpp
+++ b/src/server/ChatServer.hpp
@@ -17,6 +17,28 @@ enum handle_room_enter {
class ChatRoom;
class UserInfo;
+class StorageOfUsers {
+ int online;
+ struct item {
+ UserInfo *u;
+ item *next;
+ };
+ item *first;
+public:
+ StorageOfUsers() : online(0), first(0) {}
+ ~StorageOfUsers();
+
+ void SendAllUsers(const char *msg, UserInfo *except = 0,
+ const int spec_msg = system_msg);
+
+ void AddUser(UserInfo *u);
+ void RemoveUser(UserInfo *u);
+ // func for fast clear storage:
+ UserInfo *Disconnect();
+
+ int GetOnline() const { return online; }
+};
+
class ChatServer : public FdHandler {
EventSelector *the_selector;
DatabaseManager *dbase;
@@ -25,6 +47,8 @@ class ChatServer : public FdHandler {
int room_len;
ChatRoom *lobby;
+ StorageOfUsers *talkers;
+
ChatServer(EventSelector *sel, DatabaseManager *db, int fd);
public:
~ChatServer();
@@ -49,15 +73,4 @@ private:
virtual void Handle(bool r, bool w);
};
-/*class StorageOfUsers {
- struct item {
- ChatSession *s;
- item *next;
- }
- item *first;
-public:
- AddSession(ChatSession *s);
- RemoveSession(ChatSession *s);
-};*/
-
#endif \ No newline at end of file