Skip to content

Commit 129c13c

Browse files
authored
Merge pull request #17 from savi-lang/deprecate/pending-reads
Deprecate the `TCP.Engine.pending_reads` method.
2 parents 192ace1 + d30c3ca commit 129c13c

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

spec/TCP.Spec.savi

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@
5454
|
5555
@env.err.print("[Echoer] Failed to get local and/or remote address")
5656
)
57+
5758
| IO.Action.Read |
58-
@io.pending_reads -> (bytes_available |
59-
bytes val = @io.read_stream.extract_all
60-
@env.err.print("[Echoer] Received: \(Inspect[bytes])")
61-
@io.write_stream << bytes
62-
try @io.flush! // TODO: should we flush automatically on close below?
63-
@io.close
64-
)
59+
bytes val = @io.read_stream.extract_all
60+
@env.err.print("[Echoer] Received: \(Inspect[bytes])")
61+
@io.write_stream << bytes
62+
try @io.flush! // TODO: should we flush automatically on close below?
63+
@io.close
64+
6565
| IO.Action.Closed |
6666
@env.err.print("[Echoer] Closed")
6767
@listener.dispose // ask the listener to close too
@@ -105,12 +105,10 @@
105105
@env.err.print(@io.connect_error.name)
106106

107107
| IO.Action.Read |
108-
@io.pending_reads -> (bytes_available |
109-
if (bytes_available >= b"Hello, World!".size) (
110-
bytes val = @io.read_stream.extract_all
111-
@env.err.print("[EchoClient] Received: \(Inspect[bytes])")
112-
@io.close
113-
)
108+
if (@io.read_stream.bytes_ahead_of_marker >= b"Hello, World!".size) (
109+
bytes val = @io.read_stream.extract_all
110+
@env.err.print("[EchoClient] Received: \(Inspect[bytes])")
111+
@io.close
114112
)
115113

116114
| IO.Action.Closed |

src/TCP.Engine.savi

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,21 @@
5151
// This allows the listener to keep an accurate count of how many
5252
// connections have been opened so far.
5353
try @_listener.as!(IO.Actor(IO.Action)).io_deferred_action(IO.Action.ClosedChild)
54-
55-
// TODO: windows complete writes, flush-after-mute (pending writes logic from Pony)
56-
// | IO.Action.Write |
57-
// ...
54+
yield action
55+
56+
| IO.Action.Read |
57+
if Platform.is_windows (
58+
None // TODO: @_windows_complete_reads(arg)
59+
|
60+
@_pending_reads_unix -> (yield action)
61+
)
62+
63+
// TODO: windows complete writes, flush-after-mute (pending writes logic from Pony)
64+
// | IO.Action.Write |
65+
// ...
66+
|
67+
yield action
5868
)
59-
yield action
6069
)
6170
@
6271

@@ -67,21 +76,27 @@
6776
:fun ref flush!
6877
@write_stream.flush!
6978

79+
// TODO: Remove this deprecated method.
80+
:: DEPRECATED: There's no need for this method anymore.
81+
:: Loading pending reads into the read stream will now be handled
82+
:: automatically by the TCP engine, which will yield an `IO.Action.Read`
83+
:: action for each time a pending read is completed.
84+
::
85+
:: So you can just remove this function call from your code and de-indent
86+
:: the body of your yield block, such that it becomes part of the outer code.
87+
:: If you need to know the number of bytes available, then you can call
88+
:: `io.read_stream.bytes_ahead_of_marker` to find out.
7089
:fun ref pending_reads
7190
:yields USize for None
72-
if Platform.is_windows (
73-
None // TODO: @_windows_complete_reads(arg)
74-
|
75-
@_pending_reads_unix -> (bytes_available | yield bytes_available)
76-
)
91+
yield @read_stream.bytes_ahead_of_marker
7792
@
7893

7994
:fun ref _pending_reads_unix None
80-
:yields USize for None
95+
:yields None for None
8196
while @io.is_readable (
8297
try (
8398
bytes_read = @read_stream.receive_from!(@io)
84-
if (bytes_read > 0) (yield @read_stream.bytes_ahead_of_marker)
99+
if (bytes_read > 0) (yield None)
85100
)
86101
)
87102

0 commit comments

Comments
 (0)