ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) { int ret; ssize_t n; while ((n = g_sys_sendmsg(sockfd, msg, flags)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(sockfd, EVENT_WRITABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(SENDMSG_TIMEOUT); del_fd_event(sockfd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t sendfile_timeout(int out_fd, int in_fd, off_t *offset, size_t count, int timeout) { int ret; ssize_t n; while ((n = sendfile(out_fd, in_fd, offset, count)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(out_fd, EVENT_WRITABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(timeout * 1000); del_fd_event(out_fd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t writev(int fd, const struct iovec *iov, int iovcnt) { int ret; ssize_t n; while ((n = g_sys_writev(fd, iov, iovcnt)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(fd, EVENT_WRITABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(WRITEV_TIMEOUT); del_fd_event(fd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { int ret; ssize_t n; while ((n = g_sys_sendto(sockfd, buf, len, flags, dest_addr, addrlen)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(sockfd, EVENT_WRITABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(SENDTO_TIMEOUT); del_fd_event(sockfd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t read(int fd, void *buf, size_t count) { int ret; ssize_t n; while ((n = g_sys_read(fd, buf, count)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(fd, EVENT_READABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(READ_TIMEOUT); del_fd_event(fd, EVENT_READABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) { int ret; ssize_t n; while ((n = g_sys_recvfrom(sockfd, buf, len, flags, src_addr, addrlen)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(sockfd, EVENT_READABLE, event_rw_callback, current_coro()); if (ret) return -2; schedule_timeout(RECVFROM_TIMEOUT); del_fd_event(sockfd, EVENT_READABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
/* in_fd一般就是文件fd, out_fd一定要是远端连接fd */ ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count) { ssize_t n; while ((n = g_sys_sendfile(out_fd, in_fd, offset, count)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; if (add_fd_event(out_fd, EVENT_WRITABLE, event_rw_callback, current_coro())) return -2; schedule_timeout(SENDFILE_TIMEOUT); del_fd_event(out_fd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t send(int sockfd, const void *buf, size_t len, int flags) { ssize_t n; while ((n = g_sys_send(sockfd, buf, len, flags)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; if (add_fd_event(sockfd, EVENT_WRITABLE, event_rw_callback, current_coro())) return -2; schedule_timeout(SEND_TIMEOUT); del_fd_event(sockfd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t write(int fd, const void *buf, size_t count) { ssize_t n; while ((n = g_sys_write(fd, buf, count)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; if (add_fd_event(fd, EVENT_WRITABLE, event_rw_callback, current_coro())) return -2; schedule_timeout(WRITE_TIMEOUT); del_fd_event(fd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) { ssize_t n; while ((n = g_sys_recvmsg(sockfd, msg, flags)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; if (add_fd_event(sockfd, EVENT_READABLE, event_rw_callback, current_coro())) return -2; schedule_timeout(RECVMSG_TIMEOUT); del_fd_event(sockfd, EVENT_READABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } return n; }
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { int ret; int connfd = 0; while ((connfd = g_sys_accept(sockfd, addr, addrlen)) < 0) { if (EINTR == errno) continue; if (!fd_not_ready()) return -1; ret = add_fd_event(sockfd, EVENT_READABLE, event_conn_callback, current_coro()); if (ret) return -2; schedule_timeout(ACCEPT_TIMEOUT); del_fd_event(sockfd, EVENT_READABLE); if (is_wakeup_by_timeout()) { errno = ETIME; return -3; } } ret = set_nonblock(connfd); if (ret) { close(connfd); return -4; } ret = enable_tcp_no_delay(connfd); if (ret) { close(connfd); return -5; } ret = set_keep_alive(connfd, KEEP_ALIVE); if (ret) { close(connfd); return -6; } return connfd; }
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) { int ret; int flags; socklen_t len; set_nonblock(sockfd); /* connect < 0 && errno == EINPROGRESS才需要跟踪fd是否可写 否则其他情形都是错误的, 直接返回 */ ret = g_sys_connect(sockfd, addr, addrlen); if (0 == ret) //succ return 0; if (ret < 0 && errno != EINPROGRESS) return -1; ret = add_fd_event(sockfd, EVENT_WRITABLE, event_conn_callback, current_coro()); if (ret) return -2; schedule_timeout(CONN_TIMEOUT); del_fd_event(sockfd, EVENT_WRITABLE); if (is_wakeup_by_timeout()) { errno = ETIMEDOUT; return -3; } ret = getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &flags, &len); if (ret == -1 || flags || !len) { if (flags) errno = flags; return -4; } return 0; }