소스 검색

hio_t add connected flag to avoid SIGPIPE by hio_write

ithewei 5 년 전
부모
커밋
27a86e3fef
4개의 변경된 파일10개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 0
      event/hevent.h
  2. 1 1
      event/hloop.c
  3. 4 0
      event/nio.c
  4. 4 0
      event/overlapio.c

+ 1 - 0
event/hevent.h

@@ -88,6 +88,7 @@ struct hio_s {
     HEVENT_FIELDS
     // flags
     unsigned    ready       :1;
+    unsigned    connected   :1;
     unsigned    closed      :1;
     unsigned    accept      :1;
     unsigned    connect     :1;

+ 1 - 1
event/hloop.c

@@ -634,7 +634,7 @@ void hio_ready(hio_t* io) {
     if (io->ready) return;
     // flags
     io->ready = 1;
-    io->closed = 0;
+    io->connected = io->closed = 0;
     io->accept = io->connect = io->connectex = 0;
     io->recv = io->send = 0;
     io->recvfrom = io->sendto = 0;

+ 4 - 0
event/nio.c

@@ -52,6 +52,7 @@ static void __heartbeat_timer_cb(htimer_t* timer) {
 }
 
 static void __accept_cb(hio_t* io) {
+    io->connected = 1;
     /*
     char localaddrstr[SOCKADDR_STRLEN] = {0};
     char peeraddrstr[SOCKADDR_STRLEN] = {0};
@@ -78,6 +79,7 @@ static void __accept_cb(hio_t* io) {
 }
 
 static void __connect_cb(hio_t* io) {
+    io->connected = 1;
     /*
     char localaddrstr[SOCKADDR_STRLEN] = {0};
     char peeraddrstr[SOCKADDR_STRLEN] = {0};
@@ -494,6 +496,7 @@ int hio_read (hio_t* io) {
 }
 
 int hio_write (hio_t* io, const void* buf, size_t len) {
+    if (!io->connected) return -1;
     int nwrite = 0;
     if (write_queue_empty(&io->write_queue)) {
 try_write:
@@ -552,6 +555,7 @@ int hio_close (hio_t* io) {
         return 0;
     }
 
+    io->connected = 0;
     io->closed = 1;
     hio_done(io);
     __close_cb(io);

+ 4 - 0
event/overlapio.c

@@ -111,6 +111,7 @@ static void on_acceptex_complete(hio_t* io) {
     if (io->accept_cb) {
         setsockopt(connfd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (const char*)&listenfd, sizeof(int));
         hio_t* connio = hio_get(io->loop, connfd);
+        connio->connected = 1;
         connio->userdata = io->userdata;
         memcpy(connio->localaddr, io->localaddr, localaddrlen);
         memcpy(connio->peeraddr, io->peeraddr, peeraddrlen);
@@ -137,6 +138,7 @@ static void on_connectex_complete(hio_t* io) {
         hio_close(io);
         return;
     }
+    io->connected = 1;
     if (io->connect_cb) {
         setsockopt(io->fd, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
         socklen_t addrlen = sizeof(struct sockaddr_in6);
@@ -300,6 +302,7 @@ int hio_read (hio_t* io) {
 }
 
 int hio_write(hio_t* io, const void* buf, size_t len) {
+    if (!io->connected) return -1;
     int nwrite = 0;
 try_send:
     if (io->io_type == HIO_TYPE_TCP) {
@@ -380,6 +383,7 @@ disconnect:
 
 int hio_close (hio_t* io) {
     if (io->closed) return 0;
+    io->connected = 0;
     io->closed = 1;
     hio_done(io);
     if (io->hovlp) {