Skip to content

Commit

Permalink
Merge pull request #7 from savi-lang/update/latest-savi
Browse files Browse the repository at this point in the history
Update for breaking `AsioEvent` changes in latest Savi version.
  • Loading branch information
jemc authored Mar 19, 2022
2 parents 5a9a3a6 + b25b195 commit ceb940d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
8 changes: 4 additions & 4 deletions spec/TCP.Spec.savi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

:be dispose: @io.close

:fun ref _io_react(action IO.Action)
:fun ref io_react(action IO.Action)
case action == (
| IO.Action.Opened |
TCP.Spec.EchoClient.new(@env, Inspect[@io.listen_port_number])
Expand All @@ -36,7 +36,7 @@
@io = TCP.Engine.accept(@, --ticket)
@env.err.print("[Echoer] Accepted")

:fun ref _io_react(action IO.Action)
:fun ref io_react(action IO.Action)
case action == (
| IO.Action.Read |
@io.pending_reads -> (bytes_available |
Expand All @@ -63,15 +63,15 @@
TCP.auth(@env.root).connect.to("localhost", port)
)

// TODO: Can we make this trigger _io_react with IO.Action.OpenFailed
// TODO: Can we make this trigger io_react with IO.Action.OpenFailed
// automatically via the same mechanism we will use for queuing later
// pending reads, instead of checking for this error case here?
if (@io.connect_error != OSError.None) (
@env.err.print("[EchoClient] Failed to connect:")
@env.err.print(@io.connect_error.name)
)

:fun ref _io_react(action IO.Action)
:fun ref io_react(action IO.Action)
case action == (
| IO.Action.Opened |
@env.err.print("[EchoClient] Connected")
Expand Down
2 changes: 1 addition & 1 deletion src/TCP.Accept.Ticket.savi
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
:: your program will eventually be unable to accept additional sockets.
:fun iso reject
@_listener.io_deferred_action(IO.Action.ClosedChild)
_LibPonyOS.pony_os_socket_close(@_fd)
_FFI.pony_os_socket_close(@_fd)
4 changes: 2 additions & 2 deletions src/TCP.Engine.savi
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
@write_stream = ByteStream.Writer.new(@io)
@_listener = ticket._listener

:fun ref react(event CPointer(AsioEvent), flags U32, arg U32) @
:fun ref react(event AsioEvent) @
:yields IO.Action
@io.react(event, flags, arg) -> (action |
@io.react(event) -> (action |
case action == (
| IO.Action.Closed |
// If this TCP connection is the child of a TCP listener, then let
Expand Down
30 changes: 15 additions & 15 deletions src/TCP.Listen.Engine.savi
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

:let _actor IO.Actor(IO.Action)
:var _fd U32: -1
:var _event CPointer(AsioEvent): CPointer(AsioEvent).null
:var _event_id AsioEvent.ID: CPointer(AsioEvent.ID.Opaque).null // TODO: AsioEvent.ID.null

:var _count USize: 0
:var _limit USize
Expand All @@ -15,30 +15,30 @@
:fun listen_port_number: _NetAddress._for_fd(@_fd).port // TODO: what happens if @_fd is invalid (-1)?

:new (@_actor, ticket TCP.Listen.Ticket, @_limit = 0)
event = _LibPonyOS.pony_os_listen_tcp(
event = _FFI.pony_os_listen_tcp(
@_actor
ticket.host.cstring
ticket.port.cstring
)
if event.is_not_null (
@_event = event
@_fd = AsioEvent.fd(@_event)
@_event_id = event
@_fd = _FFI.pony_asio_event_fd(@_event_id)
@_actor.io_deferred_action(IO.Action.Opened)
|
@listen_error = _LibPonyOS.pony_os_errno
@listen_error = _FFI.pony_os_errno
@_closed = True
@_actor.io_deferred_action(IO.Action.OpenFailed)
)

// TODO: Delegate much of the AsioEvent internals to an `IO.CoreEngine`.
:fun ref react(event CPointer(AsioEvent), flags U32, arg U32) @
if (@_event === event) (
if AsioEvent.is_readable(flags) (
:fun ref react(event AsioEvent) @
if (event.id === @_event_id) (
if event.is_readable (
yield IO.Action.Read
)
if AsioEvent.is_disposable(flags) (
AsioEvent.destroy(@_event)
@_event = CPointer(AsioEvent).null
if event.is_disposable (
_FFI.pony_asio_event_destroy(@_event_id)
@_event_id = CPointer(AsioEvent.ID.Opaque).null // TODO: AsioEvent.ID.null
yield IO.Action.Closed
)
)
Expand All @@ -52,7 +52,7 @@
if @_closed.not (
try (
while (@_limit == 0 || @_count < @_limit) (
conn_fd = _LibPonyOS.pony_os_accept(@_event)
conn_fd = _FFI.pony_os_accept(@_event_id)
case conn_fd == (
| 0 | error! // EWOULDBLOCK, don't try again
| -1 | None // Some other error, so we can try again
Expand Down Expand Up @@ -82,11 +82,11 @@
@

:fun ref close
if (@_closed.not && @_event.is_not_null) (
if (@_closed.not && @_event_id.is_not_null) (
// When not on windows, unsubscribe immediately here instead of later.
if Platform.is_windows.not AsioEvent.unsubscribe(@_event)
if Platform.is_windows.not _FFI.pony_asio_event_unsubscribe(@_event_id)

_LibPonyOS.pony_os_socket_close(@_fd)
_FFI.pony_os_socket_close(@_fd)
@_fd = -1
)
@
12 changes: 12 additions & 0 deletions src/_FFI.savi
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:module _FFI
:ffi pony_asio_event_fd(event AsioEvent.ID) U32
:ffi pony_asio_event_unsubscribe(event AsioEvent.ID) None
:ffi pony_asio_event_destroy(event AsioEvent.ID) None

:ffi pony_os_listen_tcp(owner AsioEvent.Actor, host CPointer(U8), service CPointer(U8)) AsioEvent.ID
:ffi pony_os_accept(event AsioEvent.ID) U32
:ffi pony_os_socket_close(fd U32) None
:ffi pony_os_errno OSError
:ffi pony_os_sockname(fd U32, net_addr _NetAddress'ref) None
:ffi pony_os_ipv4(net_addr _NetAddress'box) Bool
:ffi pony_os_ipv6(net_addr _NetAddress'box) Bool
12 changes: 0 additions & 12 deletions src/_Lib.savi

This file was deleted.

16 changes: 8 additions & 8 deletions src/_NetAddress.savi
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
:let _ipv6d U32: 0 :: Bits 97-128 of an IPv6 address in network byte order.
:let _scope U32: 0 :: IPv6 scope (unicast, anycast, multicast, etc...).

:new _for_fd(fd): _LibPonyOS.pony_os_sockname(fd, @)
:new _for_fd(fd): _FFI.pony_os_sockname(fd, @)

:fun is_ipv4: _LibPonyOS.pony_os_ipv4(@)
:fun is_ipv6: _LibPonyOS.pony_os_ipv6(@)
:fun is_ipv4: _FFI.pony_os_ipv4(@)
:fun is_ipv6: _FFI.pony_os_ipv6(@)

:fun port: _LibC.ntohs(@_port) // (converted to host byte order)
:fun scope: _LibC.ntohl(@_scope) // (converted to host byte order)
:fun ipv4_addr: _LibC.ntohl(@_ipv4) // (converted to host byte order)
// TODO: ipv6_addr (needs tuple return value)
// TODO: family (needs Platform.is_big_endian)
:fun port: @_port.be_to_native // (converted to host byte order)
:fun scope: @_scope.be_to_native // (converted to host byte order)
:fun ipv4_addr: @_ipv4.be_to_native // (converted to host byte order)
// TODO: ipv6_addr
// TODO: family

:fun "=="(other _NetAddress'box)
@_family == other._family
Expand Down

0 comments on commit ceb940d

Please sign in to comment.