Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

SessionManager.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "net/SessionManager.h"
  19. #include "Config.h"
  20. #include "Core.h"
  21. #include "Log.h"
  22. #include <algorithm>
  23. static const chrono::microseconds kMaxTimeout = chrono::seconds(1);
  24. template<class Mutex>
  25. class unlock_guard
  26. {
  27. public:
  28. unlock_guard(Mutex& mutex) : mutex_(mutex)
  29. {
  30. mutex_.unlock();
  31. }
  32. ~unlock_guard()
  33. {
  34. mutex_.lock();
  35. }
  36. unlock_guard& operator=(const unlock_guard&) = delete;
  37. private:
  38. Mutex& mutex_;
  39. };
  40. SessionManager::SessionManager() :
  41. done_(false),
  42. primary_(nullptr),
  43. clientBegin_(net_clock::now()),
  44. thread_(bind(&SessionManager::run, this))
  45. {
  46. Config& config = Core::get().config();
  47. int serverIp = config.getInt("SessionManager.serverIp", 0);
  48. int serverPort = config.getInt("SessionManager.serverPort", 0);
  49. string accountName = config.getString("SessionManager.accountName", "");
  50. string accountKey = config.getString("SessionManager.accountTicket", "");
  51. config.erase("SessionManager");
  52. if(serverIp == 0)
  53. {
  54. LOG(Net, Warn) << "no login info in configuration\n";
  55. return;
  56. }
  57. Address address(serverIp, static_cast<uint16_t>(serverPort));
  58. lock_guard<mutex> lock(mutex_);
  59. unique_ptr<Session> session(new Session(*this, address, move(accountName), move(accountKey)));
  60. primary_ = session.get();
  61. sessions_.push_back(move(session));
  62. }
  63. SessionManager::~SessionManager()
  64. {
  65. {
  66. lock_guard<mutex> lock(mutex_);
  67. done_ = true;
  68. }
  69. thread_.join();
  70. }
  71. void SessionManager::handleBlobs()
  72. {
  73. vector<BlobPtr> blobs;
  74. {
  75. lock_guard<mutex> lock(mutex_);
  76. for(unique_ptr<Session>& session : sessions_)
  77. {
  78. session->blobAssembler().getBlobs(blobs);
  79. if(!blobs.empty())
  80. {
  81. break;
  82. }
  83. }
  84. }
  85. for(BlobPtr& blob : blobs)
  86. {
  87. blobHandler_.handle(move(blob));
  88. }
  89. }
  90. void SessionManager::sendBlob(BlobPtr blob)
  91. {
  92. if(primary_ == nullptr)
  93. {
  94. LOG(Net, Warn) << "no primary session, dropping outgoing blob\n";
  95. return;
  96. }
  97. primary_->sendBlob(move(blob));
  98. }
  99. void SessionManager::add(unique_ptr<Session> session)
  100. {
  101. sessions_.push_back(move(session));
  102. }
  103. bool SessionManager::exists(Address address) const
  104. {
  105. for(const unique_ptr<Session>& session : sessions_)
  106. {
  107. if(session->baseAddress() == address)
  108. {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. void SessionManager::setPrimary(Session* primary)
  115. {
  116. primary_ = primary;
  117. }
  118. void SessionManager::send(const Packet& packet)
  119. {
  120. socket_.send(packet);
  121. }
  122. net_time_point SessionManager::getClientBegin() const
  123. {
  124. return clientBegin_;
  125. }
  126. void SessionManager::run()
  127. {
  128. lock_guard<mutex> lock(mutex_);
  129. while(!done_)
  130. {
  131. bool readable;
  132. {
  133. unlock_guard<mutex> unlock(mutex_);
  134. readable = socket_.wait(getReadTimeout());
  135. }
  136. if(readable)
  137. {
  138. Packet packet;
  139. while(socket_.recv(packet))
  140. {
  141. handle(packet);
  142. }
  143. }
  144. tick();
  145. }
  146. }
  147. void SessionManager::handle(const Packet& packet)
  148. {
  149. auto it = sessions_.begin();
  150. for(/**/; it != sessions_.end(); ++it)
  151. {
  152. if((*it)->recvAddress() == packet.address)
  153. {
  154. break;
  155. }
  156. }
  157. if(it == sessions_.end())
  158. {
  159. LOG(Net, Warn) << packet.address << " packet matches no session\n";
  160. return;
  161. }
  162. try
  163. {
  164. (*it)->handle(packet);
  165. }
  166. catch(const runtime_error& e)
  167. {
  168. LOG(Net, Error) << (*it)->baseAddress() << " threw an error: " << e.what() << "\n";
  169. if(it->get() == primary_)
  170. {
  171. primary_ = nullptr;
  172. }
  173. sessions_.erase(it);
  174. }
  175. }
  176. void SessionManager::tick()
  177. {
  178. net_time_point now = net_clock::now();
  179. for(auto it = sessions_.begin(); it != sessions_.end(); /**/)
  180. {
  181. try
  182. {
  183. (*it)->tick(now);
  184. }
  185. catch(const runtime_error& e)
  186. {
  187. LOG(Net, Error) << (*it)->baseAddress() << " threw an error: " << e.what() << "\n";
  188. if(it->get() == primary_)
  189. {
  190. primary_ = nullptr;
  191. }
  192. it = sessions_.erase(it);
  193. continue;
  194. }
  195. ++it;
  196. }
  197. }
  198. chrono::microseconds SessionManager::getReadTimeout() const
  199. {
  200. net_time_point now = net_clock::now();
  201. net_time_point nextTick = net_time_point::max();
  202. for(const unique_ptr<Session>& session : sessions_)
  203. {
  204. nextTick = min(nextTick, session->nextTick());
  205. }
  206. if(nextTick <= now)
  207. {
  208. // don't use a negative or zero or timeout, which means "wait forever"
  209. return chrono::microseconds(1);
  210. }
  211. // don't use a timeout bigger than kMaxTimeout, so we keep checking values of done_
  212. return min(chrono::duration_cast<chrono::microseconds>(nextTick - now), kMaxTimeout);
  213. }