Skip to content

Commit ec7f966

Browse files
committed
Replace fnTcpClientRxBuffer with std::string (FujiNetWIFI#1025)
1 parent 70589df commit ec7f966

File tree

2 files changed

+1
-210
lines changed

2 files changed

+1
-210
lines changed

lib/tcpip/fnTcpClient.cpp

Lines changed: 0 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -37,164 +37,6 @@
3737
#define FNTCP_SELECT_TIMEOUT_US (1000000)
3838
#define FNTCP_FLUSH_BUFFER_SIZE (1024)
3939

40-
#if 0
41-
class fnTcpClientRxBuffer
42-
{
43-
private:
44-
size_t _size;
45-
uint8_t *_buffer;
46-
size_t _pos;
47-
size_t _fill;
48-
int _fd;
49-
bool _failed;
50-
51-
size_t r_available()
52-
{
53-
if (_fd < 0)
54-
{
55-
return 0;
56-
}
57-
#if defined(_WIN32)
58-
unsigned long count;
59-
int res = ioctlsocket(_fd, FIONREAD, &count);
60-
if (res != 0)
61-
{
62-
_failed = true;
63-
return 0;
64-
}
65-
#else
66-
int count;
67-
#ifdef ESP_PLATFORM
68-
int res = lwip_ioctl(_fd, FIONREAD, &count);
69-
#else
70-
int res = ioctl(_fd, FIONREAD, &count);
71-
#endif
72-
if (res < 0)
73-
{
74-
_failed = true;
75-
return 0;
76-
}
77-
#endif
78-
return count;
79-
}
80-
81-
// Fill our buffer with data from the given socket descriptor
82-
// Returns how much data was read
83-
size_t fillBuffer()
84-
{
85-
// Allocate space for the buffer if we haven't already
86-
if (!_buffer)
87-
{
88-
_buffer = (uint8_t *)malloc(_size);
89-
if (!_buffer)
90-
{
91-
Debug_printf("Not enough memory to allocate buffer\r\n");
92-
_failed = true;
93-
return 0;
94-
}
95-
}
96-
97-
if (_fill && _pos == _fill)
98-
{
99-
_fill = 0;
100-
_pos = 0;
101-
}
102-
103-
if (!_buffer || _size <= _fill || !r_available())
104-
{
105-
return 0;
106-
}
107-
108-
// Read the data
109-
int res = recv(_fd, (char *)(_buffer + _fill), _size - _fill, MSG_DONTWAIT);
110-
if (res < 0)
111-
{
112-
int err = compat_getsockerr();
113-
#if defined(_WIN32)
114-
if (err != WSAEWOULDBLOCK)
115-
#else
116-
if (err != EWOULDBLOCK)
117-
#endif
118-
{
119-
_failed = true;
120-
}
121-
return 0;
122-
}
123-
_fill += res;
124-
return res;
125-
}
126-
127-
public:
128-
fnTcpClientRxBuffer(int fd, size_t size = 1436)
129-
: _size(size), _buffer(NULL), _pos(0), _fill(0), _fd(fd), _failed(false) {}
130-
131-
~fnTcpClientRxBuffer() { free(_buffer); }
132-
133-
bool failed() { return _failed; }
134-
135-
// Read data and return how many bytes were read
136-
int read(uint8_t *dst, size_t len)
137-
{
138-
// Fail if bad param or we're at the end of our buffer and couldn't get more data
139-
if (!dst || !len || (_pos == _fill && !fillBuffer()))
140-
{
141-
return -1;
142-
}
143-
144-
size_t a = _fill - _pos;
145-
if (len <= a || ((len - a) <= (_size - _fill) && fillBuffer() >= (len - a)))
146-
{
147-
if (len == 1)
148-
{
149-
*dst = _buffer[_pos];
150-
}
151-
else
152-
{
153-
memcpy(dst, _buffer + _pos, len);
154-
}
155-
_pos += len;
156-
return len;
157-
}
158-
159-
size_t left = len;
160-
size_t toRead = a;
161-
uint8_t *buf = dst;
162-
memcpy(buf, _buffer + _pos, toRead);
163-
_pos += toRead;
164-
left -= toRead;
165-
buf += toRead;
166-
while (left)
167-
{
168-
if (!fillBuffer())
169-
return len - left;
170-
171-
a = _fill - _pos;
172-
toRead = (a > left) ? left : a;
173-
memcpy(buf, _buffer + _pos, toRead);
174-
_pos += toRead;
175-
left -= toRead;
176-
buf += toRead;
177-
}
178-
return len;
179-
}
180-
181-
// Return value at current buffer position
182-
int peek()
183-
{
184-
// Return an error if we don't have any more data in our buffer and couldn't get more
185-
if (_pos == _fill && !fillBuffer())
186-
return -1;
187-
188-
return _buffer[_pos];
189-
}
190-
191-
size_t available()
192-
{
193-
return _fill - _pos + r_available();
194-
}
195-
};
196-
#endif
197-
19840
class fnTcpClientSocketHandle
19941
{
20042
private:
@@ -217,11 +59,7 @@ fnTcpClient::fnTcpClient(int fd)
21759
{
21860
_connected = true;
21961
_clientSocketHandle.reset(new fnTcpClientSocketHandle(fd));
220-
#if 0
221-
_rxBuffer.reset(new fnTcpClientRxBuffer(fd));
222-
#else
22362
_rxBuffer.clear();
224-
#endif
22563
}
22664

22765
fnTcpClient::~fnTcpClient()
@@ -232,9 +70,6 @@ fnTcpClient::~fnTcpClient()
23270
void fnTcpClient::stop()
23371
{
23472
_clientSocketHandle = nullptr;
235-
#if 0
236-
_rxBuffer = nullptr;
237-
#endif
23873
_connected = false;
23974
}
24075

@@ -365,11 +200,7 @@ int fnTcpClient::connect(in_addr_t ip, uint16_t port, int32_t timeout)
365200
#endif
366201
// Create a socket handle and recieve buffer objects
367202
_clientSocketHandle.reset(new fnTcpClientSocketHandle(sockfd));
368-
#if 0
369-
_rxBuffer.reset(new fnTcpClientRxBuffer(sockfd));
370-
#else
371203
_rxBuffer.clear();
372-
#endif
373204
_connected = true;
374205

375206
return 1;
@@ -541,16 +372,6 @@ size_t fnTcpClient::write(uint8_t data)
541372
// Fill buffer with read data
542373
int fnTcpClient::read(uint8_t *buf, size_t size)
543374
{
544-
#if 0
545-
int res = -1;
546-
res = _rxBuffer->read(buf, size);
547-
if (_rxBuffer->failed())
548-
{
549-
Debug_printf("fail on fd %d, errno: %d, \"%s\"\r\n", fd(), errno, strerror(errno));
550-
stop();
551-
}
552-
return res;
553-
#else
554375
size_t rlen;
555376

556377
rlen = std::min(available(), size);
@@ -560,7 +381,6 @@ int fnTcpClient::read(uint8_t *buf, size_t size)
560381
_rxBuffer.erase(0, rlen);
561382
}
562383
return rlen;
563-
#endif
564384
}
565385

566386
// Read one byte of data. Return read byte or negative value for error
@@ -595,19 +415,9 @@ int fnTcpClient::read_until(char terminator, char *buf, size_t size)
595415
// Peek at next byte available for reading
596416
int fnTcpClient::peek()
597417
{
598-
#if 0
599-
int res = _rxBuffer->peek();
600-
if (_rxBuffer->failed())
601-
{
602-
Debug_printf("fail on fd %d, errno: %d, \"%s\"\r\n", fd(), errno, strerror(errno));
603-
stop();
604-
}
605-
return res;
606-
#else
607418
if (_rxBuffer.size())
608419
return _rxBuffer[0];
609420
return -1;
610-
#endif
611421
}
612422

613423
void fnTcpClient::updateFIFO()
@@ -651,21 +461,8 @@ void fnTcpClient::updateFIFO()
651461
// Return number of bytes available for reading
652462
size_t fnTcpClient::available()
653463
{
654-
#if 0
655-
if (!_rxBuffer)
656-
return 0;
657-
658-
int res = _rxBuffer->available();
659-
if (_rxBuffer->failed())
660-
{
661-
Debug_printf("fail on fd %d, errno: %d, \"%s\"\r\n", fd(), errno, strerror(errno));
662-
stop();
663-
}
664-
return res;
665-
#else
666464
updateFIFO();
667465
return _rxBuffer.size();
668-
#endif
669466
}
670467

671468
// Send all pending data and clear receive buffer

lib/tcpip/fnTcpClient.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@
44
#define _FN_TCPCLIENT_H_
55

66
#include <memory>
7+
#include <string>
78

89
#include "compat_inet.h"
910

1011
class fnTcpClientSocketHandle;
11-
#if 0
12-
class fnTcpClientRxBuffer;
13-
#endif
1412

1513
class fnTcpClient
1614
{
1715
protected:
18-
#if 0
19-
std::shared_ptr<fnTcpClientRxBuffer> _rxBuffer;
20-
#else
2116
std::string _rxBuffer;
22-
#endif
2317
std::shared_ptr<fnTcpClientSocketHandle> _clientSocketHandle;
2418
bool _connected = false;
2519

0 commit comments

Comments
 (0)