Outgoing connections now work in mingw driver.
This commit is contained in:
@@ -33,23 +33,54 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static SOCKET open_connection(const std::string &target) {
|
||||
SOCKET open_connection(const std::string &target, std::string &err) {
|
||||
PADDRINFOA addrs = nullptr;
|
||||
PADDRINFOA goodaddr = nullptr;
|
||||
SOCKET sock = INVALID_SOCKET;
|
||||
std::string host, port;
|
||||
util::split_host_port(target, host, port);
|
||||
PADDRINFOA addrs;
|
||||
int status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
|
||||
assert(status == 0);
|
||||
PADDRINFOA goodaddr = find_good_addr(addrs);
|
||||
assert(goodaddr != nullptr);
|
||||
freeaddrinfo(addrs);
|
||||
SOCKET sock = socket(goodaddr->ai_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
assert(sock != INVALID_SOCKET);
|
||||
u_long mode = 1; // 1 to enable non-blocking socket
|
||||
|
||||
err = "";
|
||||
util::split_host_port(target, host, port);
|
||||
int status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
|
||||
while (status == WSATRY_AGAIN) {
|
||||
status = getaddrinfo(host.c_str(), port.c_str(), nullptr, &addrs);
|
||||
goto error;
|
||||
}
|
||||
if (status == WSAHOST_NOT_FOUND) {
|
||||
err = "host not found";
|
||||
goto error;
|
||||
}
|
||||
if (status != 0) {
|
||||
err = "DNS resolution hard failure";
|
||||
goto error;
|
||||
}
|
||||
goodaddr = find_good_addr(addrs);
|
||||
if (goodaddr == nullptr) {
|
||||
err = "host not an internet host";
|
||||
goto error;
|
||||
}
|
||||
sock = socket(goodaddr->ai_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
assert(sock != INVALID_SOCKET);
|
||||
status = ioctlsocket(sock, FIONBIO, &mode);
|
||||
assert(status == 0);
|
||||
status = connect(sock, goodaddr->ai_addr, goodaddr->ai_addrlen);
|
||||
assert(status == 0);
|
||||
if (status != 0) {
|
||||
int errcode = WSAGetLastError();
|
||||
if (errcode != WSAEWOULDBLOCK) {
|
||||
std::ostringstream oss;
|
||||
oss << "Error " << errcode;
|
||||
err = oss.str();
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
freeaddrinfo(addrs);
|
||||
return sock;
|
||||
|
||||
error:
|
||||
if (sock != INVALID_SOCKET) closesocket(sock);
|
||||
if (addrs != nullptr) freeaddrinfo(addrs);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
|
||||
void init(DrivenEngine *de) {
|
||||
@@ -87,9 +118,15 @@ public:
|
||||
driven_->drv_get_new_outgoing(chans);
|
||||
for (int chid : chans) {
|
||||
assert(socket_[chid] == INVALID_SOCKET);
|
||||
SOCKET sock = open_connection(driven_->drv_get_target(chid));
|
||||
socket_[chid] = sock;
|
||||
connected_[chid] = false;
|
||||
std::string err;
|
||||
SOCKET sock = open_connection(driven_->drv_get_target(chid), err);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
driven_->drv_notify_close(chid);
|
||||
engine_wakeup_ = true;
|
||||
} else {
|
||||
socket_[chid] = sock;
|
||||
connected_[chid] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,12 +228,7 @@ public:
|
||||
timeout.tv_sec = mstimeout / 1000;
|
||||
timeout.tv_usec = (mstimeout - (timeout.tv_sec*1000)) * 1000;
|
||||
int status = select(1, &rfds, &wfds, &efds, &timeout);
|
||||
if (status == SOCKET_ERROR) {
|
||||
int err = WSAGetLastError();
|
||||
std::cerr << "ERR:" << err << std::endl;
|
||||
}
|
||||
assert(status != SOCKET_ERROR);
|
||||
|
||||
for (int chid = 1; chid < MAX_CHAN; chid++) {
|
||||
SOCKET sock = socket_[chid];
|
||||
if (sock == INVALID_SOCKET) continue;
|
||||
@@ -215,7 +247,7 @@ public:
|
||||
}
|
||||
if (FD_ISSET(sock, &rfds)) {
|
||||
int nrecv = recv(sock, chbuf.get(), 65536, 0);
|
||||
if (nrecv == SOCKET_ERROR) {
|
||||
if ((nrecv == SOCKET_ERROR) || (nrecv == 0)) {
|
||||
close_socket(chid);
|
||||
continue;
|
||||
} else {
|
||||
@@ -234,7 +266,7 @@ public:
|
||||
init(de);
|
||||
DrivenEngine::set(de);
|
||||
driven_->drv_set_lua_source(util::read_lua_source("lua"));
|
||||
driven_->drv_invoke_event_update();
|
||||
driven_->drv_invoke_event_init();
|
||||
while (!de->drv_get_stop_driver()) {
|
||||
engine_wakeup_ = false;
|
||||
handle_lua_source();
|
||||
|
||||
Reference in New Issue
Block a user