Skip to content

Commit

Permalink
cover case where write is too big for the buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
thatstoasty committed Jun 1, 2024
1 parent 78404e8 commit 30bd79c
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 288 deletions.
12 changes: 6 additions & 6 deletions gojo/fmt/fmt.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ fn format_bytes(format: String, arg: List[Byte]) -> String:

fn format_integer(format: String, arg: Int) -> String:
var verb = find_first_verb(format, List[String]("%x", "%X", "%d", "%q"))
var arg_to_place = String(arg)
var arg_to_place = str(arg)
if verb == "%x":
arg_to_place = String(convert_base10_to_base16(arg)).lower()
arg_to_place = str(convert_base10_to_base16(arg)).lower()
elif verb == "%X":
arg_to_place = String(convert_base10_to_base16(arg)).upper()
arg_to_place = str(convert_base10_to_base16(arg)).upper()
elif verb == "%q":
arg_to_place = "'" + String(arg) + "'"
arg_to_place = "'" + str(arg) + "'"

return replace_first(format, verb, arg_to_place)


fn format_float(format: String, arg: Float64) -> String:
return replace_first(format, String("%f"), arg)
return replace_first(format, str("%f"), str(arg))


fn format_boolean(format: String, arg: Bool) -> String:
Expand Down Expand Up @@ -214,6 +214,6 @@ fn printf(formatting: String, *args: Args) raises:
elif argument.isa[Bool]():
text = format_boolean(text, argument[Bool])
else:
raise Error("Unknown for argument #" + String(i))
raise Error("Unknown for argument #" + str(i))

print(text)
14 changes: 7 additions & 7 deletions gojo/io/io.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn write_string[W: Writer](inout writer: W, string: String) -> (Int, Error):
Returns:
The number of bytes written and an error, if any.
"""
return writer.write(string.as_bytes())
return writer.write(string.as_bytes_slice())


fn write_string[W: StringWriter](inout writer: W, string: String) -> (Int, Error):
Expand All @@ -34,7 +34,7 @@ fn write_string[W: StringWriter](inout writer: W, string: String) -> (Int, Error
return writer.write_string(string)


fn read_at_least[R: Reader](inout reader: R, inout dest: List[Byte], min: Int) -> (Int, Error):
fn read_at_least[R: Reader](inout reader: R, inout dest: Span[Byte], min: Int) -> (Int, Error):
"""Reads from r into buf until it has read at least min bytes.
It returns the number of bytes copied and an error if fewer bytes were read.
The error is EOF only if no bytes were read.
Expand Down Expand Up @@ -70,7 +70,7 @@ fn read_at_least[R: Reader](inout reader: R, inout dest: List[Byte], min: Int) -
return total_bytes_read, error


fn read_full[R: Reader](inout reader: R, inout dest: List[Byte]) -> (Int, Error):
fn read_full[R: Reader](inout reader: R, inout dest: Span[Byte]) -> (Int, Error):
"""Reads exactly len(buf) bytes from r into buf.
It returns the number of bytes copied and an error if fewer bytes were read.
The error is EOF only if no bytes were read.
Expand Down Expand Up @@ -132,7 +132,7 @@ fn read_full[R: Reader](inout reader: R, inout dest: List[Byte]) -> (Int, Error)
# }


# fn copy_buffer[W: Writer, R: Reader](dst: W, src: R, buf: List[Byte]) raises -> Int64:
# fn copy_buffer[W: Writer, R: Reader](dst: W, src: R, buf: Span[Byte]) raises -> Int64:
# """Actual implementation of copy and CopyBuffer.
# if buf is nil, one is allocated.
# """
Expand All @@ -152,11 +152,11 @@ fn read_full[R: Reader](inout reader: R, inout dest: List[Byte]) -> (Int, Error)
# return written


# fn copy_buffer[W: Writer, R: ReaderWriteTo](dst: W, src: R, buf: List[Byte]) -> Int64:
# fn copy_buffer[W: Writer, R: ReaderWriteTo](dst: W, src: R, buf: Span[Byte]) -> Int64:
# return src.write_to(dst)


# fn copy_buffer[W: WriterReadFrom, R: Reader](dst: W, src: R, buf: List[Byte]) -> Int64:
# fn copy_buffer[W: WriterReadFrom, R: Reader](dst: W, src: R, buf: Span[Byte]) -> Int64:
# return dst.read_from(src)

# # LimitReader returns a Reader that reads from r
Expand Down Expand Up @@ -421,7 +421,7 @@ fn read_all[R: Reader](inout reader: R) -> (List[Byte], Error):
var at_eof: Bool = False

while True:
var temp = List[Byte](capacity=BUFFER_SIZE)
var temp = Span(List[Byte](capacity=BUFFER_SIZE))
var bytes_read: Int
var err: Error
bytes_read, err = reader.read(temp)
Expand Down
8 changes: 4 additions & 4 deletions gojo/io/traits.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ trait Reader(Movable):
Implementations must not retain p."""

fn read(inout self, inout dest: List[Byte]) -> (Int, Error):
fn read(inout self, inout dest: Span[Byte]) -> (Int, Error):
...


Expand All @@ -94,7 +94,7 @@ trait Writer(Movable):
Implementations must not retain p.
"""

fn write(inout self, src: List[Byte]) -> (Int, Error):
fn write(inout self, src: Span[Byte]) -> (Int, Error):
...


Expand Down Expand Up @@ -227,7 +227,7 @@ trait ReaderAt:
Implementations must not retain p."""

fn read_at(self, inout dest: List[Byte], off: Int64) -> (Int, Error):
fn read_at(self, inout dest: Span[Byte], off: Int64) -> (Int, Error):
...


Expand All @@ -248,7 +248,7 @@ trait WriterAt:
Implementations must not retain p."""

fn write_at(self, src: List[Byte], off: Int64) -> (Int, Error):
fn write_at(self, src: Span[Byte], off: Int64) -> (Int, Error):
...


Expand Down
6 changes: 3 additions & 3 deletions gojo/net/address.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ struct TCPAddr(Addr):

fn __str__(self) -> String:
if self.zone != "":
return join_host_port(String(self.ip) + "%" + self.zone, self.port)
return join_host_port(self.ip, self.port)
return join_host_port(str(self.ip) + "%" + self.zone, str(self.port))
return join_host_port(self.ip, str(self.port))

fn network(self) -> String:
return NetworkType.tcp.value
Expand All @@ -69,7 +69,7 @@ fn resolve_internet_addr(network: String, address: String) raises -> TCPAddr:
if address != "":
var host_port = split_host_port(address)
host = host_port.host
port = host_port.port
port = str(host_port.port)
portnum = atol(port.__str__())
elif network == NetworkType.ip.value or network == NetworkType.ip4.value or network == NetworkType.ip6.value:
if address != "":
Expand Down
1 change: 0 additions & 1 deletion gojo/net/dial.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ struct Dialer:
var tcp_addr = resolve_internet_addr(network, address)
var socket = Socket(local_address=self.local_address)
socket.connect(tcp_addr.ip, tcp_addr.port)
print(String("Connected to ") + socket.remote_address)
return TCPConnection(socket^)


Expand Down
17 changes: 9 additions & 8 deletions gojo/net/ip.mojo
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from utils.variant import Variant
from utils.static_tuple import StaticTuple
from sys.info import os_is_linux, os_is_macos
from ..syscall.types import (
c_int,
Expand Down Expand Up @@ -102,15 +103,15 @@ fn get_ip_address(host: String) raises -> String:
var address_family: Int32 = 0
var address_length: UInt32 = 0
if result.isa[addrinfo]():
var addrinfo = result.get[addrinfo]()
ai_addr = addrinfo[].ai_addr
address_family = addrinfo[].ai_family
address_length = addrinfo[].ai_addrlen
var addrinfo = result[addrinfo]
ai_addr = addrinfo.ai_addr
address_family = addrinfo.ai_family
address_length = addrinfo.ai_addrlen
else:
var addrinfo = result.get[addrinfo_unix]()
ai_addr = addrinfo[].ai_addr
address_family = addrinfo[].ai_family
address_length = addrinfo[].ai_addrlen
var addrinfo = result[addrinfo_unix]
ai_addr = addrinfo.ai_addr
address_family = addrinfo.ai_family
address_length = addrinfo.ai_addrlen

if not ai_addr:
print("ai_addr is null")
Expand Down
2 changes: 1 addition & 1 deletion gojo/net/socket.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ struct Socket(FileDescriptorBase):
# Try to send all the data in the buffer. If it did not send all the data, keep trying but start from the offset of the last successful send.
while total_bytes_sent < len(src):
if attempts > max_attempts:
raise Error("Failed to send message after " + String(max_attempts) + " attempts.")
raise Error("Failed to send message after " + str(max_attempts) + " attempts.")

var bytes_sent = send(
self.sockfd.fd,
Expand Down
4 changes: 2 additions & 2 deletions gojo/net/tcp.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn resolve_internet_addr(network: String, address: String) raises -> TCPAddr:
if address != "":
var host_port = split_host_port(address)
host = host_port.host
port = host_port.port
port = str(host_port.port)
portnum = atol(port.__str__())
elif network == NetworkType.ip.value or network == NetworkType.ip4.value or network == NetworkType.ip6.value:
if address != "":
Expand All @@ -50,7 +50,7 @@ struct ListenConfig(CollectionElement):
socket.bind(tcp_addr.ip, tcp_addr.port)
socket.set_socket_option(SO_REUSEADDR, 1)
socket.listen()
print(String("Listening on ") + socket.local_address)
print(str("Listening on ") + str(socket.local_address))
return TCPListener(socket^, self, network, address)


Expand Down
Loading

0 comments on commit 30bd79c

Please sign in to comment.