@@ -87,10 +87,6 @@ trait Addr(Stringable, Representable, Writable, EqualityComparableCollectionElem
8787 fn __init__ (out self , ip : String, port : UInt16):
8888 ...
8989
90- @implicit
91- fn __init__ (out self , host_port : HostPort):
92- ...
93-
9490 fn network (self ) -> String:
9591 ...
9692
@@ -117,13 +113,6 @@ struct NoTLSListener:
117113 fn __moveinit__ (out self , owned existing : Self):
118114 self .socket = existing.socket^
119115
120- # fn __del__(owned self):
121- # logger.info("Listener cleaning up", self.socket)
122- # try:
123- # self.teardown()
124- # except e:
125- # logger.error("NoTLSListener.__del__: Failed to close connection: " + str(e))
126-
127116 fn accept (self ) raises -> TCPConnection:
128117 return TCPConnection(self .socket.accept())
129118
@@ -152,7 +141,8 @@ struct ListenConfig:
152141 network in NetworkType.SUPPORTED_TYPES ,
153142 " Unsupported network type for internet address resolution. Unix addresses are not supported yet." ,
154143 ]()
155- var addr = TCPAddr(HostPort.from_string(address))
144+ var local = parse_address(address)
145+ var addr = TCPAddr(local[0 ], local[1 ])
156146 var socket : Socket[TCPAddr]
157147 try :
158148 socket = Socket[TCPAddr]()
@@ -215,13 +205,6 @@ struct TCPConnection(Connection):
215205 fn __moveinit__ (inout self , owned existing : Self):
216206 self .socket = existing.socket^
217207
218- # fn __del__(owned self):
219- # logger.info("TCPConnection cleaning up", self.socket)
220- # try:
221- # self.teardown()
222- # except e:
223- # logger.error("TCPConnection.__del__: Failed to close connection: " + str(e))
224-
225208 fn read (self , mut buf : Bytes) raises -> Int:
226209 try :
227210 return self .socket.receive_into(buf)
@@ -409,12 +392,6 @@ struct TCPAddr(Addr):
409392 self .port = port
410393 self .zone = " "
411394
412- @implicit
413- fn __init__ (out self , host_port : HostPort):
414- self .ip = host_port.host
415- self .port = host_port.port
416- self .zone = " "
417-
418395 fn network (self ) -> String:
419396 return NetworkType.tcp.value
420397
@@ -447,54 +424,56 @@ alias MissingPortError = Error("missing port in address")
447424alias TooManyColonsError = Error(" too many colons in address" )
448425
449426
450- @value
451- struct HostPort :
452- var host : String
453- var port : UInt16
427+ fn parse_address (address : String) raises -> (String, UInt16):
428+ """ Parse an address string into a host and port.
454429
455- @ staticmethod
456- fn from_string (address : String) raises -> HostPort:
457- var colon_index = address.rfind(" :" )
458- if colon_index == - 1 :
459- raise MissingPortError
460-
461- var host : String = " "
462- var port : String = " "
463- var j : Int = 0
464- var k : Int = 0
430+ Args:
431+ address: The address string.
465432
466- if address[0 ] == " [" :
467- var end_bracket_index = address.find(" ]" )
468- if end_bracket_index == - 1 :
469- raise Error(" missing ']' in address" )
433+ Returns:
434+ A tuple containing the host and port.
435+ """
436+ var colon_index = address.rfind(" :" )
437+ if colon_index == - 1 :
438+ raise MissingPortError
470439
471- if end_bracket_index + 1 == len (address):
472- raise MissingPortError
473- elif end_bracket_index + 1 == colon_index:
474- host = address[1 :end_bracket_index]
475- j = 1
476- k = end_bracket_index + 1
477- else :
478- if address[end_bracket_index + 1 ] == " :" :
479- raise TooManyColonsError
480- else :
481- raise MissingPortError
482- else :
483- host = address[:colon_index]
484- if host.find(" :" ) != - 1 :
485- raise TooManyColonsError
440+ var host : String = " "
441+ var port : String = " "
442+ var j : Int = 0
443+ var k : Int = 0
486444
487- if address[j:].find( " [ " ) != - 1 :
488- raise Error( " unexpected '[' in address " )
489- if address[k:].find( " ] " ) ! = - 1 :
490- raise Error(" unexpected ']' in address" )
445+ if address[0 ] == " [ " :
446+ var end_bracket_index = address.find( " ] " )
447+ if end_bracket_index = = - 1 :
448+ raise Error(" missing ']' in address" )
491449
492- port = address[colon_index + 1 :]
493- if port == " " :
450+ if end_bracket_index + 1 == len (address):
494451 raise MissingPortError
495- if host == " " :
496- raise Error(" missing host" )
497- return HostPort(host, int (port))
452+ elif end_bracket_index + 1 == colon_index:
453+ host = address[1 :end_bracket_index]
454+ j = 1
455+ k = end_bracket_index + 1
456+ else :
457+ if address[end_bracket_index + 1 ] == " :" :
458+ raise TooManyColonsError
459+ else :
460+ raise MissingPortError
461+ else :
462+ host = address[:colon_index]
463+ if host.find(" :" ) != - 1 :
464+ raise TooManyColonsError
465+
466+ if address[j:].find(" [" ) != - 1 :
467+ raise Error(" unexpected '[' in address" )
468+ if address[k:].find(" ]" ) != - 1 :
469+ raise Error(" unexpected ']' in address" )
470+
471+ port = address[colon_index + 1 :]
472+ if port == " " :
473+ raise MissingPortError
474+ if host == " " :
475+ raise Error(" missing host" )
476+ return host, UInt16(int (port))
498477
499478
500479fn binary_port_to_int (port : UInt16) -> Int:
0 commit comments