Skip to content
This repository was archived by the owner on Nov 22, 2023. It is now read-only.

Commit 0436b71

Browse files
author
gek
committed
make push
1 parent c4a45ff commit 0436b71

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ strtest
2121
bigunit
2222
dirlist
2323
embedc
24+
sock_test*

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ string_stdlib_test:
8989
dirlist:
9090
cbas tests2/dirlist.cbas
9191
$(CC) $(CFLAGS_CBAS) auto_out.c -o dirlist -lpthread -lm
92+
93+
sock_test1:
94+
cbas tests2/sockets_test.cbas
95+
$(CC) $(CFLAGS_CBAS) auto_out.c -o sock_test1 -lpthread -lm
9296

9397
embedc:
9498
cbas embedc.cbas

library/toc/sockets.hbas

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class px_socket
2828
int sock_fd
2929
end
3030

31+
fn inline px_close(int fd):
32+
@inline_C "close($fd);"
33+
end
3134

3235
method inline px_socket.ctor():
3336
this.sock_fd = -1;
@@ -207,7 +210,7 @@ method inline px_socket.accept(char* addrout, uint maxlen, uint* addrlen, uint n
207210
"
208211
if(rfd < 0)
209212
//-2 = no connection, but no real error...
210-
if(errno == EAGAIN() || errno == EWOULDBLOCK())
213+
if(errno == EAGAIN || errno == EWOULDBLOCK)
211214
errno = 0;
212215
return -2;
213216
end
@@ -238,7 +241,7 @@ fn inline px_send(int sfd, byte* buf, uint nbytes)->TGT_IMAX:
238241
TGT_IMAX rval;
239242
errno = 0;
240243
@inline_C "$rval = send($sfd, $buf, $nbytes, 0);"
241-
if(rval < 0 && (errno == EAGAIN() || errno == EWOULDBLOCK()))
244+
if(rval < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
242245
return -2;
243246
end
244247
return rval
@@ -248,7 +251,7 @@ fn inline px_recv(int sfd, byte* buf, uint nbytes)->TGT_IMAX:
248251
TGT_IMAX rval;
249252
errno = 0;
250253
@inline_C "$rval = recv($sfd, $buf, $nbytes, 0);"
251-
if(rval < 0 && (errno == EAGAIN() || errno == EWOULDBLOCK()))
254+
if(rval < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
252255
return -2;
253256
end
254257
return rval

tests2/sockets_test.cbas

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
2+
#include <toc_native_user>
3+
#include <toc/sockets.hbas>
4+
5+
6+
7+
@wksht prnt [
8+
[
9+
@pprint [println mutoa ARG1]
10+
][
11+
ARG1
12+
]
13+
]
14+
15+
16+
fn client:
17+
px_socket a
18+
19+
int q = a.connectIPV4("127.0.0.1", 9999,0, 0);
20+
if(q == -1)
21+
perror("CLIENT CONNECT ERROR: ");
22+
sys_exit(1);
23+
end
24+
//let the server send us data....
25+
char[512] buf
26+
memclear(buf, 512);
27+
if(px_recv(a.sock_fd, buf, 512) == -1)
28+
perror("CLIENT RECV ERROR: ");
29+
sys_exit(1);
30+
end
31+
//print it to the screen...
32+
@prnt[
33+
/ "CLIENT RECEIVED:"
34+
/ (buf)
35+
]
36+
37+
end
38+
39+
fn server:
40+
errno = 0;
41+
px_socket a
42+
int q = a.bindIPV4(9999,0,0);
43+
if(q == -1)
44+
perror("SERVER ERROR: ");
45+
sys_exit(1);
46+
end
47+
//set to listen
48+
a.listen(5);
49+
if(errno)
50+
perror("SERVER LISTEN ERROR: ");
51+
sys_exit(1);
52+
end
53+
//accept a single client...
54+
q = a.accept((char*)0, 0, (uint*)0, 0);
55+
if(q == -1)
56+
perror("SERVER ACCEPT ERROR: ");
57+
sys_exit(1);
58+
end
59+
char[512] buf
60+
memclear(buf, 512);
61+
//
62+
srand(unixtime);
63+
64+
itoa(buf, rand);
65+
@prnt[
66+
/ "SERVER SENDING:"
67+
/ (buf)
68+
]
69+
px_send(q, buf, 512);
70+
//close it...
71+
px_close(q);
72+
73+
end
74+
75+
76+
fn pub main(int argc, schar** argv)->int:
77+
if(argc < 2)
78+
@prnt[
79+
/ "Is this the client or the server?"
80+
/ "say `client` or `server`"
81+
]
82+
sys_exit(1)
83+
end
84+
if(
85+
(char*)argv[1] streq "server" ||
86+
(char*)argv[1] streq "SERVER" ||
87+
(char*)argv[1] streq "Server" ||
88+
(char*)argv[1] streq "serv" ||
89+
(char*)argv[1] streq "SERV" ||
90+
(char*)argv[1] streq "Serv"
91+
)
92+
server();
93+
return 0;
94+
else
95+
client();
96+
return 0;
97+
end
98+
return 0;
99+
end
100+
101+
fn codegen codegen_main():
102+
cgstr pref
103+
pref.fromstr(SEABASS_STDLIB_PREFIX);
104+
pref.addstr(INTERNET_SOCKETS_PREFIX);
105+
cg_emitC(pref.s);
106+
pref.free();
107+
end
108+
109+

0 commit comments

Comments
 (0)