1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-12-03 05:41:13 +03:00

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
This commit is contained in:
David A. Mellis
2011-08-23 19:12:03 -04:00
parent b788ad593f
commit 8059abe581
24 changed files with 290 additions and 174 deletions

View File

@@ -70,19 +70,18 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1;
}
void Client::write(uint8_t b) {
if (_sock != MAX_SOCK_NUM)
send(_sock, &b, 1);
long Client::write(uint8_t b) {
return write(&b, 1);
}
void Client::write(const char *str) {
if (_sock != MAX_SOCK_NUM)
send(_sock, (const uint8_t *)str, strlen(str));
long Client::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
void Client::write(const uint8_t *buf, size_t size) {
if (_sock != MAX_SOCK_NUM)
send(_sock, buf, size);
long Client::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) return -1;
if (!send(_sock, buf, size)) return -2;
return size;
}
int Client::available() {

View File

@@ -12,9 +12,9 @@ public:
uint8_t status();
int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port);
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual long write(uint8_t);
virtual long write(const char *str);
virtual long write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);

View File

@@ -42,13 +42,15 @@ bool IPAddress::operator==(const uint8_t* addr)
return memcmp(addr, _address, sizeof(_address)) == 0;
}
void IPAddress::printTo(Print& p) const
long IPAddress::printTo(Print& p) const
{
long n = 0, t;
for (int i =0; i < 3; i++)
{
p.print(_address[i], DEC);
p.print('.');
if ((t = p.print(_address[i], DEC)) > 0) n += t;
if ((t = p.print('.')) > 0) n+= t;
}
p.print(_address[3], DEC);
if ((t = p.print(_address[3], DEC)) > 0) n += t;
return n;
}

View File

@@ -60,7 +60,7 @@ public:
IPAddress& operator=(const uint8_t *address);
IPAddress& operator=(uint32_t address);
virtual void printTo(Print& p) const;
virtual long printTo(Print& p) const;
friend class EthernetClass;
friend class UDP;

View File

@@ -67,18 +67,20 @@ Client Server::available()
return Client(MAX_SOCK_NUM);
}
void Server::write(uint8_t b)
long Server::write(uint8_t b)
{
write(&b, 1);
}
void Server::write(const char *str)
long Server::write(const char *str)
{
write((const uint8_t *)str, strlen(str));
}
void Server::write(const uint8_t *buffer, size_t size)
long Server::write(const uint8_t *buffer, size_t size)
{
long n = 0;
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
@@ -86,7 +88,9 @@ void Server::write(const uint8_t *buffer, size_t size)
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {
client.write(buffer, size);
n += client.write(buffer, size);
}
}
return n;
}

View File

@@ -14,9 +14,9 @@ public:
Server(uint16_t);
Client available();
void begin();
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual long write(uint8_t);
virtual long write(const char *str);
virtual long write(const uint8_t *buf, size_t size);
};
#endif

View File

@@ -102,21 +102,22 @@ int UDP::endPacket()
return sendUDP(_sock);
}
void UDP::write(uint8_t byte)
long UDP::write(uint8_t byte)
{
write(&byte, 1);
return write(&byte, 1);
}
void UDP::write(const char *str)
long UDP::write(const char *str)
{
size_t len = strlen(str);
write((const uint8_t *)str, len);
return write((const uint8_t *)str, len);
}
void UDP::write(const uint8_t *buffer, size_t size)
long UDP::write(const uint8_t *buffer, size_t size)
{
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
_offset += bytes_written;
return bytes_written;
}
int UDP::parsePacket()

View File

@@ -67,11 +67,11 @@ public:
// Returns 1 if the packet was sent successfully, 0 if there was an error
int endPacket();
// Write a single byte into the packet
virtual void write(uint8_t);
virtual long write(uint8_t);
// Write a string of characters into the packet
virtual void write(const char *str);
virtual long write(const char *str);
// Write size bytes from buffer into the packet
virtual void write(const uint8_t *buffer, size_t size);
virtual long write(const uint8_t *buffer, size_t size);
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available