Want more features on Pastebin? Sign Up, it's FREE!
Guest

trololol

By: a guest on Nov 10th, 2010  |  syntax: C++  |  size: 1.36 KB  |  views: 38  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //////////////////server.h////////////////////////
  2. #ifndef SERVER_H
  3. #define SERVER_H
  4.  
  5. #ifdef WIN32
  6.         #define _WIN32_WINNT 0x0501
  7. #endif
  8. #include <boost/shared_ptr.hpp>
  9. #include <boost/asio.hpp>
  10. #include <map>
  11.  
  12. class Client;
  13.  
  14. typedef boost::shared_ptr<Client> ClientPtr;
  15. typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SocketPtr;
  16. typedef std::map<SocketPtr, ClientPtr> ClientsMap;
  17.  
  18. class Server
  19. {
  20.         public:
  21.                 Server();
  22.                 ~Server();
  23.  
  24.                 void start();
  25.  
  26.                 void startAccept();
  27.  
  28.                 ClientsMap::const_iterator begin() { return m_clients.begin(); }
  29.                 ClientsMap::const_iterator end() { return m_clients.end(); }
  30.  
  31.                 int size() { return m_clients.size(); }
  32.  
  33.         private:
  34.                 ClientsMap m_clients;
  35.                 bool accept_connections;
  36.                 boost::asio::io_service service;
  37.                 boost::asio::ip::tcp::acceptor* m_acceptor;
  38. };
  39. #endif
  40. /////////////////////server.cpp//////////////////////////
  41. #include "server.h"
  42. #include "defines.h"
  43. #include "client.h"
  44.  
  45. Server::Server()
  46. {
  47.         service.run();
  48. }
  49.  
  50. Server::~Server()
  51. {
  52.         m_clients.clear();
  53. }
  54.  
  55. void Server::start()
  56. {
  57.         m_acceptor = new boost::asio::ip::tcp::acceptor(service, boost::asio::ip::tcp::endpoint(
  58.                 boost::asio::ip::tcp::v4(), SERVER_PORT));
  59. }
  60.  
  61. void Server::startAccept()
  62. {
  63.         SocketPtr s(new boost::asio::ip::tcp::socket(service));
  64.         m_acceptor->accept(*s);
  65.         ClientPtr ptr(new Client(s));
  66.         m_clients[s] = ptr;
  67. }
clone this paste RAW Paste Data