I'm trying to create TCP client class in C++. I wrote my class like below and on const I want to enable receive_once and send_data only.
send_data and receive_once has some exception to disconnect directly(something like sigpipe, negative return from receive...). So send_data, receive_once have to call disconnect in some cases.
class TcpClient {
public:
TcpClient();
void connect_server(ip, port);
void disconnect();
void send_data(payload) const {
if (sigpipe) disconnect();
}
void receive_once(payload) const {
if (len == -1) disconnect();
}
}
But I want to enable send_data, receive_once only on const. I want to disable connect_server, disconnect directly on const but, it has to disconnected on some cases like sigpipe. Is there any trick to do this?
int main() {
const TcpClient tcp_client;
tcp_client.connect_server(ip, port); // DISABLE
tcp_client.disconnect(); //DISABLE
tcp_client.send_data(payload); //ENABLE
tcp_client.receive_once(payload); //ENABLE
}