0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-14 00:27:55 +08:00

Not compiling version of windows sock

This commit is contained in:
Sergey Yagovtsev 2016-09-09 06:14:34 +03:00
parent f95d88bb1a
commit 03765b08bc
6 changed files with 120 additions and 5 deletions

View File

@ -8,7 +8,7 @@ set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Network REQUIRED)
#find_package(Qt5Network REQUIRED)
include_directories(${ROOT}/3rdparty/nanomsg/src)

View File

@ -29,5 +29,8 @@ add_definitions(
add_library(${PROJECT_NAME} SHARED ${SOURCES})
if(UNIX)
set(PLATFORM_LIBS thread)
endif(UNIX)
target_link_libraries(${PROJECT_NAME} pthread)
target_link_libraries(${PROJECT_NAME} ${PLATFORM_LIBS})

View File

@ -17,9 +17,10 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
**/
#include "easy_socket.h"
#include <strings.h>
#ifndef _WIN32
#include <strings.h>
EasySocket::EasySocket()
{
@ -32,6 +33,10 @@ EasySocket::EasySocket()
setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
}
EasySocket::~EasySocket()
{
}
size_t EasySocket::write(const void *buf, size_t nbyte)
{
if(m_socket <= 0){
@ -74,5 +79,91 @@ int EasySocket::connect()
return ::connect(m_socket,(struct sockaddr *) &serv_addr,sizeof(serv_addr));
}
#else
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
EasySocket::EasySocket()
{
// socket
WSADATA wsaData;
int wsaret = WSAStartup(0x101, &wsaData);
if (wsaret == 0)
{
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);;
if (m_socket == INVALID_SOCKET) {
WSACleanup();
}
}
}
EasySocket::~EasySocket()
{
if (m_socket)
WSACleanup();
}
size_t EasySocket::write(const void *buf, size_t nbyte)
{
if (m_socket <= 0){
return -1;
}
return send(m_socket, (const char*)buf, nbyte,0);
}
size_t EasySocket::read(void *buf, size_t nbyte)
{
if (m_socket <= 0){
return -1;
}
return recv(m_socket, (char*)buf, nbyte,0);
}
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) {
WSACleanup();
return false;
}
return false;
}
int EasySocket::connect()
{
if (!m_socket || !result){
return -1;
}
SOCKET ConnectSocket = socket(result->ai_family, result->ai_socktype,
result->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
return -1;
}
// Connect to server.
auto iResult = ::connect(ConnectSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}
return iResult;
}
#endif

View File

@ -27,6 +27,21 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#else
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#endif
class EasySocket
@ -36,10 +51,15 @@ class EasySocket
uint16_t m_port = 0;
struct sockaddr_in serv_addr;
struct hostent *server = nullptr;
#else
SOCKET m_socket = 0;
struct addrinfo *result = NULL;
struct addrinfo hints;
#endif
public:
EasySocket();
~EasySocket();
size_t write(const void *buf, size_t nbyte);
size_t read(void *buf, size_t nbyte);

View File

@ -6,7 +6,7 @@
#ifdef _WIN32
#define INITGUID // This is to enable using SystemTraceControlGuid in evntrace.h.
#include <Windows.h>
//#include <Windows.h>
//#include <Strsafe.h>
#include <wmistr.h>
#include <evntrace.h>

View File

@ -32,13 +32,14 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include <functional>
#include <sstream>
#include <string.h>
#include <thread>
#include <atomic>
//////////////////////////////////////////////////////////////////////////
#ifdef _WIN32
#include <Windows.h>
//#include <Windows.h>
#else
#include <thread>
#include <sys/types.h>