Skip to content

Commit 3316ede

Browse files
committed
Moving write errors out of return value into separate API methods.
write(), print(), println() now return size_t (and don't use negative values to signal errors). Print adds writeError() for checking for write errors, clearWriteError() to reset the flag to false, and a protected setWriteError() for signalling errors. http://code.google.com/p/arduino/issues/detail?id=598
1 parent 3e3e3ed commit 3316ede

File tree

8 files changed

+33
-27
lines changed

8 files changed

+33
-27
lines changed

Client.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,23 @@ int Client::connect(IPAddress ip, uint16_t port) {
7070
return 1;
7171
}
7272

73-
ssize_t Client::write(uint8_t b) {
73+
size_t Client::write(uint8_t b) {
7474
return write(&b, 1);
7575
}
7676

77-
ssize_t Client::write(const char *str) {
77+
size_t Client::write(const char *str) {
7878
return write((const uint8_t *) str, strlen(str));
7979
}
8080

81-
ssize_t Client::write(const uint8_t *buf, size_t size) {
82-
if (_sock == MAX_SOCK_NUM) return -1;
83-
if (!send(_sock, buf, size)) return -2;
81+
size_t Client::write(const uint8_t *buf, size_t size) {
82+
if (_sock == MAX_SOCK_NUM) {
83+
setWriteError();
84+
return 0;
85+
}
86+
if (!send(_sock, buf, size)) {
87+
setWriteError();
88+
return 0;
89+
}
8490
return size;
8591
}
8692

Client.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Client : public Stream {
1212
uint8_t status();
1313
int connect(IPAddress ip, uint16_t port);
1414
int connect(const char *host, uint16_t port);
15-
virtual ssize_t write(uint8_t);
16-
virtual ssize_t write(const char *str);
17-
virtual ssize_t write(const uint8_t *buf, size_t size);
15+
virtual size_t write(uint8_t);
16+
virtual size_t write(const char *str);
17+
virtual size_t write(const uint8_t *buf, size_t size);
1818
virtual int available();
1919
virtual int read();
2020
virtual int read(uint8_t *buf, size_t size);

IPAddress.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ bool IPAddress::operator==(const uint8_t* addr)
4242
return memcmp(addr, _address, sizeof(_address)) == 0;
4343
}
4444

45-
ssize_t IPAddress::printTo(Print& p) const
45+
size_t IPAddress::printTo(Print& p) const
4646
{
47-
ssize_t n = 0, t;
47+
size_t n = 0;
4848
for (int i =0; i < 3; i++)
4949
{
50-
if ((t = p.print(_address[i], DEC)) > 0) n += t;
51-
if ((t = p.print('.')) > 0) n+= t;
50+
n += p.print(_address[i], DEC);
51+
n += p.print('.');
5252
}
53-
if ((t = p.print(_address[3], DEC)) > 0) n += t;
53+
n += p.print(_address[3], DEC);
5454
return n;
5555
}
5656

IPAddress.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class IPAddress : public Printable {
6060
IPAddress& operator=(const uint8_t *address);
6161
IPAddress& operator=(uint32_t address);
6262

63-
virtual ssize_t printTo(Print& p) const;
63+
virtual size_t printTo(Print& p) const;
6464

6565
friend class EthernetClass;
6666
friend class UDP;

Server.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ Client Server::available()
6767
return Client(MAX_SOCK_NUM);
6868
}
6969

70-
ssize_t Server::write(uint8_t b)
70+
size_t Server::write(uint8_t b)
7171
{
7272
write(&b, 1);
7373
}
7474

75-
ssize_t Server::write(const char *str)
75+
size_t Server::write(const char *str)
7676
{
7777
write((const uint8_t *)str, strlen(str));
7878
}
7979

80-
ssize_t Server::write(const uint8_t *buffer, size_t size)
80+
size_t Server::write(const uint8_t *buffer, size_t size)
8181
{
82-
ssize_t n = 0;
82+
size_t n = 0;
8383

8484
accept();
8585

Server.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public Print {
1414
Server(uint16_t);
1515
Client available();
1616
void begin();
17-
virtual ssize_t write(uint8_t);
18-
virtual ssize_t write(const char *str);
19-
virtual ssize_t write(const uint8_t *buf, size_t size);
17+
virtual size_t write(uint8_t);
18+
virtual size_t write(const char *str);
19+
virtual size_t write(const uint8_t *buf, size_t size);
2020
};
2121

2222
#endif

Udp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,18 @@ int UDP::endPacket()
102102
return sendUDP(_sock);
103103
}
104104

105-
ssize_t UDP::write(uint8_t byte)
105+
size_t UDP::write(uint8_t byte)
106106
{
107107
return write(&byte, 1);
108108
}
109109

110-
ssize_t UDP::write(const char *str)
110+
size_t UDP::write(const char *str)
111111
{
112112
size_t len = strlen(str);
113113
return write((const uint8_t *)str, len);
114114
}
115115

116-
ssize_t UDP::write(const uint8_t *buffer, size_t size)
116+
size_t UDP::write(const uint8_t *buffer, size_t size)
117117
{
118118
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
119119
_offset += bytes_written;

Udp.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ class UDP : public Stream {
6767
// Returns 1 if the packet was sent successfully, 0 if there was an error
6868
int endPacket();
6969
// Write a single byte into the packet
70-
virtual ssize_t write(uint8_t);
70+
virtual size_t write(uint8_t);
7171
// Write a string of characters into the packet
72-
virtual ssize_t write(const char *str);
72+
virtual size_t write(const char *str);
7373
// Write size bytes from buffer into the packet
74-
virtual ssize_t write(const uint8_t *buffer, size_t size);
74+
virtual size_t write(const uint8_t *buffer, size_t size);
7575

7676
// Start processing the next available incoming packet
7777
// Returns the size of the packet in bytes, or 0 if no packets are available

0 commit comments

Comments
 (0)