summaryrefslogtreecommitdiffstats
path: root/src/server/ChatServer.hpp
blob: 4fa90e59887aa832946acd56b19b7d5a4512f81b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef WC_CHATSERVER_H
#define WC_CHATSERVER_H

#include "sockets.hpp"
#include "database.hpp"
#include "../const_vars.hpp"

#define CONSOLE_LOG(f_, ...) printf((f_), ##__VA_ARGS__)

enum handle_room_enter {
    enter_noexist,
    enter_private,
    enter_uncorrect_pass,
    enter_success
};

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;

    ChatRoom **room;
    int room_len;
    ChatRoom *lobby;

    StorageOfUsers *talkers;

    ChatServer(EventSelector *sel, DatabaseManager *db, int fd);
public:
    ~ChatServer();
    static ChatServer *Start(EventSelector *sel, 
        DatabaseManager *db, int fd);

    int AddRoom(char *password);
    bool DeleteRoom(int id); // call only if room is empty
    bool RoomExist(int id) const;

    void GotoLobby(ChatRoom *cur_room, UserInfo *u);
    handle_room_enter ChangeSessionRoom(ChatRoom *cur_room,
        UserInfo *u, int id, char *pass);

    void CloseConnection(UserInfo *u);

    // work with database:
    AnswerDB *QuerySelect(const char *sql) 
        { return dbase->QuerySelect(sql); }

private:
    virtual void Handle(bool r, bool w); 
};

#endif