Explorar o código

Rewrite websocket for Lua (Step 14 of ?)

bel %!s(int64=11) %!d(string=hai) anos
pai
achega
edadd7ebf2

+ 1 - 0
VS2012/civetweb_lua/civetweb_lua.vcxproj

@@ -172,6 +172,7 @@
   <ItemGroup>
     <None Include="..\..\src\md5.inl" />
     <None Include="..\..\src\mod_lua.inl" />
+    <None Include="..\..\src\timer.inl" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 3 - 0
VS2012/civetweb_lua/civetweb_lua.vcxproj.filters

@@ -56,5 +56,8 @@
     <None Include="..\..\src\mod_lua.inl">
       <Filter>inl files</Filter>
     </None>
+    <None Include="..\..\src\timer.inl">
+      <Filter>inl files</Filter>
+    </None>
   </ItemGroup>
 </Project>

+ 12 - 39
src/civetweb.c

@@ -2032,9 +2032,9 @@ static int mg_stat(struct mg_connection *conn, const char *path,
 static void set_close_on_exec(int fd, struct mg_connection *conn /* may be null */)
 {
     if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) {
-        if (conn)
-            mg_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s",
-                   __func__, strerror(ERRNO));
+        if (conn) {
+            mg_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO));
+        }
     }
 }
 
@@ -2061,8 +2061,7 @@ int mg_start_thread(mg_thread_func_t func, void *param)
 
 /* Start a thread storing the thread context. */
 
-static int mg_start_thread_with_id(mg_thread_func_t func, void *param,
-                                   pthread_t *threadidptr)
+static int mg_start_thread_with_id(mg_thread_func_t func, void *param, pthread_t *threadidptr)
 {
     pthread_t thread_id;
     pthread_attr_t attr;
@@ -2158,8 +2157,7 @@ static int set_non_blocking_mode(SOCKET sock)
 
 /* Write data to the IO channel - opened file descriptor, socket or SSL
    descriptor. Return number of bytes written. */
-static int64_t push(FILE *fp, SOCKET sock, SSL *ssl, const char *buf,
-                    int64_t len)
+static int64_t push(FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len)
 {
     int64_t sent;
     int n, k;
@@ -6732,7 +6730,6 @@ static void master_thread_run(void *thread_func_param)
 }
 
 /* Threads have different return types on Windows and Unix. */
-
 #ifdef _WIN32
 static unsigned __stdcall master_thread(void *thread_func_param)
 {
@@ -6748,30 +6745,7 @@ static void *master_thread(void *thread_func_param)
 #endif /* _WIN32 */
 
 #if defined(USE_TIMERS)
-void timer_thread_run(void *thread_func_param)
-{
-    struct mg_context *ctx = (struct mg_context *) thread_func_param;
-    while (ctx->stop_flag == 0) {
-        pthread_mutex_lock(&ctx->timer_mutex);
-        /* TODO: something useful */
-        pthread_mutex_unlock(&ctx->timer_mutex);
-        mg_sleep(1);
-    }
-}
-
-#ifdef _WIN32
-static unsigned __stdcall timer_thread(void *thread_func_param)
-{
-    timer_thread_run(thread_func_param);
-    return 0;
-}
-#else
-static void *timer_thread(void *thread_func_param)
-{
-    timer_thread_run(thread_func_param);
-    return NULL;
-}
-#endif /* _WIN32 */
+#include "timer.inl"
 #endif /* USE_TIMERS */
 
 static void free_context(struct mg_context *ctx)
@@ -6792,7 +6766,7 @@ static void free_context(struct mg_context *ctx)
     (void) pthread_mutex_destroy(&ctx->nonce_mutex);
 
 #if defined(USE_TIMERS)
-    (void) pthread_mutex_destroy(&ctx->timer_mutex);
+    timers_exit(ctx);
 #endif
 
     /* Deallocate config parameters */
@@ -6987,10 +6961,6 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     (void) pthread_mutex_init(&ctx->nonce_mutex, NULL);
 
-#if defined(USE_TIMERS)
-    (void) pthread_mutex_init(&ctx->timer_mutex, NULL);
-#endif
-
     workerthreadcount = atoi(ctx->config[NUM_THREADS]);
 
     if (workerthreadcount > MAX_WORKER_THREADS) {
@@ -7010,8 +6980,11 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     }
 
 #if defined(USE_TIMERS)
-    /* Start timer thread */
-    mg_start_thread_with_id(timer_thread, ctx, &ctx->timerthreadid);
+    if (timers_init(ctx) != 0) {
+        mg_cry(fc(ctx), "Error creating timers");
+        free_context(ctx);
+        return NULL;
+    }
 #endif
 
     /* Start master (listening) thread */

+ 14 - 19
src/mod_lua.inl

@@ -736,11 +736,9 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
 {
 #ifdef USE_TIMERS
     int num_args = lua_gettop(L);
-    struct lua_websock_data *ws;
-    lua_Number timediff;
-    int type;
-    struct timespec ts_now;
-    double now;
+    struct lua_websock_data *ws;    
+    int type1,type2, ok = 0;    
+    double timediff;
     struct mg_context *ctx;
 
     lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
@@ -755,23 +753,20 @@ static int lwebsocket_set_timer(lua_State *L, int is_periodic)
         return luaL_error(L, "not enough arguments for set_timer/interval() call");
     }
 
-    type = lua_type(L, 1);
-    if ((type!=LUA_TSTRING && type!=LUA_TFUNCTION) || (type==LUA_TSTRING && num_args!=2)) {
+    type1 = lua_type(L, 1);
+    type2 = lua_type(L, 2);
+    
+    if (type1==LUA_TSTRING && type2==LUA_TNUMBER && num_args==2) {
+        timediff = (double)lua_tonumber(L, 2);
+        ok = (0==timer_add(ctx, timediff, is_periodic, lua_tostring(L, 1)));
+    } else if (type1==LUA_TFUNCTION && type2==LUA_TNUMBER)  {
+        /* TODO: not implemented yet */
+        return luaL_error(L, "invalid arguments for set_timer/interval() call");
+    } else {
         return luaL_error(L, "invalid arguments for set_timer/interval() call");
     }
 
-    if (!lua_isnumber(L, 2) || ((timediff = lua_tonumber(L, 2))<0)) {
-        return luaL_error(L, "invalid timer interval");
-    }
-
-    clock_gettime(CLOCK_MONOTONIC, &ts_now);
-    now = (double)ts_now.tv_sec + ((double)ts_now.tv_nsec * 1.0E6);
-
-    pthread_mutex_lock(&ctx->timer_mutex);
-    /* TODO: next timer call: now + timediff */
-    pthread_mutex_unlock(&ctx->timer_mutex);
-
-    lua_pushboolean(L, 1);
+    lua_pushboolean(L, ok);
     return 1;
 
 #else

+ 54 - 0
src/timer.inl

@@ -0,0 +1,54 @@
+
+struct timers {
+    pthread_t timerthreadid;        /* Time thread ID */
+    pthread_mutex_t timer_mutex;    /* Protects timer lists */
+    struct timer_list *timers;      /* List of timers */
+};
+
+static void timer_thread_run(void *thread_func_param)
+{
+    struct mg_context *ctx = (struct mg_context *) thread_func_param;
+    while (ctx->stop_flag == 0) {
+        pthread_mutex_lock(&ctx->timer_mutex);
+        /* TODO: something useful */
+        pthread_mutex_unlock(&ctx->timer_mutex);
+        mg_sleep(1);
+    }
+}
+
+static int timer_add(struct mg_context * ctx, double rel_time, int is_periodic, const char * action)
+{
+    pthread_mutex_lock(&ctx->timer_mutex);
+    /* TODO: something useful */
+    pthread_mutex_unlock(&ctx->timer_mutex);
+    return 0;
+}
+
+#ifdef _WIN32
+static unsigned __stdcall timer_thread(void *thread_func_param)
+{
+    timer_thread_run(thread_func_param);
+    return 0;
+}
+#else
+static void *timer_thread(void *thread_func_param)
+{
+    timer_thread_run(thread_func_param);
+    return NULL;
+}
+#endif /* _WIN32 */
+
+static int timers_init(struct mg_context * ctx)
+{
+    (void) pthread_mutex_init(&ctx->timer_mutex, NULL);
+
+    /* Start timer thread */
+    mg_start_thread_with_id(timer_thread, ctx, &ctx->timerthreadid);
+
+    return 0;
+}
+
+static void timers_exit(struct mg_context * ctx)
+{
+    (void) pthread_mutex_destroy(&ctx->timer_mutex);
+}