Ability to customize bind for HTTP
This commit is contained in:
parent
3f76e9af9f
commit
ddba5b21b3
|
@ -33,9 +33,52 @@ namespace UDPT
|
||||||
/* HTTPServer */
|
/* HTTPServer */
|
||||||
HTTPServer::HTTPServer (uint16_t port, int threads)
|
HTTPServer::HTTPServer (uint16_t port, int threads)
|
||||||
{
|
{
|
||||||
int r;
|
|
||||||
SOCKADDR_IN sa;
|
SOCKADDR_IN sa;
|
||||||
|
|
||||||
|
memset((void*)&sa, 0, sizeof(sa));
|
||||||
|
sa.sin_addr.s_addr = 0L;
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons (port);
|
||||||
|
|
||||||
|
this->init(sa, threads);
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTPServer::HTTPServer(Settings *s)
|
||||||
|
{
|
||||||
|
Settings::SettingClass *sc = s->getClass("apiserver");
|
||||||
|
list<SOCKADDR_IN> localEndpoints;
|
||||||
|
uint16_t port;
|
||||||
|
int threads;
|
||||||
|
|
||||||
|
port = 6969;
|
||||||
|
threads = 1;
|
||||||
|
|
||||||
|
if (sc != NULL)
|
||||||
|
{
|
||||||
|
port = sc->getInt("port", 6969);
|
||||||
|
threads = sc->getInt("threads", 1);
|
||||||
|
sc->getIPs("bind", localEndpoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (threads <= 0)
|
||||||
|
threads = 1;
|
||||||
|
|
||||||
|
if (localEndpoints.empty())
|
||||||
|
{
|
||||||
|
SOCKADDR_IN sa;
|
||||||
|
memset((void*)&sa, 0, sizeof(sa));
|
||||||
|
sa.sin_family = AF_INET;
|
||||||
|
sa.sin_port = htons (port);
|
||||||
|
sa.sin_addr.s_addr = 0L;
|
||||||
|
localEndpoints.push_front(sa);
|
||||||
|
}
|
||||||
|
|
||||||
|
this->init(localEndpoints.front(), threads);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HTTPServer::init (SOCKADDR_IN &localEndpoint, int threads)
|
||||||
|
{
|
||||||
|
int r;
|
||||||
this->thread_count = threads;
|
this->thread_count = threads;
|
||||||
this->threads = new HANDLE[threads];
|
this->threads = new HANDLE[threads];
|
||||||
this->isRunning = false;
|
this->isRunning = false;
|
||||||
|
@ -48,11 +91,7 @@ namespace UDPT
|
||||||
throw ServerException (1, "Failed to create Socket");
|
throw ServerException (1, "Failed to create Socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
sa.sin_addr.s_addr = 0L;
|
r = bind (this->srv, (SOCKADDR*)&localEndpoint, sizeof(localEndpoint));
|
||||||
sa.sin_family = AF_INET;
|
|
||||||
sa.sin_port = htons (port);
|
|
||||||
|
|
||||||
r = bind (this->srv, (SOCKADDR*)&sa, sizeof(sa));
|
|
||||||
if (r == SOCKET_ERROR)
|
if (r == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
throw ServerException (2, "Failed to bind socket");
|
throw ServerException (2, "Failed to bind socket");
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include "../multiplatform.h"
|
#include "../multiplatform.h"
|
||||||
|
#include "../settings.hpp"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#define REQUEST_BUFFER_SIZE 2048
|
#define REQUEST_BUFFER_SIZE 2048
|
||||||
|
@ -130,6 +131,7 @@ namespace UDPT
|
||||||
typedef void (reqCallback)(HTTPServer*,Request*,Response*);
|
typedef void (reqCallback)(HTTPServer*,Request*,Response*);
|
||||||
|
|
||||||
HTTPServer (uint16_t port, int threads);
|
HTTPServer (uint16_t port, int threads);
|
||||||
|
HTTPServer (Settings *s);
|
||||||
|
|
||||||
void addApp (list<string> *path, reqCallback *);
|
void addApp (list<string> *path, reqCallback *);
|
||||||
|
|
||||||
|
@ -152,6 +154,8 @@ namespace UDPT
|
||||||
appNode rootNode;
|
appNode rootNode;
|
||||||
map<string, void*> customData;
|
map<string, void*> customData;
|
||||||
|
|
||||||
|
void init (SOCKADDR_IN &localEndpoint, int threads);
|
||||||
|
|
||||||
static void handleConnections (HTTPServer *);
|
static void handleConnections (HTTPServer *);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
13
src/main.cpp
13
src/main.cpp
|
@ -54,23 +54,14 @@ static void _doAPIStart (Settings *settings, WebApp **wa, HTTPServer **srv, Data
|
||||||
if (sc == NULL)
|
if (sc == NULL)
|
||||||
return; // no settings set!
|
return; // no settings set!
|
||||||
|
|
||||||
if (sc->get("enable") != "1")
|
if (!sc->getBool("enable", false))
|
||||||
{
|
{
|
||||||
cerr << "API Server not enabled." << endl;
|
cerr << "API Server not enabled." << endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string s_port = sc->get("port");
|
|
||||||
string s_threads = sc->get("threads");
|
|
||||||
|
|
||||||
uint16_t port = (s_port == "" ? 6969 : atoi (s_port.c_str()));
|
|
||||||
uint16_t threads = (s_threads == "" ? 1 : atoi (s_threads.c_str()));
|
|
||||||
|
|
||||||
if (threads <= 0)
|
|
||||||
threads = 1;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
*srv = Instance.httpserver = new HTTPServer (port, threads);
|
*srv = Instance.httpserver = new HTTPServer (settings);
|
||||||
*wa = Instance.wa = new WebApp (*srv, drvr, settings);
|
*wa = Instance.wa = new WebApp (*srv, drvr, settings);
|
||||||
(*wa)->deploy();
|
(*wa)->deploy();
|
||||||
} catch (ServerException &e)
|
} catch (ServerException &e)
|
||||||
|
|
Loading…
Reference in a new issue