Remove some duplicated code in serialport
Reuse SendArray implementation instead of repeating the same code pattern several times. This change started as a removal of warning about comparison of integer expressions of different signedness: ‘int’ and ‘Bitu’ (when comparing return value of SDLNet_TCP_Send), but turned into a small cleanup and removal of dead code.
This commit is contained in:
parent
c132263116
commit
534a603cb8
2 changed files with 26 additions and 59 deletions
|
@ -16,16 +16,10 @@
|
|||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
/* $Id $ */
|
||||
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#if C_MODEM
|
||||
|
||||
/*****************************************************************************/
|
||||
// C++ SDLnet wrapper
|
||||
|
||||
#include "misc_util.h"
|
||||
|
||||
struct _TCPsocketX {
|
||||
|
@ -198,7 +192,6 @@ bool TCPClientSocket::ReceiveArray(Bit8u* data, Bitu* size) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
Bits TCPClientSocket::GetcharNonBlock() {
|
||||
// return:
|
||||
// -1: no data
|
||||
|
@ -214,66 +207,40 @@ Bits TCPClientSocket::GetcharNonBlock() {
|
|||
}
|
||||
else return -1;
|
||||
}
|
||||
bool TCPClientSocket::Putchar(Bit8u data) {
|
||||
if(SDLNet_TCP_Send(mysock, &data, 1)!=1) {
|
||||
isopen=false;
|
||||
|
||||
bool TCPClientSocket::Putchar(Bit8u data)
|
||||
{
|
||||
return SendArray(&data, 1);
|
||||
}
|
||||
|
||||
bool TCPClientSocket::SendArray(Bit8u* data, Bitu bufsize)
|
||||
{
|
||||
if (SDLNet_TCP_Send(mysock, data, bufsize) != (int)bufsize) {
|
||||
isopen = false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TCPClientSocket::SendArray(Bit8u* data, Bitu bufsize) {
|
||||
if(SDLNet_TCP_Send(mysock, data, bufsize)!=bufsize) {
|
||||
isopen=false;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TCPClientSocket::SendByteBuffered(Bit8u data) {
|
||||
|
||||
if(sendbufferindex==(sendbuffersize-1)) {
|
||||
// buffer is full, get rid of it
|
||||
sendbuffer[sendbufferindex]=data;
|
||||
sendbufferindex=0;
|
||||
|
||||
if(SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize)!=sendbuffersize) {
|
||||
isopen=false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
sendbuffer[sendbufferindex]=data;
|
||||
bool TCPClientSocket::SendByteBuffered(Bit8u data)
|
||||
{
|
||||
if (sendbufferindex < (sendbuffersize - 1)) {
|
||||
sendbuffer[sendbufferindex] = data;
|
||||
sendbufferindex++;
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
// buffer is full, get rid of it
|
||||
sendbuffer[sendbufferindex] = data;
|
||||
sendbufferindex = 0;
|
||||
return SendArray(sendbuffer, sendbuffersize);
|
||||
}
|
||||
/*
|
||||
bool TCPClientSocket::SendArrayBuffered(Bit8u* data, Bitu bufsize) {
|
||||
|
||||
Bitu bytes
|
||||
while(
|
||||
|
||||
// first case, buffer already full
|
||||
if(sendbufferindex==(sendbuffersize-1)) {
|
||||
// buffer is full, get rid of it
|
||||
sendbuffer[sendbufferindex]=data;
|
||||
sendbufferindex=0;
|
||||
|
||||
if(SDLNet_TCP_Send(mysock, sendbuffer, sendbuffersize)!=sendbuffersize) {
|
||||
isopen=false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
void TCPClientSocket::FlushBuffer() {
|
||||
if(sendbufferindex) {
|
||||
if(SDLNet_TCP_Send(mysock, sendbuffer,
|
||||
sendbufferindex)!=sendbufferindex) {
|
||||
isopen=false;
|
||||
|
||||
void TCPClientSocket::FlushBuffer()
|
||||
{
|
||||
if (sendbufferindex) {
|
||||
if (!SendArray(sendbuffer, sendbufferindex))
|
||||
return;
|
||||
}
|
||||
sendbufferindex=0;
|
||||
sendbufferindex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -322,4 +289,5 @@ TCPClientSocket* TCPServerSocket::Accept() {
|
|||
|
||||
return new TCPClientSocket(new_tcpsock);
|
||||
}
|
||||
|
||||
#endif // #if C_MODEM
|
||||
|
|
|
@ -90,7 +90,6 @@ class TCPClientSocket {
|
|||
|
||||
// buffered send functions
|
||||
bool SendByteBuffered(Bit8u data);
|
||||
bool SendArrayBuffered(Bit8u* data, Bitu bufsize);
|
||||
|
||||
private:
|
||||
TCPsocket mysock;
|
||||
|
|
Loading…
Add table
Reference in a new issue