| 85 | } |
| 86 | |
| 87 | int mt_tcp_create_sock(void) |
| 88 | { |
| 89 | int fd; |
| 90 | int flag; |
| 91 | |
| 92 | fd = ::socket(AF_INET, SOCK_STREAM, 0); |
| 93 | if (fd < 0) |
| 94 | { |
| 95 | MTLOG_ERROR("create tcp socket failed, error: %m"); |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | flag = fcntl(fd, F_GETFL, 0); |
| 100 | if (flag == -1) |
| 101 | { |
| 102 | ::close(fd); |
| 103 | MTLOG_ERROR("get fd flags failed, error: %m"); |
| 104 | return -2; |
| 105 | } |
| 106 | |
| 107 | if (flag & O_NONBLOCK) |
| 108 | return fd; |
| 109 | |
| 110 | if (fcntl(fd, F_SETFL, flag | O_NONBLOCK | O_NDELAY) == -1) |
| 111 | { |
| 112 | ::close(fd); |
| 113 | MTLOG_ERROR("set fd flags failed, error: %m"); |
| 114 | return -3; |
| 115 | } |
| 116 | |
| 117 | return fd; |
| 118 | } |
| 119 | |
| 120 | static TcpKeepConn* mt_tcp_get_keep_conn(struct sockaddr_in* dst, int& sock) |
| 121 | { |
no test coverage detected