}
int64_t iBytesWritten = 0;
- struct timeval *tv;
+ struct timeval *tv(NULL);
while (iBytesWritten < len)
{
#include "../os.h"
#include "../sockets/socket.h"
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <poll.h>
+
namespace PLATFORM
{
+ inline void TcpShutdownSocket(socket_t socket)
+ {
+ shutdown(socket, SHUT_RDWR);
+ }
+
inline int TcpGetSocketError(socket_t socket)
{
int iReturn(0);
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];
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);
}
}
{
public:
CSocket(void) :
- m_socket(INVALID_SOCKET) {};
+ m_socket(INVALID_SOCKET),
+ m_iError(0) {};
+
virtual ~CSocket(void)
{
Close();
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;
}
return strReturn;
}
+ virtual int GetErrorNumber(void) const
+ {
+ return m_iError;
+ }
+
protected:
socket_t m_socket;
CStdString m_strError;
+ int m_iError;
CMutex m_mutex;
};
};
#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;
}
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;