0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-27 08:41:02 +08:00

common code for linux and windows

This commit is contained in:
Sergey Yagovtsev 2016-09-20 00:54:56 +03:00
parent 38eecc722d
commit 4c73886233
4 changed files with 30 additions and 83 deletions

View File

@ -54,10 +54,9 @@ public:
CONNECTION_STATE_DISCONNECTED
};
private:
void checkResult(int result);
bool checkSocket(socket_t s) const;
static int _close(socket_t s);
@ -69,17 +68,13 @@ private:
int wsaret = -1;
#ifndef _WIN32
struct hostent * server;
struct sockaddr_in serv_addr;
struct hostent *server = nullptr;
#else
struct addrinfo *result = NULL;
struct addrinfo hints;
#endif
ConnectionState m_state = CONNECTION_STATE_UNKNOWN;
public:
EasySocket();
~EasySocket();

View File

@ -26,6 +26,13 @@ add_definitions(
-D_BUILD_PROFILER
)
if(WIN32)
add_definitions(
-D_WINSOCK_DEPRECATED_NO_WARNINGS
)
endif(WIN32)
add_library(${PROJECT_NAME} SHARED ${SOURCES})
if(UNIX)

View File

@ -218,10 +218,21 @@ int EasySocket::accept()
return m_replySocket;
}
#ifndef _WIN32
#include <strings.h>
#include <errno.h>
bool EasySocket::setAddress(const char *serv, uint16_t portno)
{
server = gethostbyname(serv);
if (server == NULL) {
return false;
//fprintf(stderr,"ERROR, no such host\n");
}
memset((char *)&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
memcpy((char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length);
serv_addr.sin_port = htons(portno);
return true;
}
int EasySocket::connect()
{
@ -230,6 +241,7 @@ int EasySocket::connect()
//fprintf(stderr,"ERROR, no such host\n");
}
int res = ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
checkResult(res);
if(res == 0){
struct timeval tv;
@ -243,66 +255,3 @@ int EasySocket::connect()
}
return res;
}
bool EasySocket::setAddress(const char *serv, uint16_t portno)
{
server = gethostbyname(serv);
if (server == NULL) {
return false;
//fprintf(stderr,"ERROR, no such host\n");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
return true;
}
#else
bool EasySocket::setAddress(const char *serv, uint16_t portno)
{
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
int iResult;
char buffer[20] = {};
_itoa(portno, buffer, 10);
iResult = getaddrinfo(serv, buffer, &hints, &result);
if (iResult != 0) {
return false;
}
return true;
}
int EasySocket::connect()
{
if (!m_socket || !result){
return -1;
}
// Connect to server.
auto iResult = ::connect(m_socket, result->ai_addr, (int)result->ai_addrlen);
checkResult(iResult);
if (iResult == SOCKET_ERROR) {
return iResult;
}
/**/
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
m_replySocket = m_socket;
return iResult;
}
#endif

View File

@ -640,9 +640,6 @@ void ProfileManager::startListen()
dumpBlocksToStream(os);
dm.size = (uint32_t)os.stream().str().length();
//dm.size = 8192*4;
int packet_size = int(sizeof(dm)) + int(dm.size);
char *sendbuf = new char[packet_size];
@ -654,16 +651,15 @@ void ProfileManager::startListen()
bytes = socket.send(sendbuf, packet_size);
hasConnect = bytes > 0;
std::string tempfilename = "test_snd.prof";
/*std::string tempfilename = "test_snd.prof";
std::ofstream of(tempfilename, std::fstream::binary);
of.write((const char*)os.stream().str().c_str(), dm.size);
of.close();
of.close();*/
delete[] sendbuf;
//std::this_thread::sleep_for(std::chrono::seconds(2));
replyMessage.type = profiler::net::MESSAGE_TYPE_REPLY_END_SEND_BLOCKS;
bytes = socket.send(&replyMessage, sizeof(replyMessage));
//hasConnect = bytes > 0;
hasConnect = bytes > 0;
}
break;
default: