From: Brendan Hansen Date: Mon, 4 Apr 2022 15:18:19 +0000 (-0500) Subject: cleaning up before first push X-Git-Tag: v0.0.1~5 X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=ce1dae373dbb48d2d3cd4f7b0ce5c5a391449e02;p=onyx_net.git cleaning up before first push --- diff --git a/README.md b/README.md new file mode 100644 index 0000000..afb5049 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Onyx-Net + +Stable and reliable networking protocol on top of UDP for +games and applications. It is based off of ENet, but is not compatible +in the slightest with it. + +A single server is connected to by multiple clients using a UDP Socket +each. On each socket, multiple channels are used. Each channel supports: + - Sequencing + - Reliable transmissions + - Commands (control codes) + +These are supported independent of the other channels. This enable the +programmer to use sequencial packets for data that needs to arrive in +order, and unsequencial / unreliable packets for things like movement +updates. + +This protocol will also support larger transmissions, though for +practically, assume that anything over 1MB in size should probably be +transferred in a different way, or chunked up by the application layer. + + +Currently, these features are not supported: +[ ] Bundle acknowledgement messages together. + +[ ] Packets are not necessarily sequenced. A dropped packet followed + by a successful packet with be out of order. + +[x] Framemented reliable data. diff --git a/doc.md b/doc.md deleted file mode 100644 index 3aa18ff..0000000 --- a/doc.md +++ /dev/null @@ -1,34 +0,0 @@ -# Onyx-Net - -Stable and reliable networking protocol on top of UDP for -games and applications. It is based off of ENet, but is not compatible -in the slightest with it. - -A single server is connected to by multiple clients using a UDP Socket -each. On each socket, multiple channels are used. Each channel supports: - - Sequencing - - Reliable transmissions - - Commands (control codes) -These are supported independent of the other channels. This enable the -programmer to use sequencial packets for data that needs to arrive in -order, and unsequencial / unreliable packets for things like movement -updates. - -This protocol will also support larger transmissions, though for -practically, assume that anything over 1MB in size should probably be -transferred in a different way, or chunked up by the application layer. - - - -Each packet is at most 1400 bytes in size. - -0 2 4 6 8 ... -| peerID | sentTime | checksum | channelID | commandID | data - OPTIONAL OPTIONAL - - -Currently, these features are not supported: - [ ] Packets are not necessarily sequenced. A dropped packet followed - by a successful packet with be out of order. - - [x] Framemented reliable data. \ No newline at end of file diff --git a/example/udp_client.onyx b/example/udp_client.onyx new file mode 100644 index 0000000..507545a --- /dev/null +++ b/example/udp_client.onyx @@ -0,0 +1,106 @@ +#load "core/std" +#load "src/module" + +use package core +onyx_net :: package onyx_net + +octets_to_addr :: (a, b, c, d: u8) -> u32 { + return (cast(u32) a << 24) | + (cast(u32) b << 16) | + (cast(u32) c << 8) | + (cast(u32) d << 0); +} + +main :: (args) => { + // + // 'null' signifies that this host should not bind a socket + // to a port. Only 1 peer is needed, as this is only connecting + // to one server. + host: ^onyx_net.Host; + if host', err := onyx_net.host_create(null, 1); err != .None { + println(err); + } + + addr: net.Socket_Address; + addr.addr = octets_to_addr(127, 0, 0, 1); + addr.port = 8080; + peer := onyx_net.host_connect(host, ^addr, 2); + + input_thread: thread.Thread; + td_type :: struct { peer: ^onyx_net.Peer; } + td := td_type.{ peer }; + + thread.spawn(^input_thread, ^td, (data) => { + peer := data.peer; + input_reader := io.reader_make(^stdin); + + while true { + line := io.read_line(^input_reader) |> string.strip_whitespace(); + + switch line { + case "disconnect" { + println("Disconnecting"); + onyx_net.peer_send_disconnect(peer); + onyx_net.peer_flush_outgoing_commands(peer); + onyx_net.peer_disconnect(peer); + break; + } + + case "send" { + packet := new(onyx_net.Packet); + packet.flags |= .Reliable; + packet.data = "What's Up?"; + println("Sending what's up..."); + + onyx_net.peer_send(peer, 0, packet); + } + } + } + }); + + while true { + for host->get_events(timeout=100) { + // printf("{*}\n", it); + + if it.type == .Connection { + println("Successfully connected to server!"); + } + + if it.type == .Disconnection { + println("Lost connection to server."); + break break; + } + + if it.type == .Message { + print("Server says: "); + print(it.data); + print("\n"); + } + + // if it.type == .Message { + // packet := new(onyx_net.Packet); + // packet.flags |= .Reliable; + // packet.data = "HELLO"; + + // onyx_net.peer_send(it.peer, 0, packet); + // } + } + + // if random.between(0, 3) == 0 { + // packet := new(onyx_net.Packet); + // packet.flags |= .Reliable; + // packet.data = "What's Up?"; + // println("Sending what's up..."); + + // onyx_net.peer_send(peer, 0, packet); + // } + + // if random.between(0, 100) == 4 { + // println("Disconnecting"); + // onyx_net.peer_send_disconnect(peer); + // onyx_net.peer_flush_outgoing_commands(peer); + // onyx_net.peer_disconnect(peer); + // break; + // } + } +} diff --git a/example/udp_server.onyx b/example/udp_server.onyx new file mode 100644 index 0000000..184127a --- /dev/null +++ b/example/udp_server.onyx @@ -0,0 +1,46 @@ +#load "core/std" +#load "src/module" + +use package core +onyx_net :: package onyx_net + + +main :: (args) => { + addr: net.Socket_Address; + addr.port = 8080; + + server, err := onyx_net.host_create(^addr, 32); + if err != .None { + println(err); + return; + } + + while true { + for server->get_events(timeout = 2000) { + // printf("{*}\n", it); + + if it.type == .Connection { + printf("New connection from: {}\n", it.peer.addr); + packet := new(onyx_net.Packet); + packet.flags |= .Reliable; + packet.data = "Welcome!!!"; + + onyx_net.host_broadcast(server, 0, packet); + } + + if it.type == .Disconnection { + printf("Disconnection: {}\n", it.peer.addr); + } + + if it.type == .Message { + if it.data == "What's Up?" { + packet := new(onyx_net.Packet); + packet.flags |= .Reliable; + packet.data = #file_contents "src/host.onyx"; + + onyx_net.peer_send(it.peer, 0, packet); + } + } + } + } +} diff --git a/src/host.onyx b/src/host.onyx index 3ab8264..978a0b8 100644 --- a/src/host.onyx +++ b/src/host.onyx @@ -68,6 +68,7 @@ host_create :: (addr: ^net.Socket_Address, peer_count: u32) -> (^Host, Host_Crea host.current_time = ~~ (os.time() & cast(u64) 0xFFFFFFFF); for^ peer: host.peers { + memory.set(peer, 0, sizeof typeof *peer); peer.host = host; peer.state = .Disconnected; peer.channels = .{ null, 0 }; diff --git a/src/peer.onyx b/src/peer.onyx index fb3535a..e40d929 100644 --- a/src/peer.onyx +++ b/src/peer.onyx @@ -15,8 +15,8 @@ Peer_State :: enum { Peer :: struct { host: ^Host; - addr: net.Socket_Address; state: Peer_State; + addr: net.Socket_Address; channels: [] Channel; diff --git a/udp_client.onyx b/udp_client.onyx deleted file mode 100644 index 507545a..0000000 --- a/udp_client.onyx +++ /dev/null @@ -1,106 +0,0 @@ -#load "core/std" -#load "src/module" - -use package core -onyx_net :: package onyx_net - -octets_to_addr :: (a, b, c, d: u8) -> u32 { - return (cast(u32) a << 24) | - (cast(u32) b << 16) | - (cast(u32) c << 8) | - (cast(u32) d << 0); -} - -main :: (args) => { - // - // 'null' signifies that this host should not bind a socket - // to a port. Only 1 peer is needed, as this is only connecting - // to one server. - host: ^onyx_net.Host; - if host', err := onyx_net.host_create(null, 1); err != .None { - println(err); - } - - addr: net.Socket_Address; - addr.addr = octets_to_addr(127, 0, 0, 1); - addr.port = 8080; - peer := onyx_net.host_connect(host, ^addr, 2); - - input_thread: thread.Thread; - td_type :: struct { peer: ^onyx_net.Peer; } - td := td_type.{ peer }; - - thread.spawn(^input_thread, ^td, (data) => { - peer := data.peer; - input_reader := io.reader_make(^stdin); - - while true { - line := io.read_line(^input_reader) |> string.strip_whitespace(); - - switch line { - case "disconnect" { - println("Disconnecting"); - onyx_net.peer_send_disconnect(peer); - onyx_net.peer_flush_outgoing_commands(peer); - onyx_net.peer_disconnect(peer); - break; - } - - case "send" { - packet := new(onyx_net.Packet); - packet.flags |= .Reliable; - packet.data = "What's Up?"; - println("Sending what's up..."); - - onyx_net.peer_send(peer, 0, packet); - } - } - } - }); - - while true { - for host->get_events(timeout=100) { - // printf("{*}\n", it); - - if it.type == .Connection { - println("Successfully connected to server!"); - } - - if it.type == .Disconnection { - println("Lost connection to server."); - break break; - } - - if it.type == .Message { - print("Server says: "); - print(it.data); - print("\n"); - } - - // if it.type == .Message { - // packet := new(onyx_net.Packet); - // packet.flags |= .Reliable; - // packet.data = "HELLO"; - - // onyx_net.peer_send(it.peer, 0, packet); - // } - } - - // if random.between(0, 3) == 0 { - // packet := new(onyx_net.Packet); - // packet.flags |= .Reliable; - // packet.data = "What's Up?"; - // println("Sending what's up..."); - - // onyx_net.peer_send(peer, 0, packet); - // } - - // if random.between(0, 100) == 4 { - // println("Disconnecting"); - // onyx_net.peer_send_disconnect(peer); - // onyx_net.peer_flush_outgoing_commands(peer); - // onyx_net.peer_disconnect(peer); - // break; - // } - } -} diff --git a/udp_server.onyx b/udp_server.onyx deleted file mode 100644 index 184127a..0000000 --- a/udp_server.onyx +++ /dev/null @@ -1,46 +0,0 @@ -#load "core/std" -#load "src/module" - -use package core -onyx_net :: package onyx_net - - -main :: (args) => { - addr: net.Socket_Address; - addr.port = 8080; - - server, err := onyx_net.host_create(^addr, 32); - if err != .None { - println(err); - return; - } - - while true { - for server->get_events(timeout = 2000) { - // printf("{*}\n", it); - - if it.type == .Connection { - printf("New connection from: {}\n", it.peer.addr); - packet := new(onyx_net.Packet); - packet.flags |= .Reliable; - packet.data = "Welcome!!!"; - - onyx_net.host_broadcast(server, 0, packet); - } - - if it.type == .Disconnection { - printf("Disconnection: {}\n", it.peer.addr); - } - - if it.type == .Message { - if it.data == "What's Up?" { - packet := new(onyx_net.Packet); - packet.flags |= .Reliable; - packet.data = #file_contents "src/host.onyx"; - - onyx_net.peer_send(it.peer, 0, packet); - } - } - } - } -}