Skip to content

Commit a846508

Browse files
authored
Merge pull request #185 from Joe7M/master
Fix bug in BGETC when using sockets
2 parents 03da10e + 378ea6a commit a846508

File tree

7 files changed

+72
-9
lines changed

7 files changed

+72
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Client connection closed
2+
Server connection closed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!../../../src/platform/console/sbasic
2+
3+
' This file (socket_io_client.bas) will be called by socket_io.bas
4+
' Shebang needs to link to correct sbasic file.
5+
6+
open "SOCL:127.0.0.1:10000" as #1
7+
8+
' Test bputc and bgetc
9+
for byte = 0 to 255
10+
bputc #1, byte
11+
next
12+
13+
' Test print
14+
print #1, "Test1234"
15+
print #1, "Test-1234"
16+
print #1, "Test1234"
17+
18+
' Test EOF bug
19+
byte = 0
20+
bputc #1, byte
21+
22+
' Test LOF
23+
print #1, "Test1234"
24+
25+
close #1
26+
27+
print "Client connection closed"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
' socket-io.bas will start a socket client. The client will send data.
2+
' socket-io.bas will check if data was received correctly
3+
4+
' make socket-io-client.bas executable and then execute it
5+
chmod "../../../samples/distro-examples/tests/socket-io-client.bas", 0o777
6+
exec "../../../samples/distro-examples/tests/socket-io-client.bas"
7+
8+
open "SOCL:10000" as #1
9+
10+
' Test bputc and bgetc
11+
for ii = 0 to 255
12+
Recv = bgetc(1)
13+
if(Recv != ii) then throw "BGETC: " + ii + " expected, received " + Recv
14+
next
15+
16+
' Test print and input
17+
input #1, ans
18+
if(ans != "Test1234") then throw "INPUT: Test1234 expected, received " + ans
19+
20+
input #1, ans, "-", ans2
21+
if(ans != "Test") then throw "INPUT: Test expected, received " + ans
22+
if(ans2 != "1234") then throw "INPUT: 1234 expected, received " + ans2
23+
24+
ans = input(9, 1)
25+
if(ans != "Test1234\n") then throw "INPUT: Test1234 expected, received " + "\"" + ans + "\""
26+
27+
' Test for bug in SB 12.25: when "0" is received, EOF should not return true
28+
ans = bgetc(1)
29+
if(EOF(1)) then throw "EOF: zero received and eof returns true"
30+
31+
' Test LOF
32+
if(LOF(1) != 9) then throw "LOF: 9 bytes expected. " + LOF(1) + " bytes waiting."
33+
34+
close #1
35+
36+
print "Server connection closed"

src/common/fs_socket_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ int sockcl_eof(dev_file_t *f) {
245245
}
246246

247247
//
248-
// returns the size of the data which are waiting in stream's queue
248+
// returns the size of data waiting in stream's queue
249249
//
250250
int sockcl_length(dev_file_t *f) {
251251
return net_peek((socket_t) (long) f->handle);

src/common/inet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void net_disconnect(socket_t s);
134134
/**
135135
* @ingroup net
136136
*
137-
* returns true if something is waiting in input-buffer
137+
* returns number of bytes waiting in input-buffer
138138
*
139139
* @param s the socket
140140
* @return non-zero if something is waiting in input-buffer; otherwise returns 0

src/common/inet2.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ int net_input(socket_t s, char *buf, int size, const char *delim) {
161161
if (bytes <= 0) {
162162
return count; // no more data
163163
} else {
164-
if (ch == 0) {
165-
return count;
166-
}
167164
if (delim) {
168165
if ((strchr(delim, ch) != NULL)) {
169166
return count; // delimiter found
@@ -178,19 +175,19 @@ int net_input(socket_t s, char *buf, int size, const char *delim) {
178175
}
179176

180177
/**
181-
* return true if there something waiting
178+
* return available data in bytes
182179
*/
183180
int net_peek(socket_t s) {
184181
#if defined(_Win32)
185182
unsigned long bytes;
186183

187184
ioctlsocket(s, FIONREAD, &bytes);
188-
return (bytes > 0);
185+
return (bytes);
189186
#else
190187
int bytes;
191188

192189
ioctl(s, FIONREAD, &bytes);
193-
return (bytes > 0);
190+
return (bytes);
194191
#endif
195192
}
196193

src/platform/console/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ TEST_DIR=../../../samples/distro-examples/tests
3434
UNIT_TESTS=array break byref eval-test iifs matrices metaa ongoto \
3535
uds hash pass1 call_tau short-circuit strings stack-test \
3636
replace-test read-data proc optchk letbug ptr ref input \
37-
trycatch chain stream-files split-join sprint all scope goto keymap
37+
trycatch chain stream-files split-join sprint all scope \
38+
goto keymap socket-io
3839

3940
test: ${bin_PROGRAMS}
4041
@for utest in $(UNIT_TESTS); do \

0 commit comments

Comments
 (0)