SHARE
TWEET

Untitled

a guest Jul 21st, 2019 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////
  2. // Exception.h
  3. //////
  4. #ifndef __EXCEPTION_H
  5.     #define __EXCEPTION_H
  6.  
  7.     #include <stdio.h>
  8.     #include <stdarg.h>
  9.  
  10.     class Exception {
  11.  
  12.         public:
  13.             Exception(const char *message, ...);
  14.             ~Exception();
  15.  
  16.             char *getMessage();
  17.  
  18.         private:
  19.             char message[512];
  20.  
  21.     };
  22.  
  23. #endif
  24. ////////////
  25. ///Exception.cpp
  26. ////////////
  27.  
  28. #include "Exception.h"
  29.  
  30.     Exception::Exception(const char *message, ...) {
  31.         va_list args;
  32.         va_start(args, message);
  33.         vsprintf(this->message, message, args);
  34.         va_end(args);
  35.     }
  36.  
  37.     Exception::~Exception() {
  38.  
  39.     }
  40.  
  41.     char *Exception::getMessage() {
  42.         return message;
  43.     }
  44.  
  45. ///////
  46. /// HTTPServer.h
  47. //////
  48.  
  49. #ifndef __SERVER_H
  50.     #define __SERVER_H
  51.  
  52.     #include <stdio.h>
  53.     #include <sys/socket.h>
  54.     #include <sys/epoll.h>
  55.     #include <arpa/inet.h>
  56.     #include <errno.h>
  57.     #include <vector>
  58.  
  59.     #include "HTTPConnection.h"
  60.     #include "Exception.h"
  61.    
  62.     // without forward declaration i get "Exception is not a type name"
  63.     class Exception;
  64.     // with forward declaration i get "use of incomplete type"
  65.  
  66.     class HTTPServer {
  67.  
  68.         public:
  69.  
  70.             HTTPServer(unsigned short listenPort);
  71.             ~HTTPServer();
  72.  
  73.             void Listen();
  74.  
  75.         private:
  76.             int listenSock;
  77.             int epollFd;
  78.             struct sockaddr_in address;
  79.             std::vector<HTTPConnection> connections;
  80.             unsigned short listenPort;
  81.  
  82.     };
  83.  
  84. #endif
  85.  
  86. ////
  87. //  HTTPServer.cpp
  88. ////
  89.  
  90. #include "HTTPServer.h"
  91.  
  92.     HTTPServer::HTTPServer(unsigned short listenPort) {
  93.         this->listenPort = listenPort;
  94.  
  95.     }
  96.  
  97.     HTTPServer::~HTTPServer() {
  98.  
  99.     }
  100.  
  101.     void HTTPServer::Listen() {
  102.  
  103.         listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  104.  
  105.         if(listenSock == -1) {
  106.             throw Exception("Failed to create listen socket.");
  107.         }
  108.  
  109.         epollFd = epoll_create(0);
  110.  
  111.         if(epollFd == -1) {
  112.             throw Exception("Failed to create epoll instance.");
  113.         }
  114.  
  115.         address.sin_family = AF_INET;
  116.         address.sin_port = htons(listenPort);
  117.         address.sin_addr.s_addr = INADDR_ANY;
  118.  
  119.         if(bind(listenSock, (struct sockaddr*)&address, sizeof(struct sockaddr)) < 0) {
  120.             throw Exception("Cannot bind listen socket to port %u", listenPort);
  121.         }
  122.  
  123.         if(listen(listenSock, 50) != 0) {
  124.             throw Exception("Cannot listen() on listen socket.");
  125.         }
  126.  
  127.         printf("Server listening on port %u\n", listenPort);
  128.  
  129.         while(1) {
  130.  
  131.  
  132.  
  133.         }
  134.  
  135.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top