1
1
#include " w5100.h"
2
2
#include " socket.h"
3
+
3
4
extern " C" {
4
- #include " string.h"
5
+ #include " string.h"
5
6
}
6
7
7
8
#include " WProgram.h"
8
9
9
- #include " ethernet .h"
10
- #include " client .h"
11
- #include " server .h"
10
+ #include " Ethernet .h"
11
+ #include " Client .h"
12
+ #include " Server .h"
12
13
13
14
uint16_t Client::_srcport = 1024 ;
14
15
15
- Client::Client () : _connected(false ) {
16
- }
17
-
18
- Client::Client (uint8_t sock) : _connected(true ), _sock(sock) {
16
+ Client::Client (uint8_t sock) : _sock(sock) {
19
17
}
20
18
21
- Client::Client (uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _connected( false ) {
19
+ Client::Client (uint8_t *ip, uint16_t port) : _ip(ip), _port(port), _sock(MAX_SOCK_NUM ) {
22
20
}
23
21
24
22
uint8_t Client::connect () {
25
- if (_connected )
23
+ if (_sock != MAX_SOCK_NUM )
26
24
return 0 ;
27
25
28
- int i;
29
- for (i=0 ; i<MAX_SOCK_NUM; i++) {
26
+ for (int i = 0 ; i < MAX_SOCK_NUM; i++) {
30
27
uint8_t s = W5100.readSnSR (i);
31
28
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
32
29
_sock = i;
33
30
break ;
34
31
}
35
32
}
36
33
37
- if (i == MAX_SOCK_NUM)
34
+ if (_sock == MAX_SOCK_NUM)
38
35
return 0 ;
39
36
40
37
_srcport++;
41
38
if (_srcport == 0 ) _srcport = 1024 ;
42
39
socket (_sock, SnMR::TCP, _srcport, 0 );
43
40
44
- if (!::connect (_sock, _ip, _port))
41
+ if (!::connect (_sock, _ip, _port)) {
42
+ _sock = MAX_SOCK_NUM;
45
43
return 0 ;
44
+ }
46
45
47
46
while (status () != SnSR::ESTABLISHED) {
48
47
delay (1 );
49
- if (status () == SnSR::CLOSED)
48
+ if (status () == SnSR::CLOSED) {
49
+ _sock = MAX_SOCK_NUM;
50
50
return 0 ;
51
+ }
51
52
}
52
53
53
- _connected = true ;
54
54
return 1 ;
55
55
}
56
56
57
57
void Client::write (uint8_t b) {
58
- if (_connected )
58
+ if (_sock != MAX_SOCK_NUM )
59
59
send (_sock, &b, 1 );
60
60
}
61
61
62
62
void Client::write (const char *str) {
63
- if (_connected )
63
+ if (_sock != MAX_SOCK_NUM )
64
64
send (_sock, (const uint8_t *)str, strlen (str));
65
65
}
66
66
67
67
void Client::write (const uint8_t *buf, size_t size) {
68
- if (_connected )
68
+ if (_sock != MAX_SOCK_NUM )
69
69
send (_sock, buf, size);
70
70
}
71
71
72
72
int Client::available () {
73
- if (_connected )
73
+ if (_sock != MAX_SOCK_NUM )
74
74
return W5100.getRXReceivedSize (_sock);
75
75
return 0 ;
76
76
}
@@ -89,7 +89,7 @@ void Client::flush() {
89
89
}
90
90
91
91
void Client::stop () {
92
- if (!_connected )
92
+ if (_sock == MAX_SOCK_NUM )
93
93
return ;
94
94
95
95
// attempt to close the connection gracefully (send a FIN to other side)
@@ -105,19 +105,19 @@ void Client::stop() {
105
105
close (_sock);
106
106
107
107
EthernetClass::_server_port[_sock] = 0 ;
108
- _connected = false ;
108
+ _sock = MAX_SOCK_NUM ;
109
109
}
110
110
111
111
uint8_t Client::connected () {
112
- if (!_connected ) return 0 ;
112
+ if (_sock == MAX_SOCK_NUM ) return 0 ;
113
113
114
114
uint8_t s = status ();
115
115
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
116
116
(s == SnSR::CLOSE_WAIT && !available ()));
117
117
}
118
118
119
119
uint8_t Client::status () {
120
- if (!_connected ) return SnSR::CLOSED;
120
+ if (_sock == MAX_SOCK_NUM ) return SnSR::CLOSED;
121
121
return W5100.readSnSR (_sock);
122
122
}
123
123
@@ -127,13 +127,13 @@ uint8_t Client::status() {
127
127
// library.
128
128
129
129
uint8_t Client::operator ==(int p) {
130
- return !_connected ;
130
+ return _sock == MAX_SOCK_NUM ;
131
131
}
132
132
133
133
uint8_t Client::operator !=(int p) {
134
- return _connected ;
134
+ return _sock != MAX_SOCK_NUM ;
135
135
}
136
136
137
137
Client::operator bool () {
138
- return _connected ;
138
+ return _sock != MAX_SOCK_NUM ;
139
139
}
0 commit comments