aboutsummaryrefslogtreecommitdiff
path: root/elymas/lib/net/tcp.ey
blob: e7ef31e7765827d37201928e145cfc2620946bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Provide nice ways to do TCP/IP networking

<
  bin .scan  "->" via
  bin .print "<-" via
  bin .produce "<=" via
  sys .linux "+" via

  # 0 -> port
  # 1 -> host (as network byte ordered string)
  # 0 <- string representing the sockaddr_in 
  { ==port ==host
    +AFINET <=u16
    port <-un16
    host cat
    0 <-un64
  } /sockaddrIpv4 deffd

  # 0 -> "host:port"
  # 0 <- connected socket fd
  { "^([^:]+):(\\d+)$" regex not { "host:port expected" die } rep
    net .dns .resolveIpv4
    -01 txt .consume .u
    sockaddrIpv4
    +AFINET +SOCKSTREAM +IPPROTOTCP +socket _ ==s # TODO: error handling
    -01 +connect -- # TODO: error handling
    s
  } /connectFd deffd

  # 0 -> "host:port"
  # 0 <- connected socket as file
  {
    connectFd sys .fdToFile
  } /connect deffd

  # 0 -> port
  # 0 <- listening socket fd
  { # ==port
    0 <=un32 -01 sockaddrIpv4
    +AFINET +SOCKSTREAM +IPPROTOTCP +socket _ ==s # TODO: error handling
    -01 +bind -- # TODO: error handling
    s 256 +listen -- # TODO: error handling
    s
  } _ /listenFd deffd
      /listen   deffd

  # 0 -> listening socket fd
  # 0 <- newly accepted connection fd
  { # ==fd
    +accept # TODO: error handling
  } /acceptFd deffd

  # 0 -> listening socket fd
  # 0 <- newly accepted connection as file
  { # ==fd
    acceptFd sys .fdToFile
  } /accept deffd
> /tcp net .defv

# vim: syn=elymas