瀏覽代碼

C++ CivetServer - Set UserContext from constructor; add getUserContext

Simon Hailes 8 年之前
父節點
當前提交
028a2d79c4
共有 2 個文件被更改,包括 22 次插入7 次删除
  1. 13 4
      include/CivetServer.h
  2. 9 3
      src/CivetServer.cpp

+ 13 - 4
include/CivetServer.h

@@ -227,9 +227,11 @@ class CIVETWEB_API CivetServer
 	 * @throws CivetException
 	 * @throws CivetException
 	 */
 	 */
 	CivetServer(const char **options,
 	CivetServer(const char **options,
-	            const struct CivetCallbacks *callbacks = 0);
+	            const struct CivetCallbacks *callbacks = 0,
+                    const void *UserContext = 0);
 	CivetServer(std::vector<std::string> options,
 	CivetServer(std::vector<std::string> options,
-	            const struct CivetCallbacks *callbacks = 0);
+	            const struct CivetCallbacks *callbacks = 0,
+                    const void *UserContext = 0);
 
 
 	/**
 	/**
 	 * Destructor
 	 * Destructor
@@ -532,8 +534,11 @@ class CIVETWEB_API CivetServer
 	                      bool append = false);
 	                      bool append = false);
 
 
         // generic user context which can be set/read,
         // generic user context which can be set/read,
-        // the server does noting with this apart form keep it.
-        void *UserContext;
+        // the server does nothing with this apart from keep it.
+	const void *getUserContext() const
+	{
+		return UserContext;
+	}
         
         
   protected:
   protected:
 	class CivetConnection
 	class CivetConnection
@@ -549,6 +554,10 @@ class CIVETWEB_API CivetServer
 	struct mg_context *context;
 	struct mg_context *context;
 	std::map<struct mg_connection *, class CivetConnection> connections;
 	std::map<struct mg_connection *, class CivetConnection> connections;
 
 
+        // generic user context which can be set/read,
+        // the server does nothing with this apart from keep it.
+        const void *UserContext;
+
   private:
   private:
 	/**
 	/**
 	 * requestHandler(struct mg_connection *, void *cbdata)
 	 * requestHandler(struct mg_connection *, void *cbdata)

+ 9 - 3
src/CivetServer.cpp

@@ -269,11 +269,14 @@ CivetCallbacks::CivetCallbacks()
 }
 }
 
 
 CivetServer::CivetServer(const char **options,
 CivetServer::CivetServer(const char **options,
-                         const struct CivetCallbacks *_callbacks)
+                         const struct CivetCallbacks *_callbacks,
+                         const void *UserContextIn)
     : context(0)
     : context(0)
 {
 {
 	struct CivetCallbacks callbacks;
 	struct CivetCallbacks callbacks;
 
 
+        UserContext = UserContextIn;
+        
 	if (_callbacks) {
 	if (_callbacks) {
 		callbacks = *_callbacks;
 		callbacks = *_callbacks;
 		userCloseHandler = _callbacks->connection_close;
 		userCloseHandler = _callbacks->connection_close;
@@ -288,10 +291,13 @@ CivetServer::CivetServer(const char **options,
 }
 }
 
 
 CivetServer::CivetServer(std::vector<std::string> options,
 CivetServer::CivetServer(std::vector<std::string> options,
-                         const struct CivetCallbacks *_callbacks)
+                         const struct CivetCallbacks *_callbacks,
+                         const void *UserContextIn)
     : context(0)
     : context(0)
 {
 {
 	struct CivetCallbacks callbacks;
 	struct CivetCallbacks callbacks;
+        
+        UserContext = UserContextIn;
 
 
 	if (_callbacks) {
 	if (_callbacks) {
 		callbacks = *_callbacks;
 		callbacks = *_callbacks;
@@ -576,7 +582,7 @@ CivetServer::getListeningPorts()
 	std::vector<int> ports(50);
 	std::vector<int> ports(50);
 	std::vector<struct mg_server_ports> server_ports(50);
 	std::vector<struct mg_server_ports> server_ports(50);
 	int size =
 	int size =
-	    mg_get_server_ports(context, server_ports.size(), &server_ports[0]);
+	    mg_get_server_ports(context, (int)server_ports.size(), &server_ports[0]);
 	if (size <= 0) {
 	if (size <= 0) {
 		ports.resize(0);
 		ports.resize(0);
 		return ports;
 		return ports;