cleaning up before first push
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Apr 2022 15:18:19 +0000 (10:18 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 4 Apr 2022 15:18:19 +0000 (10:18 -0500)
README.md [new file with mode: 0644]
doc.md [deleted file]
example/udp_client.onyx [new file with mode: 0644]
example/udp_server.onyx [new file with mode: 0644]
src/host.onyx
src/peer.onyx
udp_client.onyx [deleted file]
udp_server.onyx [deleted file]

diff --git a/README.md b/README.md
new file mode 100644 (file)
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 (file)
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 (file)
index 0000000..507545a
--- /dev/null
@@ -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 (file)
index 0000000..184127a
--- /dev/null
@@ -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);
+                }
+            }
+        }
+    }
+}
index 3ab8264eb4df0dc38e13a55b104a676037873f33..978a0b83aff9d274497c7e0d28a39af7ab2276b8 100644 (file)
@@ -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 };
index fb3535a4d3c8e4ff398be9a799e2ace5824e2d20..e40d929c49721e9cadbea14fb241e4fde3c788f9 100644 (file)
@@ -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 (file)
index 507545a..0000000
+++ /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 (file)
index 184127a..0000000
+++ /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);
-                }
-            }
-        }
-    }
-}