1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net: add basic unix socket support (#8642)

This commit is contained in:
bettafish04 2021-02-08 23:48:23 +01:00 committed by GitHub
parent 09cff69919
commit 1a2ae0aead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -2,7 +2,9 @@ module net
// Select represents a select operation
enum Select {
read write except
read
write
except
}
// SocketType are the available sockets
@ -14,6 +16,7 @@ pub enum SocketType {
// SocketFamily are the available address families
pub enum SocketFamily {
inet = C.AF_INET
unix = C.AF_UNIX
}
struct C.in_addr {
@ -32,6 +35,11 @@ mut:
sin_addr C.in_addr
}
struct C.sockaddr_un {
mut:
sun_family int
sun_path charptr
}
struct C.addrinfo {
mut:
@ -67,9 +75,11 @@ fn C.getaddrinfo() int
fn C.connect() int
fn C.send() int
fn C.sendto() int
fn C.recv() int
fn C.recvfrom() int
fn C.shutdown() int
@ -89,11 +99,15 @@ fn C.getsockname() int
// fn C.close() int
fn C.ioctlsocket() int
fn C.fcntl() int
fn C.@select() int
fn C.FD_ZERO()
fn C.FD_SET()
fn C.FD_ISSET() bool
[typedef]

View File

@ -13,7 +13,7 @@ struct C.addrinfo {
}
pub fn (a Addr) str() string {
return '${a.saddr}:${a.port}'
return '$a.saddr:$a.port'
}
const (
@ -29,7 +29,7 @@ fn new_addr(addr C.sockaddr) ?Addr {
0
}
// Convert to string representation
buf := []byte{ len: max_ipv4_addr_len, init: 0 }
buf := []byte{len: net.max_ipv4_addr_len, init: 0}
$if windows {
res := C.WSAAddressToStringA(&addr, addr_len, C.NULL, buf.data, &buf.len)
if res != 0 {
@ -51,9 +51,7 @@ fn new_addr(addr C.sockaddr) ?Addr {
saddr = saddr.split(':')[0]
}
return Addr {
addr int(addr_len) saddr port
}
return Addr{addr, int(addr_len), saddr, port}
}
pub fn resolve_addr(addr string, family SocketFamily, typ SocketType) ?Addr {

View File

@ -3,6 +3,7 @@ module net
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <netinet/in.h>
@ -10,6 +11,7 @@ module net
#include <errno.h>
#include <fcntl.h>
#flag solaris -lsocket
fn error_code() int {
return C.errno
}