TCPSocket inherits from both Socket and TLS, both declaring receiveData()
as unrelated sibling methods - only Socket::receiveData was virtual, so
TLS::receiveData (SSL_read) was never reached regardless of TLS config,
and even when reached its result was never forwarded to onDataReceived
(that call was commented out). Added an explicit TCPSocket::receiveData
override dispatching to TLS::receiveData or Socket::receiveData based on
tlsInfo, and completed TLS::receiveData to actually return/forward the
decrypted bytes.
Write path did not exist at all. Added Socket::sendData() as a new
virtual hook mirroring receiveData, routed Socket::writeSocket() through
it instead of a raw ::write(), added TLS::sendData() (SSL_write, same
error-handling style as the existing TLS methods), and TCPSocket::sendData()
dispatching the same way the read side does.
Also fixed: a queued write in writeSocket() was being popped off the fifo
as if sent even when shutDown was true and nothing was actually written;
removed a dead empty if(tlsInfo){} stub in onDataReceived, no longer
needed now dispatch happens one level up; replaced leftover debug
std::cout lines with proper Log() calls matching the rest of the file.
Verified via full local compile: all 20 .cpp files build clean, link
into libServerCore.a, and nm confirms the new/fixed symbols are correctly
defined in the right object files.
142 lines
4.0 KiB
C++
142 lines
4.0 KiB
C++
|
|
#ifndef __TCPSocket_h__
|
|
#define __TCPSocket_h__
|
|
|
|
#include "Socket.h"
|
|
#include "TLS.h"
|
|
#include "TLSInfo.h"
|
|
#include "IPAddress.h"
|
|
|
|
namespace core {
|
|
|
|
///
|
|
/// TCPSocket
|
|
///
|
|
/// Provides a network TCP socket.
|
|
///
|
|
/// For accessing TCP network functions use this object. The connection oriented nature of TCP
|
|
/// provides a single client persistent connection with data error correction and a durable
|
|
/// synchronous data connection.
|
|
///
|
|
|
|
class TCPSocket : public Socket, public TLS {
|
|
|
|
public:
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
TCPSocket(EPoll &ePoll);
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
TCPSocket(EPoll &ePoll, std::string text);
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text);
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text);
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
virtual ~TCPSocket();
|
|
|
|
///
|
|
///
|
|
///
|
|
|
|
void connect(IPAddress &address);
|
|
|
|
IPAddress ipAddress;
|
|
|
|
///
|
|
/// The output method is called by a socket session (TCPSession) and
|
|
/// will output the detail information for the client socket. When extending
|
|
/// TCPSocket or TCPSession you can override the method to add attributes
|
|
/// to the list.
|
|
///
|
|
|
|
virtual void output(std::stringstream &out);
|
|
|
|
///
|
|
/// Override this method to receive data directly from the socket as data is
|
|
/// received. If you need data split by line termination characters then
|
|
/// override the onLineReceived method instead.
|
|
///
|
|
|
|
virtual void onDataReceived(coreutils::ZString &data) override;
|
|
|
|
///
|
|
/// Override the onLineReceived method to receive a string of characters that
|
|
/// represents a single line of data terminated by a LF or CRLF. If onDataReceived
|
|
/// was overriden this method will not be called unless the onDataReceived calls
|
|
/// this method explicitly using the class and member name.
|
|
///
|
|
|
|
virtual void onLineReceived(coreutils::ZString &line);
|
|
|
|
///
|
|
/// Override the onBlockReceived method to receive a string of characters that
|
|
/// represents a single block of data of length determined by the block length value. If
|
|
/// onDataReceived was overriden this method will not be called unless the onDataReceived
|
|
/// calls this method explicitly using the class and member name.
|
|
///
|
|
|
|
virtual void onBlockReceived(coreutils::ZString &block);
|
|
|
|
///
|
|
/// Use setBlockSize to set the amount of data that should be read at once from the
|
|
/// session data buffer.
|
|
/// If this value is set to 0 then the data will be retrieved
|
|
///
|
|
|
|
void setBlockSize(int size = 0);
|
|
|
|
///
|
|
/// Dispatches to TLS::receiveData (SSL_read) when this socket was constructed with a
|
|
/// TLSInfo/SSL_CTX, otherwise falls back to Socket::receiveData (plain ::read). Needed
|
|
/// because Socket and TLS both declare receiveData as unrelated sibling methods -
|
|
/// Socket::receiveData is virtual but TLS::receiveData does not override it, so without
|
|
/// this explicit override TLS::receiveData would never be reached and encrypted bytes
|
|
/// would be handed to onDataReceived as if they were plaintext.
|
|
///
|
|
|
|
virtual void receiveData(coreutils::ZString &buffer) override;
|
|
|
|
///
|
|
/// Dispatches to TLS::sendData (SSL_write) when this socket was constructed with a
|
|
/// TLSInfo/SSL_CTX, otherwise falls back to Socket::sendData (plain ::write). Mirrors
|
|
/// the receiveData override above for the write path.
|
|
///
|
|
|
|
virtual int sendData(const char *data, int length) override;
|
|
|
|
protected:
|
|
bool term = false;
|
|
TLSInfo *tlsInfo;
|
|
|
|
private:
|
|
char *lineBuffer = NULL;
|
|
int lineBufferSize = 0;
|
|
int lineLength = 0;
|
|
int blockLength = 0;
|
|
int blockSize = 0;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|