Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////server.h////////////////////////
- #ifndef SERVER_H
- #define SERVER_H
- #ifdef WIN32
- #define _WIN32_WINNT 0x0501
- #endif
- #include <boost/shared_ptr.hpp>
- #include <boost/asio.hpp>
- #include <map>
- class Client;
- typedef boost::shared_ptr<Client> ClientPtr;
- typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SocketPtr;
- typedef std::map<SocketPtr, ClientPtr> ClientsMap;
- class Server
- {
- public:
- Server();
- ~Server();
- void start();
- void startAccept();
- ClientsMap::const_iterator begin() { return m_clients.begin(); }
- ClientsMap::const_iterator end() { return m_clients.end(); }
- int size() { return m_clients.size(); }
- private:
- ClientsMap m_clients;
- bool accept_connections;
- boost::asio::io_service service;
- boost::asio::ip::tcp::acceptor* m_acceptor;
- };
- #endif
- /////////////////////server.cpp//////////////////////////
- #include "server.h"
- #include "defines.h"
- #include "client.h"
- Server::Server()
- {
- service.run();
- }
- Server::~Server()
- {
- m_clients.clear();
- }
- void Server::start()
- {
- m_acceptor = new boost::asio::ip::tcp::acceptor(service, boost::asio::ip::tcp::endpoint(
- boost::asio::ip::tcp::v4(), SERVER_PORT));
- }
- void Server::startAccept()
- {
- SocketPtr s(new boost::asio::ip::tcp::socket(service));
- m_acceptor->accept(*s);
- ClientPtr ptr(new Client(s));
- m_clients[s] = ptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement