From: Lars Op den Kamp Date: Mon, 30 Jan 2012 11:15:47 +0000 (+0100) Subject: posix: fixed lib/platform/sockets/tcp.h compilation and some bugs. added CTcpSocket... X-Git-Tag: upstream/2.2.0~1^2~36^2~31 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=cb2397e953b3f892b4206d5bb87968ca4cccbac8;p=deb_libcec.git posix: fixed lib/platform/sockets/tcp.h compilation and some bugs. added CTcpSocket::Shutdown() --- diff --git a/src/lib/platform/posix/os-socket.h b/src/lib/platform/posix/os-socket.h index a57f69d..9d557c1 100644 --- a/src/lib/platform/posix/os-socket.h +++ b/src/lib/platform/posix/os-socket.h @@ -64,7 +64,7 @@ namespace PLATFORM } int64_t iBytesWritten = 0; - struct timeval *tv; + struct timeval *tv(NULL); while (iBytesWritten < len) { diff --git a/src/lib/platform/posix/os-tcp.h b/src/lib/platform/posix/os-tcp.h index eb9774c..86d0603 100644 --- a/src/lib/platform/posix/os-tcp.h +++ b/src/lib/platform/posix/os-tcp.h @@ -34,8 +34,20 @@ #include "../os.h" #include "../sockets/socket.h" +#include +#include +#include +#include +#include +#include + namespace PLATFORM { + inline void TcpShutdownSocket(socket_t socket) + { + shutdown(socket, SHUT_RDWR); + } + inline int TcpGetSocketError(socket_t socket) { int iReturn(0); @@ -67,13 +79,17 @@ namespace PLATFORM else if (iPollResult == -1) *iError = errno; else - *iError = TcpGetSocketError(socket); + bConnected = true; + } + else + { + *iError = errno; } return bConnected; } - inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo *info) + inline bool TcpResolveAddress(const char *strHost, uint16_t iPort, int *iError, struct addrinfo **info) { struct addrinfo hints; char service[33]; @@ -83,6 +99,7 @@ namespace PLATFORM hints.ai_protocol = IPPROTO_TCP; sprintf(service, "%d", iPort); - return ((*iError = getaddrinfo(strHost, service, &hints, &info)) == 0); + *iError = getaddrinfo(strHost, service, &hints, info); + return !(*iError); } } diff --git a/src/lib/platform/sockets/socket.h b/src/lib/platform/sockets/socket.h index 530bd1b..2e8acdb 100644 --- a/src/lib/platform/sockets/socket.h +++ b/src/lib/platform/sockets/socket.h @@ -48,7 +48,9 @@ namespace PLATFORM { public: CSocket(void) : - m_socket(INVALID_SOCKET) {}; + m_socket(INVALID_SOCKET), + m_iError(0) {}; + virtual ~CSocket(void) { Close(); @@ -72,18 +74,16 @@ namespace PLATFORM virtual int64_t Write(uint8_t* data, uint32_t len) { CLockObject lock(m_mutex); - int iError(0); - int64_t iReturn = SocketWrite(m_socket, &iError, data, len); - m_strError = strerror(iError); + int64_t iReturn = SocketWrite(m_socket, &m_iError, data, len); + m_strError = strerror(m_iError); return iReturn; } virtual int32_t Read(uint8_t* data, uint32_t len, uint64_t iTimeoutMs = 0) { CLockObject lock(m_mutex); - int iError(0); - int32_t iReturn = SocketRead(m_socket, &iError, data, len, iTimeoutMs); - m_strError = strerror(iError); + int32_t iReturn = SocketRead(m_socket, &m_iError, data, len, iTimeoutMs); + m_strError = strerror(m_iError); return iReturn; } @@ -94,9 +94,15 @@ namespace PLATFORM return strReturn; } + virtual int GetErrorNumber(void) const + { + return m_iError; + } + protected: socket_t m_socket; CStdString m_strError; + int m_iError; CMutex m_mutex; }; }; diff --git a/src/lib/platform/sockets/tcp.h b/src/lib/platform/sockets/tcp.h index 47c71b7..ef9c418 100644 --- a/src/lib/platform/sockets/tcp.h +++ b/src/lib/platform/sockets/tcp.h @@ -39,52 +39,62 @@ #include "../posix/os-tcp.h" #endif +using namespace std; + namespace PLATFORM { class CTcpSocket : public CSocket { public: - CTcpSocket(void); + CTcpSocket(void) {}; virtual ~CTcpSocket(void) {} virtual bool Open(const CStdString &strHostname, uint16_t iPort, uint64_t nTimeout) { bool bReturn(false); - struct addrinfo *address, *addr; - int iResult = TcpResolveAddress(strHostname, iPort, address); - if (iResult) + struct addrinfo *address(NULL), *addr(NULL); + CLockObject lock(m_mutex); + if (!TcpResolveAddress(strHostname.c_str(), iPort, &m_iError, &address)) { - m_strError = strerror(iResult); + m_strError = strerror(m_iError); return bReturn; } for(addr = address; !bReturn && addr; addr = addr->ai_next) { - int iError(0); - m_socket = TcpCreateSocket(addr, &iError, nTimeout); + m_socket = TcpCreateSocket(addr, &m_iError, nTimeout); if (m_socket != INVALID_SOCKET && m_socket != SOCKET_ERROR) bReturn = true; else - m_strError = strerror(iError); + m_strError = strerror(m_iError); } freeaddrinfo(address); return bReturn; } + virtual void Shutdown(void) + { + CLockObject lock(m_mutex); + if (m_socket != INVALID_SOCKET && m_socket != SOCKET_ERROR) + TcpShutdownSocket(m_socket); + m_socket = INVALID_SOCKET; + m_strError = ""; + } + protected: virtual socket_t TcpCreateSocket(struct addrinfo* addr, int* iError, uint64_t iTimeout) { socket_t fdSock = socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); - if (fdSock == -1) + if (fdSock == INVALID_SOCKET || fdSock == SOCKET_ERROR) { - *iError = TcpGetSocketError(); + *iError = errno; return (socket_t)SOCKET_ERROR; } - if (TcpConnectSocket(addr, fdSock, iError, iTimeout) != 0) + if (!TcpConnectSocket(fdSock, addr, iError, iTimeout)) { - closesocket(fdSock); + SocketClose(fdSock); return (socket_t)SOCKET_ERROR; } diff --git a/src/lib/platform/windows/os-tcp.h b/src/lib/platform/windows/os-tcp.h index 299ce37..b05a3dd 100644 --- a/src/lib/platform/windows/os-tcp.h +++ b/src/lib/platform/windows/os-tcp.h @@ -36,6 +36,11 @@ namespace PLATFORM { + inline void TcpShutdownSocket(socket_t socket) + { + shutdown(socket, SHUT_RDWR); + } + inline int TcpResolveAddress(const CStdString &strHostname, uint16_t iPort, struct addrinfo *address) { struct addrinfo hints;