@@ -23,10 +23,10 @@ void AbstractServer::Stop() {
2323 server_stop_cv.wait (server_lock);
2424}
2525
26- static void EventHandler (mg_connection* conn, int event, void * event_data) {
26+ static void EventHandler (mg_connection* conn, int event, void * event_data, void * context ) {
2727 switch (event) {
28- case MG_EV_RECV :
29- case MG_EV_SEND :
28+ case MG_EV_READ :
29+ case MG_EV_WRITE :
3030 /* * Do nothing. Just for housekeeping. **/
3131 break ;
3232 case MG_EV_POLL:
@@ -36,22 +36,20 @@ static void EventHandler(mg_connection* conn, int event, void* event_data) {
3636 /* * Do nothing. Just for housekeeping. **/
3737 break ;
3838 case MG_EV_ACCEPT:
39- /* * Do nothing. Just for housekeeping. **/
39+ /* Initialize HTTPS connection if Server is an HTTPS Server */
40+ static_cast <AbstractServer*>(context)->acceptConnection (conn);
4041 break ;
4142 case MG_EV_CONNECT:
4243 /* * Do nothing. Just for housekeeping. **/
4344 break ;
44- case MG_EV_TIMER:
45- /* * Do nothing. Just for housekeeping. **/
46- break ;
4745
4846 case MG_EV_HTTP_CHUNK: {
4947 /* * Do nothing. Just for housekeeping. **/
5048 } break ;
5149
52- case MG_EV_HTTP_REQUEST : {
53- AbstractServer* server = static_cast <AbstractServer*>(conn-> mgr -> user_data );
54- server->OnRequest (conn, static_cast <http_message *>(event_data));
50+ case MG_EV_HTTP_MSG : {
51+ AbstractServer* server = static_cast <AbstractServer*>(context );
52+ server->OnRequest (conn, static_cast <mg_http_message *>(event_data));
5553 } break ;
5654
5755 default :
@@ -69,10 +67,12 @@ void AbstractServer::Run() {
6967
7068 // Main server loop:
7169 while (should_run) {
72- mg_mgr_poll (&mgr, 1000 );
70+ // NOLINTNEXTLINE (cppcoreguidelines-avoid-magic-numbers)
71+ mg_mgr_poll (&mgr, 100 );
7372 }
7473
7574 // Shutdown and cleanup:
75+ timer_args.clear ();
7676 mg_mgr_free (&mgr);
7777
7878 // Notify the main thread that we have shut down everything:
@@ -110,4 +110,34 @@ std::string AbstractServer::Base64Decode(const std::string& in) {
110110 return out;
111111}
112112
113+ // Sends error similar like in mongoose 6 method mg_http_send_error
114+ // https://github.com/cesanta/mongoose/blob/6.18/mongoose.c#L7081-L7089
115+ void AbstractServer::SendError (mg_connection* conn, int code, std::string& reason) {
116+ std::string headers{" Content-Type: text/plain\r\n Connection: close\r\n " };
117+ mg_http_reply (conn, code, headers.c_str (), reason.c_str ());
118+ }
119+
120+ // Checks whether a pointer to a connection is still managed by a mg_mgr.
121+ // This check tells whether it is still possible to send a message via the given connection
122+ // Note that it is still possible that the pointer of an old connection object may be reused by mongoose.
123+ // In this case, the active connection might refer to a different connection than the one the caller refers to
124+ bool AbstractServer::IsConnectionActive (mg_mgr* mgr, mg_connection* conn) {
125+ mg_connection* c{mgr->conns };
126+ while (c) {
127+ if (c == conn) {
128+ return true ;
129+ }
130+ c = c->next ;
131+ }
132+ return false ;
133+ }
134+
135+ uint16_t AbstractServer::GetRemotePort (const mg_connection* conn) {
136+ return (conn->rem .port >> 8 ) | (conn->rem .port << 8 );
137+ }
138+
139+ uint16_t AbstractServer::GetLocalPort (const mg_connection* conn) {
140+ return (conn->loc .port >> 8 ) | (conn->loc .port << 8 );
141+ }
142+
113143} // namespace cpr
0 commit comments