Mercurial > repos > tabletprog
comparison modules/socket.tp @ 145:7db37f040a6f
Basic socket support
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 09 Aug 2013 04:15:53 -0700 |
parents | 9bce890a7ff2 |
children | 4c96a393103e |
comparison
equal
deleted
inserted
replaced
144:547153211389 | 145:7db37f040a6f |
---|---|
1 #{ | 1 #{ |
2 includeSystemHeader: "sys/types.h" | |
3 includeSystemHeader: "sys/socket.h" | |
4 includeSystemHeader: "netdb.h" | |
5 | |
2 llMessage: AF_INET withVars: { | 6 llMessage: AF_INET withVars: { |
3 intret <- obj_int32 ptr | 7 intret <- obj_int32 ptr |
4 } andCode: { | 8 } andCode: { |
5 intret <- make_object: (addr_of: obj_int32_meta) NULL 0 | 9 intret <- make_object: (addr_of: obj_int32_meta) NULL 0 |
6 intret num!: AF_INET | 10 intret num!: AF_INET |
35 } andCode: :domain type protocol { | 39 } andCode: :domain type protocol { |
36 fd <- make_object: (addr_of: obj_int32_meta) NULL 0 | 40 fd <- make_object: (addr_of: obj_int32_meta) NULL 0 |
37 fd num!: (socket: (domain num) (type num) (protocol num)) | 41 fd num!: (socket: (domain num) (type num) (protocol num)) |
38 fd | 42 fd |
39 } | 43 } |
40 | 44 |
41 llMessage: getaddrinfo withVars: { | |
42 | |
43 } andCode: { | |
44 } | |
45 | |
46 new <- :domain type protocol { | 45 new <- :domain type protocol { |
47 sfd <- socket: domain type protocol | 46 sfd <- socket: domain type protocol |
48 #{ | 47 #{ |
49 fd <- {sfd} | 48 fd <- {sfd} |
50 llMessage: close withVars: { | 49 llMessage: close withVars: { |
52 } andCode: { | 51 } andCode: { |
53 sfd <- mcall: fd 1 self | 52 sfd <- mcall: fd 1 self |
54 close: (sfd num) | 53 close: (sfd num) |
55 self | 54 self |
56 } | 55 } |
56 llMessage: send:withFlags withVars: { | |
57 odata <- object ptr | |
58 flags <- obj_int32 ptr | |
59 sdata <- string ptr | |
60 sfd <- obj_int32 ptr | |
61 res <- obj_int32 ptr | |
62 } andCode: :odata :flags { | |
63 sdata <- mcall: string 1 odata | |
64 sfd <- mcall: fd 1 self | |
65 res <- make_object: (addr_of: obj_int32_meta) NULL 0 | |
66 res num!: (send: (sfd num) (sdata data) (sdata bytes) (flags num)) | |
67 res | |
68 } | |
69 send <- :data { | |
70 send: data withFlags: 0 | |
71 } | |
57 } | 72 } |
58 } | 73 } |
59 | 74 |
75 llMessage: _connectTo:onPort withVars: { | |
76 host <- string ptr | |
77 port <- string ptr | |
78 hints <- struct: addrinfo | |
79 info <- (struct: addrinfo) ptr | |
80 domain <- obj_int32 ptr | |
81 type <- obj_int32 ptr | |
82 protocol <- obj_int32 ptr | |
83 sock <- object ptr | |
84 sfd <- obj_int32 ptr | |
85 } andCode: :host :port { | |
86 memset: (addr_of: hints) 0 (sizeof: hints) | |
87 hints ai_family!: AF_UNSPEC | |
88 hints ai_socktype!: SOCK_STREAM | |
89 getaddrinfo: (host data) (port data) (addr_of: hints) (addr_of: info) | |
90 | |
91 domain <- make_object: (addr_of: obj_int32_meta) NULL 0 | |
92 domain num!: (info ai_family) | |
93 type <- make_object: (addr_of: obj_int32_meta) NULL 0 | |
94 type num!: (info ai_socktype) | |
95 protocol <- make_object: (addr_of: obj_int32_meta) NULL 0 | |
96 protocol num!: (info ai_protocol) | |
97 | |
98 sock <- mcall: new 4 self domain type protocol | |
99 sfd <- mcall: fd 1 sock | |
100 | |
101 connect: (sfd num) (info ai_addr) (info ai_addrlen) | |
102 | |
103 freeaddrinfo: info | |
104 sock | |
105 } | |
106 | |
60 connectTo:onPort <- :host :port { | 107 connectTo:onPort <- :host :port { |
61 | 108 _connectTo: host onPort: (string: port) |
62 } | 109 } |
63 listenOn: <- :port { | 110 listenOn <- :port { |
64 } | 111 } |
65 listenAt:onPort <- :host :port { | 112 listenAt:onPort <- :host :port { |
66 } | 113 } |
67 } | 114 } |