OSDN Git Service

ScriptEnvironment.cppの文字コードを変更
[mmo/main.git] / client / SocketServerManager.cpp
1 //
2 // SocketServerManager.cpp
3 //
4
5 #include "SocketServerManager.hpp"
6 #include "CardManager.hpp"
7
8 const char SocketServerManager::DELIMITOR[] = {0x0d, 0x0a, 0x0};
9
10 SocketServerManager::SocketServerManager(const ManagerAccessorPtr& manager_accessor) :
11         manager_accessor_(manager_accessor),
12         acceptor_(io_service_, tcp::endpoint(tcp::v4(), 39340))
13 {
14
15 }
16
17 void SocketServerManager::Start()
18 {
19     auto new_session = boost::make_shared<Session>(manager_accessor_, io_service_);
20     acceptor_.async_accept(new_session->socket(),
21         boost::bind(&SocketServerManager::ReceiveSession, this, new_session,
22           boost::asio::placeholders::error));
23
24         boost::thread t([this](){
25         try {
26             io_service_.run();
27         } catch (std::exception& e) {
28             std::cout << e.what() << std::endl;
29         }
30         });
31 }
32
33 void SocketServerManager::ReceiveSession(const SessionPtr& session, const boost::system::error_code& error)
34 {
35                 
36         if(!error) {
37         session->Start();
38     }
39
40     auto new_session = boost::make_shared<Session>(manager_accessor_, io_service_);
41     acceptor_.async_accept(new_session->socket(),
42         boost::bind(&SocketServerManager::ReceiveSession, this, new_session,
43           boost::asio::placeholders::error));
44
45 }
46
47 SocketServerManager::Session::Session(const ManagerAccessorPtr& manager_accessor, boost::asio::io_service& io_service) :
48
49         socket_(io_service),
50         card_(std::make_shared<Card>(manager_accessor, "", "sock", "",
51                             std::vector<std::string>()))
52 {
53         auto callback = std::make_shared<std::function<void(const std::string&)>>(
54                 [this](const std::string& str){
55                         boost::asio::write(socket_, boost::asio::buffer(str.data(), str.size()));
56                 });
57         card_->set_on_socket_reply(callback);
58
59     if (auto card_manager = manager_accessor->card_manager().lock()) {
60         card_manager->AddCard(card_);
61     }
62 }
63
64 void SocketServerManager::Session::Start()
65 {
66     boost::asio::async_read_until(socket_,
67         receive_buf_, DELIMITOR[1],
68             boost::bind(&SocketServerManager::Session::ReceiveTCP, shared_from_this(),
69               boost::asio::placeholders::error));
70 }
71
72 void SocketServerManager::Session::ReceiveTCP(const boost::system::error_code& error)
73 {
74     if (!error) {
75         std::string buffer(boost::asio::buffer_cast<const char*>(receive_buf_.data()),receive_buf_.size());
76         auto length = buffer.find_last_of(DELIMITOR);
77
78         if (length != std::string::npos) {
79
80             receive_buf_.consume(length + 1);
81             buffer.erase(length - 1);
82
83             if (!buffer.empty()) {
84
85                                 Logger::Debug(_T("Receive command: %d"), unicode::ToTString(buffer));
86                                 card_->Execute(buffer, "", 
87                                         [this](const Handle<Value>& value, const std::string error){
88                                                 if (!error.empty()) {
89                                                         std::string return_str(error);
90                                                         return_str += DELIMITOR;
91                                                         boost::asio::write(socket_, boost::asio::buffer(return_str.data(), return_str.size()));
92                                                 }
93                                         });
94             }
95
96                         boost::asio::async_read_until(socket_,
97                                 receive_buf_, DELIMITOR[1],
98                                         boost::bind(&SocketServerManager::Session::ReceiveTCP, shared_from_this(),
99                                           boost::asio::placeholders::error));
100
101         }
102
103     } else {
104
105     }
106 }
107
108 tcp::socket& SocketServerManager::Session::socket()
109 {
110         return socket_;
111 }