summaryrefslogtreecommitdiffstats
path: root/src/server/chat.hpp
blob: ca225193cb4355b1fb1549ad65410b1c07de0fb0 (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
#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();
public:
    const char *GetName() const { return name; }

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

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

    int room_len;

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

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

    int AddRoom();
    // RemoveRoom();
    // void AddSessionToRoom(ChatSession *s, int id);
    void CloseConnection(ChatSession *s)
        { the_selector->Remove(s); delete s; }
private:
    virtual void Handle(bool r, bool w); 
};


#endif