| 73 | } |
| 74 | |
| 75 | int UdpSessionNtfy::CreateSocket() |
| 76 | { |
| 77 | int osfd = socket(AF_INET, SOCK_DGRAM, 0); |
| 78 | if (osfd < 0) |
| 79 | { |
| 80 | MTLOG_ERROR("socket create failed, errno %d(%s)", errno, strerror(errno)); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | int flags = 1; |
| 85 | if (ioctl(osfd, FIONBIO, &flags) < 0) |
| 86 | { |
| 87 | MTLOG_ERROR("socket unblock failed, errno %d(%s)", errno, strerror(errno)); |
| 88 | close(osfd); |
| 89 | osfd = -1; |
| 90 | return -2; |
| 91 | } |
| 92 | |
| 93 | if (_local_addr.sin_port != 0) |
| 94 | { |
| 95 | int ret = bind(osfd, (struct sockaddr *)&_local_addr, sizeof(_local_addr)); |
| 96 | if (ret < 0) |
| 97 | { |
| 98 | MTLOG_ERROR("socket bind(%s:%d) failed, errno %d(%s)", inet_ntoa(_local_addr.sin_addr), |
| 99 | ntohs(_local_addr.sin_port), errno, strerror(errno)); |
| 100 | close(osfd); |
| 101 | osfd = -1; |
| 102 | return -3; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | this->SetOsfd(osfd); |
| 107 | this->EnableInput(); |
| 108 | MtFrame* frame = MtFrame::Instance(); |
| 109 | frame->KqueueNtfyReg(osfd, this); |
| 110 | frame->KqueueCtrlAdd(osfd, KQ_EVENT_READ); |
| 111 | |
| 112 | return osfd; |
| 113 | } |
| 114 | |
| 115 | void UdpSessionNtfy::CloseSocket() |
| 116 | { |
no test coverage detected