OSDN Git Service

サーバー用のconfig.jsonを追加
[mmo/main.git] / server / Server.hpp
1 //
2 // Server.hpp
3 //
4
5 #pragma once
6
7 #include <string>
8 #include <list>
9 #include <functional>
10 #include "../common/network/Session.hpp"
11 #include "Config.hpp"
12
13 #define UDP_MAX_RECEIVE_LENGTH (2048)
14 #define UDP_TEST_PACKET_TIME (5)
15
16 namespace network {
17
18 class Server {
19     private:
20         class ServerSession : public Session {
21             public:
22                 ServerSession(boost::asio::io_service& io_service) :
23                     Session(io_service) {};
24
25                 void Start();
26         };
27
28     public:
29         Server(const Config& config);
30         void Start(CallbackFuncPtr callback);
31         void Stop();
32         void Stop(int interrupt_type);
33         void SendAll(const Command&);
34         void SendOthers(const Command&, SessionWeakPtr);
35
36         bool Empty() const;
37                 std::string GetStatusJSON() const;
38
39         int GetSessionReadAverageLimit();
40                 int GetUserCount() const;
41                 void RefreshSession();
42
43         void SendUDPTestPacket(const std::string& ip_address, uint16_t port);
44                 void SendUDP(const std::string& message, const boost::asio::ip::udp::endpoint endpoint);
45
46         int max_total_read_average() const;
47         int max_session_read_average() const;
48         int min_session_read_average() const;
49         void set_max_total_read_average(int byte);
50         void set_max_session_read_average(int byte);
51         void set_min_session_read_average(int byte);
52
53     private:
54         void ReceiveSession(const SessionPtr&, const boost::system::error_code&);
55
56         void ReceiveUDP(const boost::system::error_code& error, size_t bytes_recvd);
57         void DoWriteUDP(const std::string& msg, const udp::endpoint& endpoint);
58         void WriteUDP(const boost::system::error_code& error, boost::shared_ptr<std::string> holder);
59
60         void FetchUDP(const std::string& buffer, const boost::asio::ip::udp::endpoint endpoint);
61
62     private:
63            Config config_;
64
65        boost::asio::io_service io_service_;
66        tcp::endpoint endpoint_;
67        tcp::acceptor acceptor_;
68
69        udp::socket socket_udp_;
70        udp::endpoint sender_endpoint_;
71
72        char receive_buf_udp_[2048];
73        unsigned char udp_packet_count_;
74
75        CallbackFuncPtr callback_;
76
77        int max_total_read_average_;
78        int max_session_read_average_;
79        int min_session_read_average_;
80        int session_read_average_;
81
82        boost::mutex mutex_;
83        std::list<SessionWeakPtr> sessions_;
84
85 };
86
87 }