Skip to content

Commit e29499d

Browse files
committed
Fixing Client and capitalizing header file names.
The switch from checking _sock against a default value, combined with the checking for unconnected clients in status(), broke the Client. I went back to checking the value of _sock against the default and removed the _connected member altogether because it was redundant (and therefore needed to be kept in sync with _sock).
1 parent ec03445 commit e29499d

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

Client.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,76 @@
11
#include "w5100.h"
22
#include "socket.h"
3+
34
extern "C" {
4-
#include "string.h"
5+
#include "string.h"
56
}
67

78
#include "WProgram.h"
89

9-
#include "ethernet.h"
10-
#include "client.h"
11-
#include "server.h"
10+
#include "Ethernet.h"
11+
#include "Client.h"
12+
#include "Server.h"
1213

1314
uint16_t Client::_srcport = 1024;
1415

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) {
1917
}
2018

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) {
2220
}
2321

2422
uint8_t Client::connect() {
25-
if (_connected)
23+
if (_sock != MAX_SOCK_NUM)
2624
return 0;
2725

28-
int i;
29-
for (i=0; i<MAX_SOCK_NUM; i++) {
26+
for (int i = 0; i < MAX_SOCK_NUM; i++) {
3027
uint8_t s = W5100.readSnSR(i);
3128
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
3229
_sock = i;
3330
break;
3431
}
3532
}
3633

37-
if (i == MAX_SOCK_NUM)
34+
if (_sock == MAX_SOCK_NUM)
3835
return 0;
3936

4037
_srcport++;
4138
if (_srcport == 0) _srcport = 1024;
4239
socket(_sock, SnMR::TCP, _srcport, 0);
4340

44-
if (!::connect(_sock, _ip, _port))
41+
if (!::connect(_sock, _ip, _port)) {
42+
_sock = MAX_SOCK_NUM;
4543
return 0;
44+
}
4645

4746
while (status() != SnSR::ESTABLISHED) {
4847
delay(1);
49-
if (status() == SnSR::CLOSED)
48+
if (status() == SnSR::CLOSED) {
49+
_sock = MAX_SOCK_NUM;
5050
return 0;
51+
}
5152
}
5253

53-
_connected = true;
5454
return 1;
5555
}
5656

5757
void Client::write(uint8_t b) {
58-
if (_connected)
58+
if (_sock != MAX_SOCK_NUM)
5959
send(_sock, &b, 1);
6060
}
6161

6262
void Client::write(const char *str) {
63-
if (_connected)
63+
if (_sock != MAX_SOCK_NUM)
6464
send(_sock, (const uint8_t *)str, strlen(str));
6565
}
6666

6767
void Client::write(const uint8_t *buf, size_t size) {
68-
if (_connected)
68+
if (_sock != MAX_SOCK_NUM)
6969
send(_sock, buf, size);
7070
}
7171

7272
int Client::available() {
73-
if (_connected)
73+
if (_sock != MAX_SOCK_NUM)
7474
return W5100.getRXReceivedSize(_sock);
7575
return 0;
7676
}
@@ -89,7 +89,7 @@ void Client::flush() {
8989
}
9090

9191
void Client::stop() {
92-
if (!_connected)
92+
if (_sock == MAX_SOCK_NUM)
9393
return;
9494

9595
// attempt to close the connection gracefully (send a FIN to other side)
@@ -105,19 +105,19 @@ void Client::stop() {
105105
close(_sock);
106106

107107
EthernetClass::_server_port[_sock] = 0;
108-
_connected = false;
108+
_sock = MAX_SOCK_NUM;
109109
}
110110

111111
uint8_t Client::connected() {
112-
if (!_connected) return 0;
112+
if (_sock == MAX_SOCK_NUM) return 0;
113113

114114
uint8_t s = status();
115115
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
116116
(s == SnSR::CLOSE_WAIT && !available()));
117117
}
118118

119119
uint8_t Client::status() {
120-
if (!_connected) return SnSR::CLOSED;
120+
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
121121
return W5100.readSnSR(_sock);
122122
}
123123

@@ -127,13 +127,13 @@ uint8_t Client::status() {
127127
// library.
128128

129129
uint8_t Client::operator==(int p) {
130-
return !_connected;
130+
return _sock == MAX_SOCK_NUM;
131131
}
132132

133133
uint8_t Client::operator!=(int p) {
134-
return _connected;
134+
return _sock != MAX_SOCK_NUM;
135135
}
136136

137137
Client::operator bool() {
138-
return _connected;
138+
return _sock != MAX_SOCK_NUM;
139139
}

Ethernet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "w5100.h"
2-
#include "ethernet.h"
2+
#include "Ethernet.h"
33

44
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
55
uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {

Ethernet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <inttypes.h>
55
//#include "w5100.h"
6-
#include "client.h"
7-
#include "server.h"
6+
#include "Client.h"
7+
#include "Server.h"
88

99
#define MAX_SOCK_NUM 4
1010

Server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ extern "C" {
44
#include "string.h"
55
}
66

7-
#include "ethernet.h"
8-
#include "client.h"
9-
#include "server.h"
7+
#include "Ethernet.h"
8+
#include "Client.h"
9+
#include "Server.h"
1010

1111
Server::Server(uint16_t port)
1212
{

0 commit comments

Comments
 (0)