| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- #include "iowatcher.h"
- #ifndef EVENT_IOCP
- #include "hevent.h"
- #include "hsocket.h"
- #include "hssl.h"
- #include "hlog.h"
- #include "hthread.h"
- static void __connect_timeout_cb(htimer_t* timer) {
- hio_t* io = (hio_t*)timer->privdata;
- if (io) {
- char localaddrstr[SOCKADDR_STRLEN] = {0};
- char peeraddrstr[SOCKADDR_STRLEN] = {0};
- hlogw("connect timeout [%s] <=> [%s]",
- SOCKADDR_STR(io->localaddr, localaddrstr),
- SOCKADDR_STR(io->peeraddr, peeraddrstr));
- io->error = ETIMEDOUT;
- hio_close(io);
- }
- }
- static void __close_timeout_cb(htimer_t* timer) {
- hio_t* io = (hio_t*)timer->privdata;
- if (io) {
- char localaddrstr[SOCKADDR_STRLEN] = {0};
- char peeraddrstr[SOCKADDR_STRLEN] = {0};
- hlogw("close timeout [%s] <=> [%s]",
- SOCKADDR_STR(io->localaddr, localaddrstr),
- SOCKADDR_STR(io->peeraddr, peeraddrstr));
- io->error = ETIMEDOUT;
- hio_close(io);
- }
- }
- static void __accept_cb(hio_t* io) {
- hio_accept_cb(io);
- }
- static void __connect_cb(hio_t* io) {
- hio_del_connect_timer(io);
- hio_connect_cb(io);
- }
- static void __read_cb(hio_t* io, void* buf, int readbytes) {
- // printd("> %.*s\n", readbytes, buf);
- io->last_read_hrtime = io->loop->cur_hrtime;
- hio_handle_read(io, buf, readbytes);
- }
- static void __write_cb(hio_t* io, const void* buf, int writebytes) {
- // printd("< %.*s\n", writebytes, buf);
- io->last_write_hrtime = io->loop->cur_hrtime;
- hio_write_cb(io, buf, writebytes);
- }
- static void __close_cb(hio_t* io) {
- // printd("close fd=%d\n", io->fd);
- hio_del_connect_timer(io);
- hio_del_close_timer(io);
- hio_del_read_timer(io);
- hio_del_write_timer(io);
- hio_del_keepalive_timer(io);
- hio_del_heartbeat_timer(io);
- hio_close_cb(io);
- }
- static void ssl_server_handshake(hio_t* io) {
- printd("ssl server handshake...\n");
- int ret = hssl_accept(io->ssl);
- if (ret == 0) {
- // handshake finish
- iowatcher_del_event(io->loop, io->fd, HV_READ);
- io->events &= ~HV_READ;
- io->cb = NULL;
- printd("ssl handshake finished.\n");
- __accept_cb(io);
- }
- else if (ret == HSSL_WANT_READ) {
- if ((io->events & HV_READ) == 0) {
- hio_add(io, ssl_server_handshake, HV_READ);
- }
- }
- else {
- hloge("ssl handshake failed: %d", ret);
- hio_close(io);
- }
- }
- static void ssl_client_handshake(hio_t* io) {
- printd("ssl client handshake...\n");
- int ret = hssl_connect(io->ssl);
- if (ret == 0) {
- // handshake finish
- iowatcher_del_event(io->loop, io->fd, HV_READ);
- io->events &= ~HV_READ;
- io->cb = NULL;
- printd("ssl handshake finished.\n");
- __connect_cb(io);
- }
- else if (ret == HSSL_WANT_READ) {
- if ((io->events & HV_READ) == 0) {
- hio_add(io, ssl_client_handshake, HV_READ);
- }
- }
- else {
- hloge("ssl handshake failed: %d", ret);
- hio_close(io);
- }
- }
- static void nio_accept(hio_t* io) {
- // printd("nio_accept listenfd=%d\n", io->fd);
- int connfd = 0, err = 0, accept_cnt = 0;
- socklen_t addrlen;
- hio_t* connio = NULL;
- while (accept_cnt++ < 3) {
- addrlen = sizeof(sockaddr_u);
- connfd = accept(io->fd, io->peeraddr, &addrlen);
- if (connfd < 0) {
- err = socket_errno();
- if (err == EAGAIN || err == EINTR) {
- return;
- } else {
- perror("accept");
- io->error = err;
- goto accept_error;
- }
- }
- addrlen = sizeof(sockaddr_u);
- getsockname(connfd, io->localaddr, &addrlen);
- connio = hio_get(io->loop, connfd);
- // NOTE: inherit from listenio
- connio->accept_cb = io->accept_cb;
- connio->userdata = io->userdata;
- if (io->unpack_setting) {
- hio_set_unpack(connio, io->unpack_setting);
- }
- if (io->io_type == HIO_TYPE_SSL) {
- if (connio->ssl == NULL) {
- // io->ssl_ctx > g_ssl_ctx > hssl_ctx_new
- hssl_ctx_t ssl_ctx = NULL;
- if (io->ssl_ctx) {
- ssl_ctx = io->ssl_ctx;
- } else if (g_ssl_ctx) {
- ssl_ctx = g_ssl_ctx;
- } else {
- io->ssl_ctx = ssl_ctx = hssl_ctx_new(NULL);
- io->alloced_ssl_ctx = 1;
- }
- if (ssl_ctx == NULL) {
- io->error = HSSL_ERROR;
- goto accept_error;
- }
- hssl_t ssl = hssl_new(ssl_ctx, connfd);
- if (ssl == NULL) {
- io->error = HSSL_ERROR;
- goto accept_error;
- }
- connio->ssl = ssl;
- }
- hio_enable_ssl(connio);
- ssl_server_handshake(connio);
- }
- else {
- // NOTE: SSL call accept_cb after handshake finished
- __accept_cb(connio);
- }
- }
- return;
- accept_error:
- hloge("listenfd=%d accept error: %s:%d", io->fd, socket_strerror(io->error), io->error);
- hio_close(io);
- }
- static void nio_connect(hio_t* io) {
- // printd("nio_connect connfd=%d\n", io->fd);
- socklen_t addrlen = sizeof(sockaddr_u);
- int ret = getpeername(io->fd, io->peeraddr, &addrlen);
- if (ret < 0) {
- io->error = socket_errno();
- goto connect_error;
- }
- else {
- addrlen = sizeof(sockaddr_u);
- getsockname(io->fd, io->localaddr, &addrlen);
- if (io->io_type == HIO_TYPE_SSL) {
- if (io->ssl == NULL) {
- // io->ssl_ctx > g_ssl_ctx > hssl_ctx_new
- hssl_ctx_t ssl_ctx = NULL;
- if (io->ssl_ctx) {
- ssl_ctx = io->ssl_ctx;
- } else if (g_ssl_ctx) {
- ssl_ctx = g_ssl_ctx;
- } else {
- io->ssl_ctx = ssl_ctx = hssl_ctx_new(NULL);
- io->alloced_ssl_ctx = 1;
- }
- if (ssl_ctx == NULL) {
- goto connect_error;
- }
- hssl_t ssl = hssl_new(ssl_ctx, io->fd);
- if (ssl == NULL) {
- goto connect_error;
- }
- io->ssl = ssl;
- }
- ssl_client_handshake(io);
- }
- else {
- // NOTE: SSL call connect_cb after handshake finished
- __connect_cb(io);
- }
- return;
- }
- connect_error:
- hlogw("connfd=%d connect error: %s:%d\n", io->fd, socket_strerror(io->error), io->error);
- hio_close(io);
- }
- static int __nio_read(hio_t* io, void* buf, int len) {
- int nread = 0;
- switch (io->io_type) {
- case HIO_TYPE_SSL:
- nread = hssl_read(io->ssl, buf, len);
- break;
- case HIO_TYPE_TCP:
- #ifdef OS_UNIX
- nread = read(io->fd, buf, len);
- #else
- nread = recv(io->fd, buf, len, 0);
- #endif
- break;
- case HIO_TYPE_UDP:
- case HIO_TYPE_KCP:
- case HIO_TYPE_IP:
- {
- socklen_t addrlen = sizeof(sockaddr_u);
- nread = recvfrom(io->fd, buf, len, 0, io->peeraddr, &addrlen);
- }
- break;
- default:
- nread = read(io->fd, buf, len);
- break;
- }
- // hlogd("read retval=%d", nread);
- return nread;
- }
- static int __nio_write(hio_t* io, const void* buf, int len) {
- int nwrite = 0;
- switch (io->io_type) {
- case HIO_TYPE_SSL:
- nwrite = hssl_write(io->ssl, buf, len);
- break;
- case HIO_TYPE_TCP:
- #ifdef OS_UNIX
- nwrite = write(io->fd, buf, len);
- #else
- nwrite = send(io->fd, buf, len, 0);
- #endif
- break;
- case HIO_TYPE_UDP:
- case HIO_TYPE_KCP:
- case HIO_TYPE_IP:
- nwrite = sendto(io->fd, buf, len, 0, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
- break;
- default:
- nwrite = write(io->fd, buf, len);
- break;
- }
- // hlogd("write retval=%d", nwrite);
- return nwrite;
- }
- static void nio_read(hio_t* io) {
- // printd("nio_read fd=%d\n", io->fd);
- void* buf;
- int len = 0, nread = 0, err = 0;
- read:
- buf = io->readbuf.base + io->readbuf.tail;
- if (io->read_flags & HIO_READ_UNTIL_LENGTH) {
- len = io->read_until_length - (io->readbuf.tail - io->readbuf.head);
- } else {
- len = io->readbuf.len - io->readbuf.tail;
- }
- assert(len > 0);
- nread = __nio_read(io, buf, len);
- // printd("read retval=%d\n", nread);
- if (nread < 0) {
- err = socket_errno();
- if (err == EAGAIN) {
- // goto read_done;
- return;
- } else if (err == EMSGSIZE) {
- // ignore
- return;
- } else {
- // perror("read");
- io->error = err;
- goto read_error;
- }
- }
- if (nread == 0) {
- goto disconnect;
- }
- io->readbuf.tail += nread;
- __read_cb(io, buf, nread);
- // if (nread == len) goto read;
- return;
- read_error:
- disconnect:
- hio_close(io);
- }
- static void nio_write(hio_t* io) {
- // printd("nio_write fd=%d\n", io->fd);
- int nwrite = 0, err = 0;
- hrecursive_mutex_lock(&io->write_mutex);
- write:
- if (write_queue_empty(&io->write_queue)) {
- hrecursive_mutex_unlock(&io->write_mutex);
- if (io->close) {
- io->close = 0;
- hio_close(io);
- }
- return;
- }
- offset_buf_t* pbuf = write_queue_front(&io->write_queue);
- char* base = pbuf->base;
- char* buf = base + pbuf->offset;
- int len = pbuf->len - pbuf->offset;
- nwrite = __nio_write(io, buf, len);
- // printd("write retval=%d\n", nwrite);
- if (nwrite < 0) {
- err = socket_errno();
- if (err == EAGAIN) {
- hrecursive_mutex_unlock(&io->write_mutex);
- return;
- } else {
- // perror("write");
- io->error = err;
- goto write_error;
- }
- }
- if (nwrite == 0) {
- goto disconnect;
- }
- pbuf->offset += nwrite;
- io->write_bufsize -= nwrite;
- __write_cb(io, buf, nwrite);
- if (nwrite == len) {
- // NOTE: after write_cb, pbuf maybe invalid.
- // HV_FREE(pbuf->base);
- HV_FREE(base);
- write_queue_pop_front(&io->write_queue);
- if (!io->closed) {
- // write continue
- goto write;
- }
- }
- hrecursive_mutex_unlock(&io->write_mutex);
- return;
- write_error:
- disconnect:
- hrecursive_mutex_unlock(&io->write_mutex);
- hio_close(io);
- }
- static void hio_handle_events(hio_t* io) {
- if ((io->events & HV_READ) && (io->revents & HV_READ)) {
- if (io->accept) {
- nio_accept(io);
- }
- else {
- nio_read(io);
- }
- }
- if ((io->events & HV_WRITE) && (io->revents & HV_WRITE)) {
- // NOTE: del HV_WRITE, if write_queue empty
- hrecursive_mutex_lock(&io->write_mutex);
- if (write_queue_empty(&io->write_queue)) {
- iowatcher_del_event(io->loop, io->fd, HV_WRITE);
- io->events &= ~HV_WRITE;
- }
- hrecursive_mutex_unlock(&io->write_mutex);
- if (io->connect) {
- // NOTE: connect just do once
- // ONESHOT
- io->connect = 0;
- nio_connect(io);
- }
- else {
- nio_write(io);
- }
- }
- io->revents = 0;
- }
- int hio_accept(hio_t* io) {
- io->accept = 1;
- return hio_add(io, hio_handle_events, HV_READ);
- }
- int hio_connect(hio_t* io) {
- int ret = connect(io->fd, io->peeraddr, SOCKADDR_LEN(io->peeraddr));
- #ifdef OS_WIN
- if (ret < 0 && socket_errno() != WSAEWOULDBLOCK) {
- #else
- if (ret < 0 && socket_errno() != EINPROGRESS) {
- #endif
- perror("connect");
- hio_close(io);
- return ret;
- }
- if (ret == 0) {
- // connect ok
- nio_connect(io);
- return 0;
- }
- int timeout = io->connect_timeout ? io->connect_timeout : HIO_DEFAULT_CONNECT_TIMEOUT;
- io->connect_timer = htimer_add(io->loop, __connect_timeout_cb, timeout, 1);
- io->connect_timer->privdata = io;
- io->connect = 1;
- return hio_add(io, hio_handle_events, HV_WRITE);
- }
- int hio_read (hio_t* io) {
- if (io->closed) {
- hloge("hio_read called but fd[%d] already closed!", io->fd);
- return -1;
- }
- hio_add(io, hio_handle_events, HV_READ);
- if (io->readbuf.tail > io->readbuf.head &&
- io->unpack_setting == NULL &&
- io->read_flags == 0) {
- hio_read_remain(io);
- }
- return 0;
- }
- int hio_write (hio_t* io, const void* buf, size_t len) {
- if (io->closed) {
- hloge("hio_write called but fd[%d] already closed!", io->fd);
- return -1;
- }
- int nwrite = 0, err = 0;
- hrecursive_mutex_lock(&io->write_mutex);
- #if WITH_KCP
- if (io->io_type == HIO_TYPE_KCP) {
- nwrite = hio_write_kcp(io, buf, len);
- // if (nwrite < 0) goto write_error;
- goto write_done;
- }
- #endif
- if (write_queue_empty(&io->write_queue)) {
- try_write:
- nwrite = __nio_write(io, buf, len);
- // printd("write retval=%d\n", nwrite);
- if (nwrite < 0) {
- err = socket_errno();
- if (err == EAGAIN) {
- nwrite = 0;
- hlogw("try_write failed, enqueue!");
- goto enqueue;
- } else {
- // perror("write");
- io->error = err;
- goto write_error;
- }
- }
- if (nwrite == 0) {
- goto disconnect;
- }
- if (nwrite == len) {
- goto write_done;
- }
- enqueue:
- hio_add(io, hio_handle_events, HV_WRITE);
- }
- if (nwrite < len) {
- if (io->write_bufsize + len - nwrite > MAX_WRITE_BUFSIZE) {
- if (io->write_bufsize > MAX_WRITE_BUFSIZE) {
- hloge("write bufsize > %u, close it!", (unsigned int)MAX_WRITE_BUFSIZE);
- goto write_error;
- }
- }
- offset_buf_t remain;
- remain.len = len - nwrite;
- remain.offset = 0;
- // NOTE: free in nio_write
- HV_ALLOC(remain.base, remain.len);
- memcpy(remain.base, ((char*)buf) + nwrite, remain.len);
- if (io->write_queue.maxsize == 0) {
- write_queue_init(&io->write_queue, 4);
- }
- write_queue_push_back(&io->write_queue, &remain);
- io->write_bufsize += remain.len;
- if (io->write_bufsize > WRITE_BUFSIZE_HIGH_WATER) {
- hlogw("write len=%d enqueue %u, bufsize=%u over high water %u",
- len, (unsigned int)(remain.len - remain.offset),
- (unsigned int)io->write_bufsize,
- (unsigned int)WRITE_BUFSIZE_HIGH_WATER);
- }
- }
- write_done:
- hrecursive_mutex_unlock(&io->write_mutex);
- if (nwrite > 0) {
- __write_cb(io, buf, nwrite);
- }
- return nwrite;
- write_error:
- disconnect:
- hrecursive_mutex_unlock(&io->write_mutex);
- /* NOTE:
- * We usually free resources in hclose_cb,
- * if hio_close_sync, we have to be very careful to avoid using freed resources.
- * But if hio_close_async, we do not have to worry about this.
- */
- hio_close_async(io);
- return nwrite < 0 ? nwrite : -1;
- }
- int hio_close (hio_t* io) {
- if (io->closed) return 0;
- if (hv_gettid() != io->loop->tid) {
- return hio_close_async(io);
- }
- hrecursive_mutex_lock(&io->write_mutex);
- if (io->closed) {
- hrecursive_mutex_unlock(&io->write_mutex);
- return 0;
- }
- if (!write_queue_empty(&io->write_queue) && io->error == 0 && io->close == 0) {
- io->close = 1;
- hrecursive_mutex_unlock(&io->write_mutex);
- hlogw("write_queue not empty, close later.");
- int timeout_ms = io->close_timeout ? io->close_timeout : HIO_DEFAULT_CLOSE_TIMEOUT;
- io->close_timer = htimer_add(io->loop, __close_timeout_cb, timeout_ms, 1);
- io->close_timer->privdata = io;
- return 0;
- }
- io->closed = 1;
- hrecursive_mutex_unlock(&io->write_mutex);
- hio_done(io);
- __close_cb(io);
- if (io->ssl) {
- hssl_free(io->ssl);
- io->ssl = NULL;
- }
- if (io->ssl_ctx && io->alloced_ssl_ctx) {
- hssl_ctx_free(io->ssl_ctx);
- io->ssl_ctx = NULL;
- }
- if (io->io_type & HIO_TYPE_SOCKET) {
- closesocket(io->fd);
- }
- return 0;
- }
- #endif
|