summaryrefslogtreecommitdiffstats
path: root/src/server/chat.hpp
blob: 4b6c8b154a57024d8b3fbf22a0eaa905449c574b (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
77
78
79
80
81
82
83
84
#ifndef CHATREALIZATION_H
#define CHATREALIZATION_H

#include "sockets.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
};

enum enum_status {
    wait_name, // expecting a username from player
    wait_reg, // expecting registration
    wait_log, // expecting login
    no_wait
};

class ChatRoom;
class Server;

class ChatSession : FdHandler {
    friend class Server;
    friend class ChatRoom;

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

    enum_status state;
	ChatRoom *the_master;

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

    void SetRoom(ChatRoom *new_master);

    virtual void Handle(bool r, bool w); 
    void ReadAndIgnore();
    void ReadAndCheck();
    void CheckLines();
public:
    const char *GetName();

    void SetName(const char *n_name);
    void Send(const char *msg, const int spec_msg = system_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(char *password);
    bool DeleteRoom(int id); // call only if room is empty

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


#endif