1 #ifndef __SOCKET_TCP_H__
2 #define __SOCKET_TCP_H__
16 inline int SetNoDelay(
bool flag =
true);
17 inline int SetLinger(
int interval_seconds = 0);
18 inline int DontLinger();
19 inline int SetSendBufferSize(
int insize);
21 inline bool ActiveOpen(
const Socket_Address &theaddress,
bool setdelay);
22 inline bool ActiveOpenNonBlocking(
const Socket_Address &theaddress);
23 inline bool ErrorIs_WouldBlocking(
int err);
24 inline bool ShutdownSend();
25 inline int SendData(
const std::string &str);
28 std::string RecvData(
int max_len);
30 inline int SendData(
const char *data,
int size);
31 inline int RecvData(
char *data,
int size);
37 static void init_type() {
38 Socket_IP::init_type();
40 Socket_IP::get_class_type());
43 return get_class_type();
45 virtual TypeHandle force_init_type() {init_type();
return get_class_type();}
55 Socket_TCP(SOCKET sck) : ::
Socket_IP(sck) {
63 int value = flag ? 1 : 0;
64 if (setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (
const char *)&value,
sizeof(value))) {
76 ll.l_linger = interval_seconds;
78 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *)&ll,
sizeof(linger));
95 int ret1 = setsockopt(_socket, SOL_SOCKET, SO_LINGER, (
const char *)&ll,
sizeof(linger));
108 if (setsockopt(_socket, SOL_SOCKET, SO_SNDBUF, (
char *)&insize,
sizeof(
int))) {
121 if (_socket == BAD_SOCKET) {
129 if (DO_CONNECT(_socket, &theaddress.GetAddressInfo()) != 0) {
144 if (_socket == BAD_SOCKET) {
151 if (DO_CONNECT(_socket, &theaddress.GetAddressInfo()) != 0) {
152 if (GETERROR() != LOCAL_CONNECT_BLOCKING) {
153 printf(
"Non Blocking Connect Error %d", GETERROR());
165 inline int Socket_TCP::
166 SendData(
const char *data,
int size) {
167 return DO_SOCKET_WRITE(_socket, data, size);
176 int ecode = DO_SOCKET_READ(_socket, data, len);
187 char *buffer = (
char *)malloc(max_len + 1);
188 int ecode =
RecvData(buffer, max_len);
190 str.assign(buffer, ecode);
198 inline bool Socket_TCP::
199 ErrorIs_WouldBlocking(
int err) {
200 return (GETERROR() == LOCAL_BLOCKING_ERROR);
203 inline bool Socket_TCP::
205 return do_shutdown_send(_socket);
219 inline int Socket_TCP::
220 SendData(
const std::string &str) {
221 return SendData(str.data(), str.size());
224 #endif //__SOCKET_TCP_H__