forked from dyatlov/aimp-web-ctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleserver.cpp
128 lines (108 loc) · 2.78 KB
/
simpleserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <windows.h>
#include <shlwapi.h>
#include "Responder.h"
#include "mongoose.h"
#include "dealer.h"
#include "simpleserver.h"
#include "AIMP2_SDK.h"
#include "ConfigFile.h"
static void *pAimpInterface;
int CACHE_TIME = 60;
int UPDATE_TIME = 10;
static const char *ajax_reply =
"HTTP/1.1 200 OK\r\n"
"Server: AIMP Web Control by Vitaly Dyatlov\r\n"
"Cache: no-cache\r\n"
"Content-Type: application/json\r\n"
"\r\n";
static void *event_handler(enum mg_event event,
struct mg_connection *conn,
const struct mg_request_info *request_info)
{
char *processed = const_cast<char*>("yes");
if (event == MG_NEW_REQUEST)
{
if(request_info->query_string != NULL)
{
std::string response;
if (get_response(pAimpInterface, response, request_info->query_string) == RESPONSE_FILENAME)
{
mg_send_file(conn, response.c_str());
}
else
{
mg_printf(conn, "%s", ajax_reply);
mg_write(conn, response.c_str(), response.size() * sizeof(char));
}
}
else
{
processed = NULL;
}
}
else
{
processed = NULL;
}
return processed;
}
static struct mg_context *ctx;
std::string GetStartPathDll()
{
TCHAR szPath[MAX_PATH + 1] = {0};
if (!GetModuleFileName(GetModuleHandle("aimp_web_ctl.dll"), szPath, MAX_PATH) || !PathRemoveFileSpec(szPath))
{
return std::string();
}
else
{
return std::string(szPath);
}
}
void start_server(void *inf)
{
//get plugins path
std::string path = GetStartPathDll();
path += "\\aimp_web_ctl\\config.cfg";
std::string port("38475");
std::string thread_count("5");
try
{
ConfigFile config( path );
config.readInto( port, string("port"), string("38475") );
config.readInto( thread_count, string("thread_count"), string("5") );
config.readInto( CACHE_TIME, string("cache_time"), 60);
config.readInto( UPDATE_TIME, string("update_time"), 10);
}
catch(...)
{
//do nothing
}
const char *list_ports_value = port.c_str();
const char *num_threads_value = thread_count.c_str();
const char *options[] = {
"document_root", "aimp_web_ctl",
"listening_ports", list_ports_value,
"num_threads", num_threads_value,
NULL
};
pAimpInterface = inf;
ctx = mg_start(&event_handler, NULL, options);
while(ctx == NULL)
{
Sleep(1000);
ctx = mg_start(&event_handler, NULL, options);
if(MessageBoxA(0, "Port is busy, try again?\r\nMake sure that all AIMP Web Control web-pages are closed and port is free", "AIMP Web Control: Error", MB_RETRYCANCEL | MB_ICONEXCLAMATION | MB_DEFBUTTON1 | MB_TOPMOST) != IDRETRY)
{
MessageBoxA(0, "To enable server you should restart AIMP", "AIMP Web Control: Information", MB_OK | MB_ICONINFORMATION | MB_TOPMOST);
break;
}
}
}
void stop_server()
{
mg_stop(ctx);
}