Advertisement
agrippa1994

boost server

Feb 13th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #define _WIN32_WINNT 0x0601
  2.  
  3. #include <boost/asio.hpp>
  4. #include <boost/system/system_error.hpp>
  5. #include <boost/bind.hpp>
  6.  
  7. #include <Windows.h>
  8. #include <iostream>
  9.  
  10. #define MAX_CLIENTS 500
  11. #define MAX_LEN 16000
  12.  
  13. using namespace std;
  14.  
  15. boost::asio::io_service io;
  16. boost::asio::ip::tcp::acceptor *Endpoint;
  17.  
  18. struct stClient
  19. {
  20.     stClient() : socket(io) {}
  21.  
  22.     unsigned int iID;
  23.     boost::asio::ip::tcp::socket socket;
  24.     bool bConnected;
  25.     char cBuffer[MAX_LEN];
  26. }   g_Clients[MAX_CLIENTS];
  27.  
  28. void OnConnected(stClient *Client)
  29. {
  30.     cout << Client->iID << " hat sich verbunden!" << endl;
  31. }
  32.  
  33. void OnData(stClient *Client,const char *data,size_t size)
  34. {
  35.     cout << Client->iID << ": " << data << endl;
  36. }
  37.  
  38. void OnDisconnect(stClient *Client)
  39. {
  40.     cout << Client->iID << " hat die Verbindung getrennt" << endl;
  41. }
  42.  
  43. /* Wenn ein Client Daten gesendet hat */
  44. void handle_read(stClient *client,const boost::system::error_code& er,const size_t transferred)
  45. {
  46.     if(er)
  47.     {
  48.         /* Wenn der Client die Verbindung getrennt hat */
  49.         if(client->bConnected)
  50.             OnDisconnect(client);
  51.         client->bConnected = false;
  52.         return;
  53.     }
  54.  
  55.     /* Daten erfolgreich bekommen */
  56.     OnData(client,client->cBuffer,transferred);
  57.     memset(client->cBuffer,0,MAX_LEN);
  58.  
  59.     client->socket.async_read_some(boost::asio::buffer(
  60.         client->cBuffer,MAX_LEN),
  61.         boost::bind(handle_read,client,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  62.  
  63. }
  64.  
  65. int GetFreeSlot()
  66. {
  67.     for(int i=0;i<MAX_CLIENTS;i++)
  68.         if(!g_Clients[i].bConnected)
  69.             return i;
  70.     return -1;
  71. }
  72.  
  73. void handle_accept(stClient *client,const boost::system::error_code& er)
  74. {
  75.     if(er)
  76.     {
  77.         cout << er.message() << endl;
  78.         return;
  79.     }
  80.  
  81.     /* Code, wenn sich ein Client verbunden hat */
  82.     OnConnected(client);
  83.  
  84.     client->socket.async_read_some(boost::asio::buffer(client->cBuffer,MAX_LEN),
  85.         boost::bind(handle_read,client,boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
  86.     client->bConnected = true;
  87.  
  88.     int slot = GetFreeSlot();
  89.     if(slot == -1)
  90.         return;
  91.  
  92.     Endpoint->async_accept(g_Clients[slot].socket,boost::bind(handle_accept,&g_Clients[slot],boost::asio::placeholders::error));
  93. }
  94.  
  95. int main()
  96. {
  97.     for(int i=0;i<MAX_CLIENTS;i++)
  98.     {
  99.         g_Clients[i].bConnected=false;
  100.         g_Clients[i].iID = i;
  101.     }
  102.  
  103.     int slot = GetFreeSlot();
  104.     if(slot == -1)
  105.         return 0;
  106.  
  107.     Endpoint = new boost::asio::ip::tcp::acceptor(io,boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),1337));
  108.     Endpoint->async_accept(g_Clients[slot].socket,boost::bind(handle_accept,&g_Clients[slot],boost::asio::placeholders::error));
  109.     io.run();
  110.     getchar();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement