This commit is contained in:
aozhiwei 2019-06-03 16:52:54 +08:00
parent c6af863cf2
commit f9222e376f

View File

@ -149,11 +149,9 @@ namespace a8
void TcpClient2::SetActive(bool active)
{
if (active) {
if (IsActive()) {
return;
}
ActiveStart();
} else {
ActiveStop();
ActiveStop();
}
}
@ -185,22 +183,33 @@ namespace a8
flags = ::fcntl(socket_, F_GETFL, 0);
::fcntl(socket_, F_SETFL, flags|O_NONBLOCK);
}
//add epoll
{
struct epoll_event ev;
ev.data.fd = socket_;
ev.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLERR;
ev.data.ptr = this;
int n = ::epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_, &ev);
assert(n == 0);
if (n != 0) {
abort();
}
}
sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(remote_address.c_str());
sa.sin_port = htons(remote_port);
if (::connect(socket_, (sockaddr*)&sa, sizeof(sa)) < 0) {
if (on_error) {
on_error(this, errno);
int ret = ::connect(socket_, (sockaddr*)&sa, sizeof(sa));
if (ret < 0) {
if (errno != EINPROGRESS) {
if (on_error) {
on_error(this, errno);
}
::close(socket_);
socket_ = INVALID_SOCKET;
return false;
}
::close(socket_);
socket_ = INVALID_SOCKET;
return false;
}
connected_ = true;
if (on_connect) {
on_connect(this);
}
return true;
}