From 877044dc40ee5482cc56444ba9bcd55e50a8745d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 18 Mar 2008 18:45:46 +0000 Subject: anyrtpproxy cleanup works now --- PracticalSocket.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'PracticalSocket.cpp') diff --git a/PracticalSocket.cpp b/PracticalSocket.cpp index 5d39557..6f7b51c 100644 --- a/PracticalSocket.cpp +++ b/PracticalSocket.cpp @@ -62,6 +62,7 @@ #include // For inet_addr() #include // For close() #include // For sockaddr_in + #include typedef void raw_type; // Type used for raw data on this platform #endif @@ -250,6 +251,24 @@ int CommunicatingSocket::recv(void *buffer, int bufferLen) return rtn; } +int CommunicatingSocket::recvNonBlocking(void *buffer, int bufferLen, int timeOut) + throw(SocketException) +{ + struct pollfd pfd[1]; + pfd[0].fd = sockDesc; + pfd[0].events = POLLIN; + int rtn = poll(pfd,1,timeOut); + if(rtn > 0) { + if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { + throw SocketException("non blocking receive failed", true); + } + if(!rtn) { + throw SocketException("connection closed by peer", false); + } + } + return rtn; +} + string CommunicatingSocket::getForeignAddress() throw(SocketException) { sockaddr_in addr; @@ -399,6 +418,29 @@ int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress, return rtn; } +int UDPSocket::recvFromNonBlocking(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort, int timeOut) throw(SocketException) { + sockaddr_in clntAddr; + socklen_t addrLen = sizeof(clntAddr); + struct pollfd pfd[1]; + pfd[0].fd = sockDesc; + pfd[0].events = POLLIN; + int rtn = poll(pfd,1,timeOut); + if(rtn > 0) { + if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) { + throw SocketException("Receive failed (recvfrom())", true); + } + if(!rtn) { + throw SocketException("connection closed by peer", false); + } + } + sourceAddress = inet_ntoa(clntAddr.sin_addr); + sourcePort = ntohs(clntAddr.sin_port); + + return rtn; +} + void UDPSocket::setMulticastTTL(unsigned char multicastTTL) throw(SocketException) { if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) { -- cgit v1.2.3