32 #ifdef HAVE_UNIX_DOMAIN_PROTOCOL
41 #define CLOSE_SEND 0x01
42 #define CLOSE_RECEIVE 0x02
43 #define CLOSE_BOTH 0x03
75 return create(AF_INET,
true);
85 return create(AF_INET, a_bShowTypicalError);
96 s=::socket(
type,SOCK_STREAM,0);
101 CAMsg::printMsg(LOG_CRIT,
"Could not create a new normal Socket -- allowed number of normal sockets exceeded!\n");
111 if (a_bShowTypicalError)
119 CAMsg::printMsg(LOG_CRIT,
"Could not create a new Socket! - Error: %s (%i)\n",
148 #ifdef HAVE_UNIX_DOMAIN_PROTOCOL
155 SINT32 ret=::unlink((
char*)path);
157 CAMsg::printMsg(LOG_ERR,
"CASocket::listen() -- could not unlink unix domain socket file name %s -- a call to bind or listen may fail...\n",path);
183 return listen(oSocketAddrINet);
200 CAMsg::printMsg(LOG_CRIT,
"CASocket::accept() -- Could not create a new normal Socket -- allowed number of normal sockets exeded!\n");
240 for(
UINT32 i=0;i<retry;i++)
279 bool bWasNonBlocking=
false;
291 if(err!=WSAEWOULDBLOCK)
302 tval.tv_sec=msTimeOut/1000;
303 tval.tv_usec=(msTimeOut%1000)*1000;
304 fd_set readSet,writeSet;
307 #pragma warning( push )
308 #pragma warning( disable : 4127 )
311 #pragma warning( pop )
312 err=::select(
m_Socket+1,&readSet,&writeSet,NULL,&tval);
314 struct pollfd opollfd;
316 opollfd.events=POLLIN|POLLOUT;
317 err=::poll(&opollfd,1,msTimeOut);
334 if (::getsockopt(
m_Socket,SOL_SOCKET,SO_ERROR,(
char*)&err,&
len)<0||err!=0)
364 "Could not get lock for closing socket! Error code: %d\n", ret);
425 CAMsg::printMsg(LOG_DEBUG,
"Fehler beim Socket-send:E_UNKNOWN (ef=%i)\n",ef);
446 bool bWasNonBlocking;
455 if(aktTimeOut>0&&(
UINT32)aktTimeOut==msTimeOut)
497 UINT64 startupTime, currentMillis;
500 bool bWasNonBlocking;
509 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendFullyTimeOut() -->non-blocking case\n");
516 CAMsg::printMsg(LOG_ERR,
"CASocket::sendFullyTimeOut() - could not set socket timeout!\n");
525 if (currentMillis >= (startupTime + msTimeOut))
528 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendFullyTimeOut() - timed out!\n");
538 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendFullyTimeOut() - send returned %i (expected: %u)!\n",ret,
len);
548 if (pSingleSocketGroup == NULL)
551 pSingleSocketGroup->
add(*
this);
553 ret= pSingleSocketGroup->
select(1000);
557 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendTimeOutFully() - error near select_once() ret=%i\n",ret);
566 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendTimeOutFully() - send returned %i\n",ret);
575 if (pSingleSocketGroup != NULL)
576 delete pSingleSocketGroup;
601 if (pSingleSocketGroup == NULL)
604 pSingleSocketGroup->
add(*
this);
607 ret= pSingleSocketGroup->
select(1000);
611 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendFully() - error near select_once() ret=%i\n",ret);
619 CAMsg::printMsg(LOG_DEBUG,
"CASocket::sendFully() - send returned %i\n",ret);
627 if (pSingleSocketGroup != NULL)
628 delete pSingleSocketGroup;
681 UINT64 currentTime,endTime;
683 set64(endTime,currentTime);
684 add64(endTime,msTimeOut);
710 msTimeOut=
diff64(endTime,currentTime);
719 UINT64 currentTime, endTime;
721 set64(endTime,currentTime);
722 add64(endTime,msTimeOut);
731 if(
byte ==
'\r' ||
byte ==
'\n')
750 msTimeOut=
diff64(endTime,currentTime);
752 while(
byte !=
'\n' && i<maxLen && ret > 0);
803 struct sockaddr_in addr;
804 socklen_t namelen=
sizeof(
struct sockaddr_in);
807 memcpy(r_Ip,&addr.sin_addr,4);
813 struct sockaddr_in addr;
814 socklen_t namelen=
sizeof(
struct sockaddr_in);
817 return ntohs(addr.sin_port);
822 struct sockaddr_in addr;
823 socklen_t namelen=
sizeof(
struct sockaddr_in);
826 memcpy(ip,&addr.sin_addr,4);
832 struct sockaddr_in addr;
833 socklen_t namelen=
sizeof(
struct sockaddr_in);
836 return ntohs(addr.sin_port);
843 return setsockopt(
m_Socket,SOL_SOCKET,SO_REUSEADDR,(
char*)&val,
sizeof(val));
849 return setsockopt(
m_Socket,SOL_SOCKET,SO_RCVBUF,(
char*)&val,
sizeof(val));
867 SINT32 ret=setsockopt(
m_Socket,SOL_SOCKET,SO_SNDBUF,(
char*)&val,
sizeof(val));
886 t.tv_sec=msTimeOut/1000;
887 t.tv_usec=(msTimeOut%1000)*1000;
888 return setsockopt(
m_Socket,SOL_SOCKET,SO_SNDTIMEO,(
char*)&t,
sizeof(t));
898 return val.tv_sec*1000+val.tv_usec/1000;
931 #ifdef HAVE_TCP_KEEPALIVE
935 CAMsg::printMsg(LOG_ERR,
"Socket option TCP-KEEP-ALIVE was not set! Reason: %s (%i)\n",
941 CAMsg::printMsg(LOG_INFO,
"Socket option TCP-KEEP-ALIVE was not set as it is not available on this machine.\n");
955 unsigned long flags=1;
965 unsigned long flags=0;
976 *b=((
flags&O_NONBLOCK)==O_NONBLOCK);
987 for(
UINT32 t=0;t<10001;t++)
995 for(
UINT32 t=0;t<maxSocket;t++)
997 parSocket[t].
close();
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
SINT32 msSleep(UINT32 ms)
Sleeps ms milliseconds.
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
void add64(UINT64 &op1, UINT32 op2)
bool isLesser64(UINT64 &smallOp1, UINT64 &bigOp2)
void set64(UINT64 &op1, UINT32 op2)
#define GET_NET_ERROR_STR(x)
#define ERR_INTERN_WOULDBLOCK
#define ERR_INTERN_CONNREFUSED
#define ERR_INTERN_SOCKET_CLOSED
#define ERR_INTERN_TIMEDOUT
#define ioctlsocket(a, b, c)
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
This is an abstract class for representing a socket address used in CASocket, CADatagramSocket and CA...
virtual SINT32 getType() const =0
The type (family) of socket for which this address is useful.
virtual const SOCKADDR * LPSOCKADDR() const =0
Casts to a SOCKADDR struct.
virtual SINT32 getSize() const =0
The size of the SOCKADDR struct needed by function of CASocket and other.
This class represents a socket address for Internet (IP) connections.
This is a class for Unix Domain Protocol Sockat Addresses.
CASocket(bool bIsReserved=false)
virtual SINT32 receive(UINT8 *buff, UINT32 len)
Will receive some bytes from the socket.
static CAMutex * m_pcsClose
virtual SINT32 getPeerPort()
virtual SINT32 listen(const CASocketAddr &psa)
Starts listening on address psa.
virtual SINT32 send(const UINT8 *buff, UINT32 len)
Sends some data over the network.
virtual SINT32 getNonBlocking(bool *b)
virtual SINT32 setKeepAlive(bool b)
Enables/disables the socket keep-alive option.
static UINT32 m_u32MaxNormalSockets
virtual SINT32 getSendTimeOut()
virtual SINT32 setSendTimeOut(UINT32 msTimeOut)
virtual SINT32 setRecvBuff(UINT32 r)
virtual SINT32 receiveFullyT(UINT8 *buff, UINT32 len, UINT32 msTimeOut)
Trys to receive all bytes.
virtual SINT32 getSendBuff()
static SINT32 getMaxOpenSockets()
Tries to find out how many socket we can open by open as many socket as possible witthout errors.
virtual SINT32 getPeerIP(UINT8 ip[4])
volatile bool m_bSocketIsClosed
check
CASingleSocketGroup * m_pSingleSocketGroupRead
virtual SINT32 receiveLine(UINT8 *line, UINT32 maxLen, UINT32 msTimeOut)
virtual SINT32 peek(UINT8 *buff, UINT32 len)
Will peek some bytes from the socket read queue.
virtual SINT32 sendTimeOut(const UINT8 *buff, UINT32 len, UINT32 msTimeOut)
Sends some data over the network.
virtual SINT32 setNonBlocking(bool b)
virtual SINT32 setReuseAddr(bool b)
virtual SINT32 sendFully(const UINT8 *buff, UINT32 len)
Sends all data over the network.
virtual SINT32 setSendBuff(SINT32 r)
Returns < 0 on error, otherwise the new sendbuffersize (which may be less than r)
virtual SINT32 accept(CASocket &s)
Accepts a new connection.
virtual SINT32 getRecvBuff()
virtual SINT32 sendFullyTimeOut(const UINT8 *buff, UINT32 len, UINT32 msTimeOut, UINT32 msTimeOutSingleSend)
Sends all data over the network.
virtual SINT32 setSocket(SOCKET s)
virtual SINT32 getLocalIP(UINT8 r_Ip[4])
LERNGRUPPE Returns the source address of the socket.
virtual SINT32 connect(const CASocketAddr &psa)
virtual SINT32 getLocalPort()
static volatile UINT32 m_u32NormalSocketsOpen
The following two variables are use to realise "reserved" sockets.