Skip to content

Commit db3d837

Browse files
committed
The path to the application directory is passed to the initialization function
1 parent cdc448f commit db3d837

File tree

5 files changed

+50
-40
lines changed

5 files changed

+50
-40
lines changed

src/server/Server.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,19 @@ namespace HttpServer
289289
{
290290
if (app->application_final)
291291
{
292-
app->application_final();
292+
const std::string root = app->root_dir;
293+
app->application_final(root.data() );
293294
}
294295
}
295296
catch (std::exception &exc)
296297
{
297-
std::cout << "Warning: error when the application finishes '" << app->server_module << "':" << exc.what() << std::endl;
298+
std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl;
298299
}
299300

300301
app->application_call = std::function<int(Transfer::app_request *, Transfer::app_response *)>();
301302
app->application_clear = std::function<void(void *, size_t)>();
302-
app->application_init = std::function<bool()>();
303-
app->application_final = std::function<void()>();
303+
app->application_init = std::function<bool(const char *)>();
304+
app->application_final = std::function<void(const char *)>();
304305
}
305306
}
306307

@@ -423,18 +424,18 @@ namespace HttpServer
423424

424425
std::function<void(void *, size_t)> app_clear = reinterpret_cast<void(*)(void *, size_t)>(addr);
425426

426-
std::function<bool()> app_init = std::function<bool()>();
427+
std::function<bool(const char *)> app_init = std::function<bool(const char *)>();
427428

428429
if (module.find("application_init", &addr) )
429430
{
430-
app_init = reinterpret_cast<bool(*)()>(addr);
431+
app_init = reinterpret_cast<bool(*)(const char *)>(addr);
431432
}
432433

433-
std::function<void()> app_final = std::function<void()>();
434+
std::function<void(const char *)> app_final = std::function<void(const char *)>();
434435

435436
if (module.find("application_final", &addr) )
436437
{
437-
app_final = reinterpret_cast<void(*)()>(addr);
438+
app_final = reinterpret_cast<void(*)(const char *)>(addr);
438439
}
439440

440441
for (auto &app : same)
@@ -448,12 +449,13 @@ namespace HttpServer
448449
{
449450
if (app->application_init)
450451
{
451-
app->application_init();
452+
const std::string root = app->root_dir;
453+
app->application_init(root.data() );
452454
}
453455
}
454456
catch (std::exception &exc)
455457
{
456-
std::cout << "Warning: error when initializing the application '" << module_name << "':" << exc.what() << std::endl;
458+
std::cout << "Warning: an exception was thrown when the application '" << module_name << "' was initialized: " << exc.what() << std::endl;
457459
}
458460
}
459461

@@ -502,7 +504,7 @@ namespace HttpServer
502504
}
503505
}
504506

505-
std::cout << "Notice: applications' modules have been updated;" << std::endl;
507+
std::cout << "Notice: application modules have been updated;" << std::endl;
506508

507509
this->controls.setProcess();
508510
this->controls.eventUpdateModule->reset();
@@ -780,7 +782,7 @@ namespace HttpServer
780782
sockets_list.addSocket(sock);
781783
}
782784

783-
std::cout << "Log: launch server's cycle;" << std::endl << std::endl;
785+
std::cout << "Log: server started work;" << std::endl << std::endl;
784786

785787
constexpr size_t queue_max_length = 1024;
786788
this->controls.eventNotFullQueue = new Utils::Event(true, true);
@@ -855,7 +857,7 @@ namespace HttpServer
855857

856858
this->clear();
857859

858-
std::cout << "Log: complete server's cycle;" << std::endl;
860+
std::cout << "Log: server work completed;" << std::endl;
859861

860862
return EXIT_SUCCESS;
861863
}

src/server/ServerApplicationSettings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace HttpServer
3131

3232
std::function<int(Transfer::app_request *, Transfer::app_response *)> application_call;
3333
std::function<void(void *, size_t)> application_clear;
34-
std::function<bool()> application_init;
35-
std::function<void()> application_final;
34+
std::function<bool(const char *)> application_init;
35+
std::function<void(const char *)> application_final;
3636
};
3737
};

src/server/ServerSettings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ namespace HttpServer
3838
{
3939
if (app->application_final)
4040
{
41-
app->application_final();
41+
const std::string root = app->root_dir;
42+
app->application_final(root.data() );
4243
}
4344
}
4445
catch (std::exception &exc)
4546
{
46-
std::cout << "Warning: the error of the application's finalize '" << app->server_module << "':" << exc.what() << std::endl;
47+
std::cout << "Warning: an exception was thrown when the application '" << app->server_module << "' was finishes: " << exc.what() << std::endl;
4748
}
4849

4950
delete app;

src/server/config/ConfigParser.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,33 @@ namespace HttpServer
244244

245245
std::function<void(void *, size_t)> app_clear = reinterpret_cast<void(*)(void *, size_t)>(addr);
246246

247-
std::function<bool()> app_init = std::function<bool()>();
247+
std::function<bool(const char *)> app_init = std::function<bool(const char *)>();
248248

249249
if (module.find("application_init", &addr) )
250250
{
251-
app_init = reinterpret_cast<bool(*)()>(addr);
251+
app_init = reinterpret_cast<bool(*)(const char *)>(addr);
252252
}
253253

254-
std::function<void()> app_final = std::function<void()>();
254+
std::function<void(const char *)> app_final = std::function<void(const char *)>();
255255

256256
if (module.find("application_final", &addr) )
257257
{
258-
app_final = reinterpret_cast<void(*)()>(addr);
258+
app_final = reinterpret_cast<void(*)(const char *)>(addr);
259+
}
260+
261+
std::string root_dir = it_root_dir->second;
262+
263+
#ifdef WIN32
264+
if ('\\' == root_dir.back() )
265+
{
266+
root_dir.pop_back();
267+
}
268+
#endif
269+
270+
// Remove back slash from root_dir
271+
if ('/' == root_dir.back() )
272+
{
273+
root_dir.pop_back();
259274
}
260275

261276
bool success = true;
@@ -264,17 +279,21 @@ namespace HttpServer
264279
{
265280
if (app_init)
266281
{
267-
success = app_init();
282+
const std::string root = root_dir;
283+
success = app_init(root.data() );
268284
}
269285
}
270-
catch (...)
286+
catch (std::exception &exc)
271287
{
288+
std::cout << "Warning: an exception was thrown when the application '" << it_module->second << "' was initialized: " << exc.what() << std::endl;
289+
272290
success = false;
273291
}
274292

275293
if (false == success)
276294
{
277295
std::cout << "Warning: error when initializing application '" << it_module->second << "';" << std::endl;
296+
278297
return false;
279298
}
280299

@@ -308,21 +327,6 @@ namespace HttpServer
308327
modules.emplace_back(std::move(module) );
309328
}
310329

311-
std::string root_dir = it_root_dir->second;
312-
313-
#ifdef WIN32
314-
if ('\\' == root_dir.back() )
315-
{
316-
root_dir.pop_back();
317-
}
318-
#endif
319-
320-
// Remove back slash from root_dir
321-
if ('/' == root_dir.back() )
322-
{
323-
root_dir.pop_back();
324-
}
325-
326330
// Create application settings struct
327331
ServerApplicationSettings *settings = new ServerApplicationSettings {
328332
std::move(ports),

src/server/protocol/ServerHttp1.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ namespace HttpServer
5454

5555
long ServerHttp1::sendData(const void *src, size_t size, const std::chrono::milliseconds &timeout, DataTransfer *dt) const
5656
{
57-
long send_size = this->sock.nonblock_send(src, size, timeout);
57+
const long send_size = this->sock.nonblock_send(src, size, timeout);
5858

59-
dt->send_total += send_size;
59+
if (send_size > 0)
60+
{
61+
dt->send_total += send_size;
62+
}
6063

6164
return send_size;
6265
}

0 commit comments

Comments
 (0)