From 61b51f61495c2b6a0d4d265ab80414745b745306 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 30 Jun 2019 16:11:55 +0200 Subject: [PATCH] os.File clean up + socket.v (dial()) --- compiler/cgen.v | 2 +- compiler/main.v | 3 +- compiler/parser.v | 2 +- vlib/net/socket.v | 78 ++++++++++++++++++++++++++++++++++++++++++ vlib/net/socket_test.v | 8 +++++ vlib/os/os.v | 10 +++--- 6 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 vlib/net/socket.v create mode 100644 vlib/net/socket_test.v diff --git a/compiler/cgen.v b/compiler/cgen.v index ece393f7eb..9bae6058bc 100644 --- a/compiler/cgen.v +++ b/compiler/cgen.v @@ -68,7 +68,7 @@ fn (g mut CGen) gen(s string) { fn (g mut CGen) save() { s := g.lines.join('\n') - g.out.appendln(s) + g.out.writeln(s) g.out.close() } diff --git a/compiler/main.v b/compiler/main.v index fc42f105e7..93f31bd58c 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -823,7 +823,6 @@ fn new_v(args []string) *V { files << f } } - obfuscate := args.contains('-obf') return &V { os: _os out_name: out_name @@ -839,7 +838,7 @@ fn new_v(args []string) *V { is_play: args.contains('play') is_prod: args.contains('-prod') is_verbose: args.contains('-verbose') - obfuscate: obfuscate + obfuscate: args.contains('-obf') is_prof: args.contains('-prof') is_live: args.contains('-live') sanitize: args.contains('-sanitize') diff --git a/compiler/parser.v b/compiler/parser.v index 57d2fd53ba..bc77ca9241 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -236,7 +236,7 @@ fn (p mut Parser) parse() { } if true && !p.first_run() && p.fileis('test') { out := os.create('/var/tmp/fmt.v') - out.appendln(p.scanner.fmt_out.str()) + out.writeln(p.scanner.fmt_out.str()) out.close() } return diff --git a/vlib/net/socket.v b/vlib/net/socket.v new file mode 100644 index 0000000000..1660248cbb --- /dev/null +++ b/vlib/net/socket.v @@ -0,0 +1,78 @@ +module net + +#include +#include +#include +#include +#include + +struct Conn { +pub: + sockfd int +} + +struct Listener { + listener int + their_addr voidptr +} + +import const ( + AF_UNSPEC + SOCK_STREAM + AI_PASSIVE +) + +struct C.addrinfo { +mut: + ai_family int + ai_socktype int + ai_flags int + ai_protocol int + ai_addrlen int + ai_next voidptr + ai_addr voidptr +} + +fn dial(addr string, port int) Conn { + println('net.dial("$addr":$port)') + mut hints := C.addrinfo{} + servinfo := &C.addrinfo{!} + mut rp := &C.addrinfo{!} + mut sockfd := -1 + // allow IPv4 or IPv6 + hints.ai_family = AF_UNSPEC + // avoid name lookup for port + // hints.ai_flags = AI_NUMERICSERV + hints.ai_socktype = SOCK_STREAM + hints.ai_flags = AI_PASSIVE + strport := '$port' + connbad := Conn{} + c_addr := addr.cstr() + rv := C.getaddrinfo(c_addr, strport.cstr(), &hints, &servinfo) + if rv != 0 { + println('Getaddrinfo failed ') + return connbad + } + // Loop through all the results and connect to the first we can + for rp = servinfo; !isnil(rp); rp = rp.ai_next { + sockfd = C.socket(rp.ai_family, rp.ai_socktype, rp.ai_protocol) + if sockfd == -1 { + continue + } + if C.connect(sockfd, rp.ai_addr, rp.ai_addrlen) { + C.close(sockfd) + continue + } + break + } + if sockfd == -1 { + println('socket: Cannot connect to host') + return connbad + } + C.freeaddrinfo(servinfo) + conn := Conn { + sockfd: sockfd, + } + return conn +} + diff --git a/vlib/net/socket_test.v b/vlib/net/socket_test.v new file mode 100644 index 0000000000..27b73b5684 --- /dev/null +++ b/vlib/net/socket_test.v @@ -0,0 +1,8 @@ +import net + +fn test_dial() { + +conn := net.dial('irc.freenode.org', 6667) +println(conn.sockfd) + +} diff --git a/vlib/os/os.v b/vlib/os/os.v index 23375c00be..dcea1fc1b4 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -188,7 +188,7 @@ fn create_file2(file, mode string) File { return res } -fn (f File) append(s string) { +pub fn (f File) write(s string) { ss := s.clone() C.fputs(ss.cstr(), f.cfile) // ss.free() @@ -208,7 +208,7 @@ fn (f File) write_bytes_at(data voidptr, size, pos int) { C.fseek(f.cfile, 0, SEEK_END) } -pub fn (f File) appendln(s string) { +pub fn (f File) writeln(s string) { // C.fwrite(s.str, 1, s.len, f.cfile) // ss := s.clone() // TODO perf @@ -446,7 +446,7 @@ pub fn home_dir() string { // write_file writes text data to a file in `path`. pub fn write_file(path, text string) { f := os.create(path) - f.appendln(text) + f.writeln(text) f.close() } @@ -505,7 +505,7 @@ pub fn is_dir(path string) bool { } } -fn chdir(path string) { +pub fn chdir(path string) { $if windows { C._chdir(path.cstr()) } @@ -555,7 +555,7 @@ pub fn ls(path string) []string { fn log(s string) { } -fn print_backtrace() { +pub fn print_backtrace() { /* # void *buffer[100]; nptrs := 0