Skip to content

Commit 0a120a9

Browse files
committed
Rename the library to netty.
1 parent 4568b58 commit 0a120a9

12 files changed

+802
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Netty is a reliable connection over UDP aimed at games. Normally UDP packets can get duplicated, dropped, or come out of order. Netty makes sure packets are not duplicated, re-sends them if they get dropped, and all packets come in order. UDP packets might also get split if they are above 512 bytes and also can fail to be sent if they are bigger than 1-2k. Netty breaks up big packets and sends them in pieces making sure each piece comes reliably in order. Finally sometimes it's impossible for two clients to communicate direclty with TCP because of NATs, but Netty provides hole punching which allows them to connect.
44

5-
## Is Netty a reimplementation of TCP?
5+
## Is Netty a implementation of TCP?
66

77
TCP is really bad for short latency sensitive messages. TCP was designed for throughput (downloading files) not latency (games). Netty will resend stuff faster than TCP, Netty will not buffer and you also get nat punch-through (which TCP does not have). Netty is basically "like TCP but for games". You should not be using Netty if you are will be sending large mount of data. By default Netty is capped at 250K of data in flight.
88

@@ -16,7 +16,7 @@ TCP is really bad for short latency sensitive messages. TCP was designed for thr
1616
| packet ordering | yes | no | yes |
1717
| packet splitting | yes | no | yes |
1818
| packet retry | yes | no | yes |
19-
| packet deduplication | yes | no | yes |
19+
| packet reduplication | yes | no | yes |
2020
| hole punch through | no | yes | yes |
2121
| connection handling | yes | no | yes |
2222
| congestion control | yes | no | yes |

examples/chatclient.nim

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import netty
2+
3+
var client = newReactor()
4+
var connection = client.connect("127.0.0.1", 2001)
5+
6+
# get persons name
7+
echo "what is your name?"
8+
var name = readLine(stdin)
9+
echo "note: press enter to see if people sent you things"
10+
11+
while true:
12+
client.tick()
13+
for packet in client.packets:
14+
echo packet.data
15+
16+
# wait for user to type a line
17+
let line = readLine(stdin)
18+
if line.len > 0:
19+
connection.send(name & ":" & line)

examples/chatclientthreads.nim

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# nim c -r --threads:on --tlsEmulation:off tests\chatclientthreads
2+
3+
import netty, terminal
4+
5+
var client = newReactor()
6+
var connection = client.connect("127.0.0.1", 2001)
7+
8+
# get persons name
9+
echo "what is your name?"
10+
var name = readLine(stdin)
11+
12+
# handle ctrl-c correclty on windows by stopping all threads
13+
proc handleCtrlC() {.noconv.} =
14+
setupForeignThreadGc()
15+
quit(1)
16+
setControlCHook(handleCtrlC)
17+
18+
# create a thread that just reads a single chart
19+
var
20+
thread: Thread[tuple[a: int]]
21+
singleChar: char
22+
proc readSingleChar(interval: tuple[a: int]) {.thread.} =
23+
while true:
24+
singleChar = getch()
25+
createThread(thread, readSingleChar, (0, ))
26+
27+
# main loop
28+
var line: string
29+
while true:
30+
client.tick()
31+
for packet in client.packets:
32+
# we got a packet, rase current line user is typing
33+
stdout.eraseLine()
34+
# write packet line
35+
echo packet.data
36+
# write back the line was typing
37+
writeStyled(line)
38+
39+
if singleChar != char(0):
40+
if singleChar == char(8):
41+
# handle backspace
42+
line.setLen(line.len - 1)
43+
else:
44+
line.add(singleChar)
45+
# a char got added, erase current line
46+
stdout.eraseLine()
47+
# write the line again
48+
writeStyled(line & " ")
49+
# put cursor in right spot
50+
stdout.setCursorXPos(line.len)
51+
52+
if singleChar == char(13):
53+
# handle sending
54+
connection.send(name & ":" & line)
55+
# clear line, reset eveything
56+
# server should echo the line back
57+
line = ""
58+
stdout.eraseLine()
59+
stdout.setCursorXPos(0)
60+
61+
# reset character
62+
singleChar = char(0)

examples/chatclientthreads.nims

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--threads:on
2+
--tlsEmulation:off

examples/chatserver.nim

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import netty
2+
3+
var server = newReactor("127.0.0.1", 2001)
4+
echo "Listenting for UDP on 127.0.0.1:2001"
5+
while true:
6+
server.tick()
7+
for connection in server.newConnections:
8+
echo "[new] ", connection.address
9+
for connection in server.deadConnections:
10+
echo "[dead] ", connection.address
11+
for packet in server.packets:
12+
echo "[msg]", packet.data
13+
# send packet data to all connections
14+
for connection in server.connections:
15+
connection.send(packet.data)

examples/client.nim

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import netty
2+
3+
# create connection
4+
var client = newReactor()
5+
# connect to server
6+
var c2s = client.connect("127.0.0.1", 1999)
7+
# send message on the connection
8+
c2s.send("hi")
9+
# main loop
10+
while true:
11+
# must call tick to both read and write
12+
client.tick()
13+
# usually there are no new packets, but if there are
14+
for packet in client.packets:
15+
# print packet data
16+
echo "GOT PACKET: ", packet.data

examples/server.nim

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import netty
2+
3+
# listen for a connection on localhost port 1999
4+
var server = newReactor("127.0.0.1", 1999)
5+
echo "Listenting for UDP on 127.0.0.1:1999"
6+
# main loop
7+
while true:
8+
# must call tick to both read and write
9+
server.tick()
10+
# usually there are no new packets, but if there are
11+
for packet in server.packets:
12+
# print packet data
13+
echo "GOT PACKET: ", packet.data
14+
# echo packet back to the client
15+
packet.connection.send("you said:" & packet.data)

netty.nimble

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Package
2+
3+
version = "0.1.0"
4+
author = "treeform"
5+
description = "Netty is a reliable UDP connection for games in Nim."
6+
license = "MIT"
7+
srcDir = "src"
8+
9+
# Dependencies
10+
11+
requires "nim >= 0.19.0"

0 commit comments

Comments
 (0)