Fix some socket bugs and improve code.

This commit is contained in:
Sönke Ludwig 2016-10-07 19:39:13 +02:00
parent 45931f2c17
commit e2814315e4

View file

@ -151,6 +151,8 @@ abstract class PosixEventDriver : EventDriver,
registerFD(sock, EventMask.read|EventMask.write|EventMask.status); registerFD(sock, EventMask.read|EventMask.write|EventMask.status);
initFD(sock);
auto ret = () @trusted { return connect(sock, address.name, address.nameLen); } (); auto ret = () @trusted { return connect(sock, address.name, address.nameLen); } ();
if (ret == 0) { if (ret == 0) {
on_connect(sock, ConnectStatus.connected); on_connect(sock, ConnectStatus.connected);
@ -162,14 +164,14 @@ abstract class PosixEventDriver : EventDriver,
} }
startNotify!(EventType.write)(sock, &onConnect); startNotify!(EventType.write)(sock, &onConnect);
} else { } else {
clearFD(sock);
unregisterFD(sock); unregisterFD(sock);
invalidateSocket(); invalidateSocket();
on_connect(sock, ConnectStatus.unknownError); on_connect(sock, ConnectStatus.unknownError);
return sock;
} }
} }
initFD(sock);
return sock; return sock;
} }
@ -206,8 +208,12 @@ abstract class PosixEventDriver : EventDriver,
} else log("Success!"); } else log("Success!");
} (); } ();
if (on_accept && sock != StreamListenSocketFD.invalid) if (sock == StreamListenSocketFD.invalid)
waitForConnections(sock, on_accept); return sock;
initFD(sock);
if (on_accept) waitForConnections(sock, on_accept);
return sock; return sock;
} }
@ -216,9 +222,9 @@ abstract class PosixEventDriver : EventDriver,
{ {
log("wait for conn"); log("wait for conn");
registerFD(sock, EventMask.read); registerFD(sock, EventMask.read);
initFD(sock);
m_fds[sock].acceptCallback = on_accept; m_fds[sock].acceptCallback = on_accept;
startNotify!(EventType.read)(sock, &onAccept); startNotify!(EventType.read)(sock, &onAccept);
onAccept(sock);
} }
private void onAccept(FD listenfd) private void onAccept(FD listenfd)
@ -270,8 +276,6 @@ abstract class PosixEventDriver : EventDriver,
} }
} }
size_t bytes_read = 0;
if (ret == 0) { if (ret == 0) {
on_read_finish(socket, IOStatus.disconnected, 0); on_read_finish(socket, IOStatus.disconnected, 0);
return; return;
@ -283,10 +287,9 @@ abstract class PosixEventDriver : EventDriver,
} }
if (ret > 0) { if (ret > 0) {
bytes_read += ret; buffer = buffer[ret .. $];
buffer = buffer[bytes_read .. $];
if (mode != IOMode.all || buffer.length == 0) { if (mode != IOMode.all || buffer.length == 0) {
on_read_finish(socket, IOStatus.ok, bytes_read); on_read_finish(socket, IOStatus.ok, ret);
return; return;
} }
} }
@ -504,7 +507,7 @@ abstract class PosixEventDriver : EventDriver,
auto sock = cast(DatagramSocketFD)createSocket(bind_address.addressFamily, SOCK_DGRAM); auto sock = cast(DatagramSocketFD)createSocket(bind_address.addressFamily, SOCK_DGRAM);
if (sock == -1) return DatagramSocketFD.invalid; if (sock == -1) return DatagramSocketFD.invalid;
if (() @trusted { return bind(sock, bind_address.name, bind_address.nameLen); } () != 0) { if (bind_address && () @trusted { return bind(sock, bind_address.name, bind_address.nameLen); } () != 0) {
closeSocket(sock); closeSocket(sock);
return DatagramSocketFD.init; return DatagramSocketFD.init;
} }
@ -537,9 +540,7 @@ abstract class PosixEventDriver : EventDriver,
on_receive_finish(socket, IOStatus.error, 0, null); on_receive_finish(socket, IOStatus.error, 0, null);
return; return;
} }
}
if (ret < 0) {
if (mode == IOMode.immediate) { if (mode == IOMode.immediate) {
on_receive_finish(socket, IOStatus.wouldBlock, 0, null); on_receive_finish(socket, IOStatus.wouldBlock, 0, null);
} else { } else {
@ -607,9 +608,7 @@ abstract class PosixEventDriver : EventDriver,
on_send_finish(socket, IOStatus.error, 0, null); on_send_finish(socket, IOStatus.error, 0, null);
return; return;
} }
}
if (ret < 0) {
if (mode == IOMode.immediate) { if (mode == IOMode.immediate) {
on_send_finish(socket, IOStatus.wouldBlock, 0, null); on_send_finish(socket, IOStatus.wouldBlock, 0, null);
} else { } else {