ithewei il y a 5 ans
Parent
commit
fad0e264ca
3 fichiers modifiés avec 20 ajouts et 0 suppressions
  1. 4 0
      README.md
  2. 12 0
      event/nlog.c
  3. 4 0
      readme_cn.md

+ 4 - 0
README.md

@@ -161,6 +161,10 @@ bin/nc 127.0.0.1 1111
 
 bin/udp 2222
 bin/nc -u 127.0.0.1 2222
+
+make loop
+bin/loop
+bin/nc 127.0.0.1 10514
 ```
 
 ## BUILD

+ 12 - 0
event/nlog.c

@@ -4,6 +4,7 @@
 #include "hdef.h"
 #include "hbase.h"
 #include "hsocket.h"
+#include "hmutex.h"
 
 typedef struct network_logger_s {
     hloop_t*            loop;
@@ -17,9 +18,12 @@ typedef struct nlog_client {
 } nlog_client;
 
 static network_logger_t s_logger = {0};
+static hmutex_t         s_mutex;
 
 static void on_close(hio_t* io) {
     printd("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
+
+    hmutex_lock(&s_mutex);
     struct list_node* next = s_logger.clients.next;
     nlog_client* client;
     while (next != &s_logger.clients) {
@@ -31,6 +35,7 @@ static void on_close(hio_t* io) {
             break;
         }
     }
+    hmutex_unlock(&s_mutex);
 }
 
 static void on_read(hio_t* io, void* buf, int readbytes) {
@@ -57,21 +62,28 @@ static void on_accept(hio_t* io) {
     nlog_client* client;
     SAFE_ALLOC_SIZEOF(client);
     client->io = io;
+
+    hmutex_lock(&s_mutex);
     list_add(&client->node, &s_logger.clients);
+    hmutex_unlock(&s_mutex);
 }
 
 void network_logger(int loglevel, const char* buf, int len) {
     struct list_node* node;
     nlog_client* client;
+
+    hmutex_lock(&s_mutex);
     list_for_each (node, &s_logger.clients) {
         client = list_entry(node, nlog_client, node);
         hio_write(client->io, buf, len);
     }
+    hmutex_unlock(&s_mutex);
 }
 
 hio_t* nlog_listen(hloop_t* loop, int port) {
     list_init(&s_logger.clients);
     s_logger.loop = loop;
     s_logger.listenio = create_tcp_server(loop, "0.0.0.0", port, on_accept);
+    hmutex_init(&s_mutex);
     return s_logger.listenio;
 }

+ 4 - 0
readme_cn.md

@@ -160,6 +160,10 @@ bin/nc 127.0.0.1 1111
 
 bin/udp 2222
 bin/nc -u 127.0.0.1 2222
+
+make loop
+bin/loop
+bin/nc 127.0.0.1 10514
 ```
 
 ## 构建