| 831 | } |
| 832 | |
| 833 | int32_t CSockLink::CreateSock() |
| 834 | { |
| 835 | if (_fd > 0) |
| 836 | { |
| 837 | return _fd; |
| 838 | } |
| 839 | |
| 840 | if (NET_PROTO_TCP == _proto_type) |
| 841 | { |
| 842 | _fd = socket(AF_INET, SOCK_STREAM, 0); |
| 843 | } |
| 844 | else |
| 845 | { |
| 846 | _fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 847 | } |
| 848 | |
| 849 | if (_fd < 0) |
| 850 | { |
| 851 | MTLOG_ERROR("create socket failed, ret %d[%m]", _fd); |
| 852 | return -1; |
| 853 | } |
| 854 | |
| 855 | int flags = 1; |
| 856 | if (ioctl(_fd, FIONBIO, &flags) < 0) |
| 857 | { |
| 858 | MTLOG_ERROR("socket unblock failed, %m"); |
| 859 | close(_fd); |
| 860 | _fd = -1; |
| 861 | return -2; |
| 862 | } |
| 863 | |
| 864 | if (NET_PROTO_TCP == _proto_type) |
| 865 | { |
| 866 | setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags)); |
| 867 | this->EnableOutput(); |
| 868 | } |
| 869 | |
| 870 | this->EnableInput(); |
| 871 | if (!MtFrame::Instance()->KqueueAddObj(this)) |
| 872 | { |
| 873 | MTLOG_ERROR("socket epoll mng failed, %m"); |
| 874 | close(_fd); |
| 875 | _fd = -1; |
| 876 | return -3; |
| 877 | } |
| 878 | |
| 879 | return _fd; |
| 880 | } |
| 881 | |
| 882 | struct sockaddr_in* CSockLink::GetDestAddr(struct sockaddr_in* addr) |
| 883 | { |
no test coverage detected