summaryrefslogtreecommitdiffstats
path: root/src/server/chat.hpp
blob: 6abfc4859dfc0c018d2eb29996d20639317b4968 (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
#ifndef CHATREALIZATION_H
#define CHATREALIZATION_H

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

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

class ChatRoom;
class Server;

class ChatSession : FdHandler {
    friend class Server;

	char name[max_name_len];
	char buffer[max_msg_len];
	int buf_used;
	bool ignoring;

	ChatRoom *the_master;

	ChatSession(ChatRoom *i_master, int i_fd) : FdHandler(i_fd),
		buf_used(0), ignoring(false), the_master(i_master) {}
    ~ChatSession() {}

    virtual void Handle(bool r, bool w); 
	void ReadAndIgnore();
    void ReadAndCheck();
    void CheckLines();

    void SetRoom(ChatRoom *new_master);
public:
    const char *GetName();

    void SetName(const char *n_name);
    void Send(const char *msg);
};

class Server : public FdHandler {
    EventSelector *the_selector;
    ChatRoom **room;
    int room_len;

    ChatRoom *lobby;

    Server(EventSelector *sel, int fd);
public:
    ~Server();

    static Server *Start(EventSelector *sel, int port);

    bool RoomExist(int id) const;

    int AddRoom();
    bool DeleteRoom(int id); // call only if room is empty

    void GotoLobby(ChatRoom *cur_room, ChatSession *s);
    bool ChangeSessionRoom(ChatRoom *cur_room, ChatSession *s, int id);
    void CloseConnection(ChatSession *s)
        { the_selector->Remove(s); delete s; }
private:
    virtual void Handle(bool r, bool w); 
};


#endif