Technical Consultation: modifyEvent prompts Invalid argument
HalcyonHuang opened this issue · 12 comments
HalcyonHuang commented
你好,我用的 ZLToolKit/tests/test_tcpClient.cpp的demo,连接tcp服务时,modifyEvent 函数 提示 Invalid argument,请问这种情况正常吗
git commit :04d1c47d2568f5ce1ff84260cefaf2754e514a5e
2024-04-18 10:52:38.787 D [test_tcpClient] [17486-stamp thread] util.cpp:366 operator() | Stamp thread started
2024-04-18 10:52:38.789 I [test_tcpClient] [17486-test_tcpClient] EventPoller.cpp:503 EventPollerPool | EventPoller created size: 2
2024-04-18 10:52:38.789 D [test_tcpClient] [17486-test_tcpClient] test_tcpClient.cpp:22 TestClient |
2024-04-18 10:52:38.790 T [test_tcpClient] [17486-test_tcpClient] TcpClient.cpp:79 startConnect | TestClient-1 start connect 192.168.0.120:8082
2024-04-18 10:52:38.791 I [test_tcpClient] [17486-event poller 1] Socket.cpp:238 attachEvent | attachEvent rawFd 9 type0
2024-04-18 10:52:38.792 T [test_tcpClient] [17486-event poller 1] TcpClient.cpp:89 onSockConnect | TestClient-1 connect result: 0(success)
2024-04-18 10:52:38.792 I [test_tcpClient] [17486-event poller 1] test_tcpClient.cpp:30 onConnect | success
2024-04-18 10:52:38.792 E [test_tcpClient] [17486-event poller 1] Socket.cpp:262 operator() | Event_Write rawFd 9 events 2
2024-04-18 10:52:38.792 E [test_tcpClient] [17486-event poller 1] EventPoller.cpp:184 modifyEvent | stop write event error _epoll_fd 8 fd 9 events 5 ret -1 Invalid argument //错误提示
bool Socket::attachEvent(const SockNum::Ptr &sock) {
weak_ptr<Socket> weak_self = shared_from_this();
InfoL << "attachEvent rawFd " << sock->rawFd() << " type" << sock->type();
if (sock->type() == SockNum::Sock_TCP_Server) {
// tcp服务器
auto result = _poller->addEvent(sock->rawFd(), EventPoller::Event_Read | EventPoller::Event_Error, [weak_self, sock](int event) {
if (auto strong_self = weak_self.lock()) {
strong_self->onAccept(sock, event);
}
});
return -1 != result;
}
// tcp客户端或udp
auto read_buffer = _poller->getSharedBuffer();
auto result = _poller->addEvent(sock->rawFd(), EventPoller::Event_Read | EventPoller::Event_Error | EventPoller::Event_Write, [weak_self, sock, read_buffer](int event) {
auto strong_self = weak_self.lock();
if (!strong_self) {
return;
}
if (event & EventPoller::Event_Read) {
ErrorL << " Event_Read rawFd " << sock->rawFd() << " events " << event ;//新增打印
strong_self->onRead(sock, read_buffer);
}
if (event & EventPoller::Event_Write) {
ErrorL << " Event_Write rawFd " << sock->rawFd() << " events " << event ;//新增打印
strong_self->onWriteAble(sock);
}
if (event & EventPoller::Event_Error) {
ErrorL << " Event_Error rawFd " << sock->rawFd() << " events " << event ;//新增打印
strong_self->emitErr(getSockErr(sock->rawFd()));
}
});
return -1 != result;
}
int EventPoller::modifyEvent(int fd, int event, PollCompleteCB cb) {
TimeTicker();
if (!cb) {
cb = [](bool success) {};
}
if (isCurrentThread()) {
#if defined(HAS_EPOLL)
struct epoll_event ev = { 0 };
ev.events = toEpoll(event);
ev.data.fd = fd;
auto ret = epoll_ctl(_epoll_fd, EPOLL_CTL_MOD, fd, &ev);
if(ret !=0){ //新增打印
ErrorL << " stop write event error _epoll_fd " << _epoll_fd << " fd " << fd << " events " << event << " ret " << ret << " " << strerror(errno);
}
cb(ret == 0);
return ret;
#else
#endif // HAS_EPOLL
}
return 0;
}
xia-chu commented
应该没啥问题 这个fd可能已经被关闭了 不用理会
HalcyonHuang commented
我也觉得奇怪,可能是我哪里没搞对吧。
@xia-chu fd 是指tcp客户端的socket吗,tcp客户端的socket没有关闭,接收到tcp服务器发来的数据,会同时触发读和写事件,日志如下:
2024-04-19 18:27:28.602 D [test_tcpClient] [89747-stamp thread] util.cpp:366 operator() | Stamp thread started
2024-04-19 18:27:28.604 I [test_tcpClient] [89747-test_tcpClient] EventPoller.cpp:507 EventPollerPool | EventPoller created size: 2
2024-04-19 18:27:28.604 D [test_tcpClient] [89747-test_tcpClient] test_tcpClient.cpp:22 TestClient |
2024-04-19 18:27:28.604 T [test_tcpClient] [89747-test_tcpClient] TcpClient.cpp:79 startConnect | TestClient-1 start connect 192.168.0.120:8082
2024-04-19 18:27:28.605 I [test_tcpClient] [89747-event poller 1] Socket.cpp:238 attachEvent | attachEvent rawFd 9 type0
2024-04-19 18:27:28.605 T [test_tcpClient] [89747-event poller 1] TcpClient.cpp:89 onSockConnect | TestClient-1 connect result: 0(success)
2024-04-19 18:27:28.605 I [test_tcpClient] [89747-event poller 1] test_tcpClient.cpp:30 onConnect | success
2024-04-19 18:27:28.605 E [test_tcpClient] [89747-event poller 1] Socket.cpp:262 operator() | Event_Write rawFd 9 events 2
2024-04-19 18:27:28.605 E [test_tcpClient] [89747-event poller 1] EventPoller.cpp:188 modifyEvent | stop write event error _epoll_fd 8 fd 9 events 5 ret -1 Invalid argument
接收到服务器发送的数据:
2024-04-19 18:27:43.562 E [test_tcpClient] [89747-event poller 1] Socket.cpp:258 operator() | Event_Read rawFd 9 events 3
2024-04-19 18:27:43.563 D [test_tcpClient] [89747-event poller 1] test_tcpClient.cpp:34 onRecv | 123 from port:8082
2024-04-19 18:27:43.563 E [test_tcpClient] [89747-event poller 1] Socket.cpp:262 operator() | Event_Write rawFd 9 events 3
2024-04-19 18:27:43.563 E [test_tcpClient] [89747-event poller 1] EventPoller.cpp:188 modifyEvent | stop write event error _epoll_fd 8 fd 9 events 5 ret -1 Invalid argument