hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

ip.ha (1015B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 // Provides default dialers for tcp and udp
      5 use net;
      6 use net::tcp;
      7 use net::udp;
      8 
      9 fn dial_tcp(addr: str, service: str) (net::socket | error) = {
     10 	const result = resolve("tcp", addr, service)?;
     11 	const addrs = result.0, port = result.1;
     12 	for (let i = 0z; i < len(addrs); i += 1) {
     13 		const addr = addrs[i];
     14 		match (tcp::connect(addr, port)) {
     15 		case let conn: net::socket =>
     16 			return conn;
     17 		case let err: net::error =>
     18 			if (i + 1 >= len(addrs)) {
     19 				return err;
     20 			};
     21 		};
     22 	};
     23 	abort(); // Unreachable
     24 };
     25 
     26 fn dial_udp(addr: str, service: str) (net::socket | error) = {
     27 	const result = resolve("udp", addr, service)?;
     28 	const addrs = result.0, port = result.1;
     29 	for (let i = 0z; i < len(addrs); i += 1) {
     30 		const addr = addrs[i];
     31 		match (udp::connect(addr, port)) {
     32 		case let sock: net::socket =>
     33 			return sock;
     34 		case let err: net::error =>
     35 			if (i + 1 >= len(addrs)) {
     36 				return err;
     37 			};
     38 		};
     39 	};
     40 	abort(); // Unreachable
     41 };