2015-07-13 22:46:41 -07:00
|
|
|
|
/*
|
|
|
|
|
|
TCPStream.h
|
|
|
|
|
|
|
|
|
|
|
|
TCPStream class definition. TCPStream provides methods to trasnfer
|
|
|
|
|
|
data between peers over a TCP/IP connection.
|
|
|
|
|
|
|
|
|
|
|
|
------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
Copyright <EFBFBD> 2013 [Vic Hargrave - http://vichargrave.com]
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2016-07-27 00:39:38 -07:00
|
|
|
|
#include "tcpsockets/TCPStream.h"
|
2015-07-13 22:46:41 -07:00
|
|
|
|
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
#include <WinSock2.h>
|
|
|
|
|
|
#else
|
2015-07-13 22:46:41 -07:00
|
|
|
|
#include <arpa/inet.h>
|
2015-12-09 00:58:06 -08:00
|
|
|
|
#include <netinet/tcp.h>
|
2015-07-13 22:46:41 -07:00
|
|
|
|
#include <unistd.h>
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
2015-07-13 22:46:41 -07:00
|
|
|
|
|
2016-07-27 00:39:38 -07:00
|
|
|
|
using namespace wpi;
|
|
|
|
|
|
|
|
|
|
|
|
TCPStream::TCPStream(int sd, sockaddr_in* address) : m_sd(sd) {
|
2015-07-13 22:46:41 -07:00
|
|
|
|
char ip[50];
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
unsigned long size = sizeof(ip) - 1;
|
2016-07-27 00:39:38 -07:00
|
|
|
|
WSAAddressToString((sockaddr*)address, sizeof sockaddr_in, nullptr, ip, &size);
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#else
|
2016-07-27 00:39:38 -07:00
|
|
|
|
inet_ntop(PF_INET, (in_addr*)&(address->sin_addr.s_addr), ip,
|
2015-07-13 22:46:41 -07:00
|
|
|
|
sizeof(ip) - 1);
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
2015-07-13 22:46:41 -07:00
|
|
|
|
m_peerIP = ip;
|
|
|
|
|
|
m_peerPort = ntohs(address->sin_port);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TCPStream::~TCPStream() { close(); }
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t TCPStream::send(const char* buffer, std::size_t len, Error* err) {
|
|
|
|
|
|
if (m_sd < 0) {
|
|
|
|
|
|
*err = kConnectionClosed;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
WSABUF wsaBuf;
|
|
|
|
|
|
wsaBuf.buf = const_cast<char*>(buffer);
|
|
|
|
|
|
wsaBuf.len = (ULONG)len;
|
|
|
|
|
|
DWORD rv;
|
|
|
|
|
|
bool result = true;
|
|
|
|
|
|
while (WSASend(m_sd, &wsaBuf, 1, &rv, 0, nullptr, nullptr) == SOCKET_ERROR) {
|
|
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
|
|
result = false;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
Sleep(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
|
char Buffer[128];
|
2015-08-28 14:16:49 -07:00
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
|
sprintf_s(Buffer, "Send() failed: WSA error=%d\n", WSAGetLastError());
|
|
|
|
|
|
#else
|
|
|
|
|
|
std::snprintf(Buffer, 128, "Send() failed: WSA error=%d\n", WSAGetLastError());
|
|
|
|
|
|
#endif
|
2015-08-04 00:01:13 -07:00
|
|
|
|
OutputDebugStringA(Buffer);
|
|
|
|
|
|
*err = kConnectionReset;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
#else
|
2015-07-13 22:46:41 -07:00
|
|
|
|
ssize_t rv = write(m_sd, buffer, len);
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
|
|
*err = kConnectionReset;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
2015-07-13 22:46:41 -07:00
|
|
|
|
return static_cast<std::size_t>(rv);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::size_t TCPStream::receive(char* buffer, std::size_t len, Error* err,
|
|
|
|
|
|
int timeout) {
|
|
|
|
|
|
if (m_sd < 0) {
|
|
|
|
|
|
*err = kConnectionClosed;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
int rv;
|
|
|
|
|
|
#else
|
2015-07-13 22:46:41 -07:00
|
|
|
|
ssize_t rv;
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
if (timeout <= 0) {
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
rv = recv(m_sd, buffer, len, 0);
|
|
|
|
|
|
#else
|
2015-07-13 22:46:41 -07:00
|
|
|
|
rv = read(m_sd, buffer, len);
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (WaitForReadEvent(timeout)) {
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
rv = recv(m_sd, buffer, len, 0);
|
|
|
|
|
|
#else
|
2015-07-13 22:46:41 -07:00
|
|
|
|
rv = read(m_sd, buffer, len);
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
|
|
|
|
|
} else {
|
2015-07-13 22:46:41 -07:00
|
|
|
|
*err = kConnectionTimedOut;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
|
|
*err = kConnectionReset;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
return static_cast<std::size_t>(rv);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TCPStream::close() {
|
2015-07-17 22:27:49 -07:00
|
|
|
|
if (m_sd >= 0) {
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
::shutdown(m_sd, SD_BOTH);
|
|
|
|
|
|
closesocket(m_sd);
|
|
|
|
|
|
#else
|
2015-07-17 22:27:49 -07:00
|
|
|
|
::shutdown(m_sd, SHUT_RDWR);
|
2015-07-13 22:46:41 -07:00
|
|
|
|
::close(m_sd);
|
2015-08-04 00:01:13 -07:00
|
|
|
|
#endif
|
2015-07-17 22:27:49 -07:00
|
|
|
|
}
|
2015-07-13 22:46:41 -07:00
|
|
|
|
m_sd = -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-08-02 00:06:27 -07:00
|
|
|
|
llvm::StringRef TCPStream::getPeerIP() const { return m_peerIP; }
|
|
|
|
|
|
|
|
|
|
|
|
int TCPStream::getPeerPort() const { return m_peerPort; }
|
|
|
|
|
|
|
2015-12-09 00:58:06 -08:00
|
|
|
|
void TCPStream::setNoDelay() {
|
|
|
|
|
|
int optval = 1;
|
|
|
|
|
|
setsockopt(m_sd, IPPROTO_TCP, TCP_NODELAY, (char*)&optval, sizeof optval);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-07-13 22:46:41 -07:00
|
|
|
|
bool TCPStream::WaitForReadEvent(int timeout) {
|
|
|
|
|
|
fd_set sdset;
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
|
|
|
|
tv.tv_sec = timeout;
|
|
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
FD_ZERO(&sdset);
|
|
|
|
|
|
FD_SET(m_sd, &sdset);
|
|
|
|
|
|
if (select(m_sd + 1, &sdset, NULL, NULL, &tv) > 0) {
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|