Skip to content

Commit e563850

Browse files
committed
Back to original blocking mode after picked up by socket selector (Fixes
1 parent 1140eb8 commit e563850

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

ext/socket/sagittarius-socket.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,9 @@ SgObject Sg_SocketConnect(SgSocket *socket, SgAddrinfo* addrinfo,
562562
SgObject timeout)
563563
{
564564
struct addrinfo *p = addrinfo->ai;
565-
int rc;
565+
int rc, nonblocking = -1;
566566
if (!SG_FALSEP(timeout)) {
567+
nonblocking = socket->nonblocking;
567568
Sg_SocketNonblocking(socket);
568569
}
569570
rc = connect(socket->socket, p->ai_addr, (int)p->ai_addrlen);
@@ -586,6 +587,9 @@ SgObject Sg_SocketConnect(SgSocket *socket, SgAddrinfo* addrinfo,
586587
goto err;
587588
}
588589
}
590+
if (nonblocking == 0) {
591+
Sg_SocketBlocking(socket);
592+
}
589593
toggle_nagle(socket->socket, 1);
590594
socket->type = SG_SOCKET_CLIENT;
591595
socket->address = SG_SOCKADDR(ai_addr(addrinfo));
@@ -594,7 +598,7 @@ SgObject Sg_SocketConnect(SgSocket *socket, SgAddrinfo* addrinfo,
594598
return socket;
595599

596600
err:
597-
if (!SG_FALSEP(timeout)) {
601+
if (nonblocking == 0) {
598602
Sg_SocketBlocking(socket);
599603
}
600604
socket->lastError = last_error;
@@ -1453,6 +1457,7 @@ static int64_t socket_read_u8(SgObject self, uint8_t *buf, int64_t size)
14531457
take sometime to flush socket even the data is continuous.
14541458
*/
14551459
long readSize = 0;
1460+
SgSocket *socket;
14561461
if (SG_PORT_HAS_U8_AHEAD(self) && size > 0) {
14571462
buf[0] = SG_PORT_U8_AHEAD(self);
14581463
SG_PORT_U8_AHEAD(self) = EOF;
@@ -1461,13 +1466,12 @@ static int64_t socket_read_u8(SgObject self, uint8_t *buf, int64_t size)
14611466
readSize++;
14621467
}
14631468
if (size == 0) return readSize;
1464-
1469+
socket = SG_PORT_SOCKET(self);
14651470
do {
14661471
/* wait a bit in case of retry (10ms?)*/
14671472
/* struct timeval tm = {0, 10000}; */
14681473
/* int ready; */
1469-
long now = Sg_SocketReceive(SG_PORT_SOCKET(self), buf + readSize,
1470-
(long)size, 0);
1474+
long now = Sg_SocketReceive(socket, buf + readSize, (long)size, 0);
14711475
if (-1 == now) {
14721476
int e = SG_PORT_SOCKET(self)->lastError;
14731477
Sg_IOReadError(SG_INTERN("read-u8"),

ext/socket/selector-win.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ static SgObject win_selector_wait(win_context_t *ctx, int n,
201201
}
202202
SG_FOR_EACH(cp, h) {
203203
SgSocket *s = SG_SOCKET(SG_CAAR(cp));
204+
ULONG val = s->nonblocking;
204205
WSAEventSelect(s->socket, NULL, 0);
206+
ioctlsocket(s->socket, FIONBIO, &val);
205207
}
206208
}
207209
ReleaseMutex(ctx->lock);

0 commit comments

Comments
 (0)