Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/net/tcp_connection.pony
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,8 @@ actor TCPConnection

if not _connected and not _closed then
// We don't have a connection yet.
if @pony_os_connected[Bool](fd) then
let errnum = @pony_os_socket_error[U32](fd)
if errnum == 0 then
// The connection was successful, make it ours.
_fd = fd
_event = event
Expand All @@ -375,7 +376,7 @@ actor TCPConnection
// The connection failed, unsubscribe the event and close.
@pony_asio_event_unsubscribe(event)
@pony_os_socket_close[None](fd)
_notify_connecting()
_notify_connecting(errnum)
end
else
// We're already connected, unsubscribe the event and close.
Expand Down Expand Up @@ -655,14 +656,16 @@ actor TCPConnection
end
end

fun ref _notify_connecting() =>
fun ref _notify_connecting(errnum: U32 = 0) =>
"""
Inform the notifier that we're connecting.
`errnum` is equal to 0 if no connection attemps has been made.
Otherwise it has errno value of failed connection.
"""
if _connect_count > 0 then
_notify.connecting(this, _connect_count)
else
_notify.connect_failed(this)
_notify.connect_error(this, errnum)
_hard_close()
end

Expand Down
7 changes: 7 additions & 0 deletions packages/net/tcp_connection_notify.pony
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ interface TCPConnectionNotify
"""
None

fun ref connect_error(conn: TCPConnection ref, errnum: U32) =>
"""
Extended version of `connect_failed` which exposes connection error.
`errnum` indicated errno value of failed connection.
"""
this.connect_failed(conn)

fun ref auth_failed(conn: TCPConnection ref) =>
"""
A raw TCPConnection has no authentication mechanism. However, when
Expand Down
13 changes: 7 additions & 6 deletions src/libponyrt/lang/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,16 @@ int pony_os_accept(asio_event_t* ev)
}

// Check this when a connection gets its first writeable event.
bool pony_os_connected(int fd)
// NOTE: error clears after the call
int pony_os_socket_error(int fd)
{
int val = 0;
socklen_t len = sizeof(int);
int sockerr;
socklen_t errlen = sizeof(sockerr);

if(getsockopt((SOCKET)fd, SOL_SOCKET, SO_ERROR, (char*)&val, &len) == -1)
return false;
if(getsockopt((SOCKET)fd, SOL_SOCKET, SO_ERROR, (char*)&sockerr, &errlen))
return errno;

return val == 0;
return sockerr;
}

static int address_family(int length)
Expand Down