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

net: ipv6 support, merge unix+ip;[pack:x] attribute (#9904)

This commit is contained in:
Emily Hudson
2021-06-13 21:53:38 +01:00
committed by GitHub
parent fa9fa77a5f
commit 535dcac8fa
52 changed files with 1277 additions and 524 deletions

View File

@@ -83,7 +83,6 @@ fn (mut ws Client) shutdown_socket() ? {
} else {
ws.conn.close() ?
}
return none
}
// dial_socket connects tcp socket and initializes default configurations

View File

@@ -4,7 +4,7 @@ module main
import x.websocket
fn main() {
mut s := websocket.new_server(9002, '/')
mut s := websocket.new_server(.ip6, 9002, '/')
s.on_message(on_message)
s.listen() or { panic(err) }
}

View File

@@ -15,6 +15,7 @@ mut:
message_callbacks []MessageEventHandler // new message callback functions
close_callbacks []CloseEventHandler // close message callback functions
pub:
family net.AddrFamily = .ip
port int // port used as listen to incoming connections
is_ssl bool // true if secure connection (not supported yet on server)
pub mut:
@@ -34,9 +35,10 @@ pub mut:
}
// new_server instance a new websocket server on provided port and route
pub fn new_server(port int, route string) &Server {
pub fn new_server(family net.AddrFamily, port int, route string) &Server {
return &Server{
ls: 0
family: family
port: port
logger: &log.Log{
level: .info
@@ -53,7 +55,7 @@ pub fn (mut s Server) set_ping_interval(seconds int) {
// listen start listen and process to incoming connections from websocket clients
pub fn (mut s Server) listen() ? {
s.logger.info('websocket server: start listen on port $s.port')
s.ls = net.listen_tcp(s.port) ?
s.ls = net.listen_tcp(s.family, ':$s.port') ?
s.set_state(.open)
go s.handle_ping()
for {

View File

@@ -1,31 +1,51 @@
import os
import net
import x.websocket
import time
import rand
// TODO: fix connecting to ipv4 websockets
// (the server seems to work with .ip, but
// Client can not connect, it needs to be passed
// .ip too?)
struct WebsocketTestResults {
pub mut:
nr_messages int
nr_pong_received int
}
// Do not run these tests everytime, since they are flaky.
// They have their own specialized CI runner.
const github_job = os.getenv('GITHUB_JOB')
const should_skip = github_job != '' && github_job != 'websocket_tests'
// tests with internal ws servers
fn test_ws() {
if github_job != '' && github_job != 'websocket_tests' {
// Do not run these tests everytime, since they are flaky.
// They have their own specialized CI runner.
fn test_ws_ipv6() {
if should_skip {
return
}
port := 30000 + rand.intn(1024)
go start_server(port)
go start_server(.ip6, port)
time.sleep(500 * time.millisecond)
ws_test('ws://localhost:$port') or { assert false }
ws_test(.ip6, 'ws://localhost:$port') or { assert false }
}
fn start_server(listen_port int) ? {
mut s := websocket.new_server(listen_port, '')
// tests with internal ws servers
fn test_ws_ipv4() {
// TODO: fix client
if true || should_skip {
return
}
port := 30000 + rand.intn(1024)
go start_server(.ip, port)
time.sleep(500 * time.millisecond)
ws_test(.ip, 'ws://localhost:$port') or { assert false }
}
fn start_server(family net.AddrFamily, listen_port int) ? {
mut s := websocket.new_server(family, listen_port, '')
// make that in execution test time give time to execute at least one time
s.ping_interval = 1
@@ -52,7 +72,7 @@ fn start_server(listen_port int) ? {
}
// ws_test tests connect to the websocket server from websocket client
fn ws_test(uri string) ? {
fn ws_test(family net.AddrFamily, uri string) ? {
eprintln('connecting to $uri ...')
mut test_results := WebsocketTestResults{}