Skip to content

Commit 8d8dcdb

Browse files
committed
write(), print(), and println() now return number of bytes written.
The type is long, and negative values indicate errors. Needs more testing. http://code.google.com/p/arduino/issues/detail?id=551
1 parent 992131d commit 8d8dcdb

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

Client.cpp

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

73-
void Client::write(uint8_t b) {
74-
if (_sock != MAX_SOCK_NUM)
75-
send(_sock, &b, 1);
73+
long Client::write(uint8_t b) {
74+
return write(&b, 1);
7675
}
7776

78-
void Client::write(const char *str) {
79-
if (_sock != MAX_SOCK_NUM)
80-
send(_sock, (const uint8_t *)str, strlen(str));
77+
long Client::write(const char *str) {
78+
return write((const uint8_t *) str, strlen(str));
8179
}
8280

83-
void Client::write(const uint8_t *buf, size_t size) {
84-
if (_sock != MAX_SOCK_NUM)
85-
send(_sock, buf, size);
81+
long 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;
84+
return size;
8685
}
8786

8887
int Client::available() {

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 void write(uint8_t);
16-
virtual void write(const char *str);
17-
virtual void write(const uint8_t *buf, size_t size);
15+
virtual long write(uint8_t);
16+
virtual long write(const char *str);
17+
virtual long 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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ bool IPAddress::operator==(const uint8_t* addr)
4242
return memcmp(addr, _address, sizeof(_address)) == 0;
4343
}
4444

45-
void IPAddress::printTo(Print& p) const
45+
long IPAddress::printTo(Print& p) const
4646
{
47+
long n = 0, t;
4748
for (int i =0; i < 3; i++)
4849
{
49-
p.print(_address[i], DEC);
50-
p.print('.');
50+
if ((t = p.print(_address[i], DEC)) > 0) n += t;
51+
if ((t = p.print('.')) > 0) n+= t;
5152
}
52-
p.print(_address[3], DEC);
53+
if ((t = p.print(_address[3], DEC)) > 0) n += t;
54+
return n;
5355
}
5456

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 void printTo(Print& p) const;
63+
virtual long printTo(Print& p) const;
6464

6565
friend class EthernetClass;
6666
friend class UDP;

Server.cpp

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

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

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

80-
void Server::write(const uint8_t *buffer, size_t size)
80+
long Server::write(const uint8_t *buffer, size_t size)
8181
{
82+
long n = 0;
83+
8284
accept();
8385

8486
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
8587
Client client(sock);
8688

8789
if (EthernetClass::_server_port[sock] == _port &&
8890
client.status() == SnSR::ESTABLISHED) {
89-
client.write(buffer, size);
91+
n += client.write(buffer, size);
9092
}
9193
}
94+
95+
return n;
9296
}

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 void write(uint8_t);
18-
virtual void write(const char *str);
19-
virtual void write(const uint8_t *buf, size_t size);
17+
virtual long write(uint8_t);
18+
virtual long write(const char *str);
19+
virtual long write(const uint8_t *buf, size_t size);
2020
};
2121

2222
#endif

Udp.cpp

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

105-
void UDP::write(uint8_t byte)
105+
long UDP::write(uint8_t byte)
106106
{
107-
write(&byte, 1);
107+
return write(&byte, 1);
108108
}
109109

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

116-
void UDP::write(const uint8_t *buffer, size_t size)
116+
long UDP::write(const uint8_t *buffer, size_t size)
117117
{
118118
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
119119
_offset += bytes_written;
120+
return bytes_written;
120121
}
121122

122123
int UDP::parsePacket()

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 void write(uint8_t);
70+
virtual long write(uint8_t);
7171
// Write a string of characters into the packet
72-
virtual void write(const char *str);
72+
virtual long write(const char *str);
7373
// Write size bytes from buffer into the packet
74-
virtual void write(const uint8_t *buffer, size_t size);
74+
virtual long 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)