소스 검색

Add callbacks for creation and deletion of the server context

bel 11 년 전
부모
커밋
64c97d56e4
2개의 변경된 파일33개의 추가작업 그리고 0개의 파일을 삭제
  1. 15 0
      include/civetweb.h
  2. 18 0
      src/civetweb.c

+ 15 - 0
include/civetweb.h

@@ -153,6 +153,17 @@ struct mg_callbacks {
        Parameters:
        Parameters:
          status: HTTP error status code. */
          status: HTTP error status code. */
     int  (*http_error)(struct mg_connection *, int status);
     int  (*http_error)(struct mg_connection *, int status);
+
+    /* Called after civetweb context has been created, before requests
+       are processed.
+       Parameters:
+         ctx: context handle */
+    void (*init_context)(struct mg_context * ctx);
+
+    /* Called when civetweb context is deleted.
+       Parameters:
+         ctx: context handle */
+    void (*exit_context)(struct mg_context * ctx);
 };
 };
 
 
 
 
@@ -236,6 +247,10 @@ CIVETWEB_API void mg_set_request_handler(struct mg_context *ctx, const char *uri
 CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx, const char *name);
 CIVETWEB_API const char *mg_get_option(const struct mg_context *ctx, const char *name);
 
 
 
 
+/* Get context from connection. */
+CIVETWEB_API struct mg_context *mg_get_context(struct mg_connection *conn);
+
+
 #if defined(MG_LEGACY_INTERFACE)
 #if defined(MG_LEGACY_INTERFACE)
 /* Return array of strings that represent valid configuration options.
 /* Return array of strings that represent valid configuration options.
    For each option, option name and default value is returned, i.e. the
    For each option, option name and default value is returned, i.e. the

+ 18 - 0
src/civetweb.c

@@ -1060,6 +1060,11 @@ const char *mg_get_option(const struct mg_context *ctx, const char *name)
     }
     }
 }
 }
 
 
+struct mg_context *mg_get_context(struct mg_connection * conn)
+{
+    return (conn == NULL) ? (struct mg_context *)NULL : &(conn->ctx);
+}
+
 size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl)
 size_t mg_get_ports(const struct mg_context *ctx, size_t size, int* ports, int* ssl)
 {
 {
     size_t i;
     size_t i;
@@ -6791,6 +6796,10 @@ static void free_context(struct mg_context *ctx)
     if (ctx == NULL)
     if (ctx == NULL)
         return;
         return;
 
 
+    if (ctx->callbacks.exit_context) {
+        ctx->callbacks.exit_context(ctx);
+    }
+
     /* All threads exited, no sync is needed. Destroy thread mutex and condvars */
     /* All threads exited, no sync is needed. Destroy thread mutex and condvars */
     (void) pthread_mutex_destroy(&ctx->thread_mutex);
     (void) pthread_mutex_destroy(&ctx->thread_mutex);
     (void) pthread_cond_destroy(&ctx->thread_cond);
     (void) pthread_cond_destroy(&ctx->thread_cond);
@@ -6903,6 +6912,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     const char *name, *value, *default_value;
     const char *name, *value, *default_value;
     int i, ok;
     int i, ok;
     int workerthreadcount;
     int workerthreadcount;
+    void (*exit_callback)(struct mg_context * ctx) = 0;
 
 
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
     WSADATA data;
     WSADATA data;
@@ -6945,6 +6955,8 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
 
     if (callbacks) {
     if (callbacks) {
         ctx->callbacks = *callbacks;
         ctx->callbacks = *callbacks;
+        exit_callback = callbacks->exit_context;
+        ctx->callbacks.exit_context = 0;
     }
     }
     ctx->user_data = user_data;
     ctx->user_data = user_data;
     ctx->request_handlers = NULL;
     ctx->request_handlers = NULL;
@@ -7028,6 +7040,12 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     }
     }
 #endif
 #endif
 
 
+    /* Context has been created - init user libraries */
+    if (ctx->callbacks.init_context) {
+        ctx->callbacks.init_context(ctx);
+    }
+    ctx->callbacks.exit_context = exit_callback;
+
     /* Start master (listening) thread */
     /* Start master (listening) thread */
     mg_start_thread_with_id(master_thread, ctx, &ctx->masterthreadid);
     mg_start_thread_with_id(master_thread, ctx, &ctx->masterthreadid);