Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rdk 4115/windows port #229

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions 3rdparty/endian/endian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//
// endian.h
//
// https://gist.github.com/panzi/6856583
//
// I, Mathias Panzenböck, place this file hereby into the public domain. Use
// it at your own risk for whatever you like. In case there are
// jurisdictions that don't support putting things in the public domain you
// can also consider it to be "dual licensed" under the BSD, MIT and Apache
// licenses, if you want to. This code is trivial anyway. Consider it an
// example on how to get the endian conversion functions on different
// platforms.

#ifndef PORTABLE_ENDIAN_H__
#define PORTABLE_ENDIAN_H__

// Byte order
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__CYGWIN__)
# include <endian.h>
#elif defined(__APPLE__)
# include <libkern/OSByteOrder.h>

# define htobe16(x) OSSwapHostToBigInt16(x)
# define htole16(x) OSSwapHostToLittleInt16(x)
# define be16toh(x) OSSwapBigToHostInt16(x)
# define le16toh(x) OSSwapLittleToHostInt16(x)

# define htobe32(x) OSSwapHostToBigInt32(x)
# define htole32(x) OSSwapHostToLittleInt32(x)
# define be32toh(x) OSSwapBigToHostInt32(x)
# define le32toh(x) OSSwapLittleToHostInt32(x)

# define htobe64(x) OSSwapHostToBigInt64(x)
# define htole64(x) OSSwapHostToLittleInt64(x)
# define be64toh(x) OSSwapBigToHostInt64(x)
# define le64toh(x) OSSwapLittleToHostInt64(x)

# define __BYTE_ORDER BYTE_ORDER
# define __BIG_ENDIAN BIG_ENDIAN
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __PDP_ENDIAN PDP_ENDIAN
#elif defined(__OpenBSD__)
# include <sys/endian.h>
#elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
# include <sys/endian.h>

# define be16toh(x) betoh16(x)
# define le16toh(x) letoh16(x)

# define be32toh(x) betoh32(x)
# define le32toh(x) letoh32(x)

# define be64toh(x) betoh64(x)
# define le64toh(x) letoh64(x)
#elif defined(_WIN32)
# include <stdlib.h>
# if BYTE_ORDER == LITTLE_ENDIAN
# if defined(_MSC_VER)
# define htobe16(x) _byteswap_ushort(x)
# define htole16(x) (x)
# define be16toh(x) _byteswap_ushort(x)
# define le16toh(x) (x)

# define htobe32(x) _byteswap_ulong(x)
# define htole32(x) (x)
# define be32toh(x) _byteswap_ulong(x)
# define le32toh(x) (x)

# define htobe64(x) _byteswap_uint64(x)
# define htole64(x) (x)
# define be64toh(x) _byteswap_uint64(x)
# define le64toh(x) (x)
# elif defined(__GNUC__) || defined(__clang__)
# define htobe16(x) __builtin_bswap16(x)
# define htole16(x) (x)
# define be16toh(x) __builtin_bswap16(x)
# define le16toh(x) (x)

# define htobe32(x) __builtin_bswap32(x)
# define htole32(x) (x)
# define be32toh(x) __builtin_bswap32(x)
# define le32toh(x) (x)

# define htobe64(x) __builtin_bswap64(x)
# define htole64(x) (x)
# define be64toh(x) __builtin_bswap64(x)
# define le64toh(x) (x)
# else
# error Compiler is not supported
# endif
# else
# error Byte order is not supported
# endif

# define __BYTE_ORDER BYTE_ORDER
# define __BIG_ENDIAN BIG_ENDIAN
# define __LITTLE_ENDIAN LITTLE_ENDIAN
# define __PDP_ENDIAN PDP_ENDIAN
#else
# error Platform is not supported
#endif

#endif // PORTABLE_ENDIAN_H__
28 changes: 21 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@ if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
endif()

option(WITH_ASAN "Compile with address sanitizer support" OFF)
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

if(MSVC)
set(BUILD_SHARED_LIBS OFF)
endif()

##
## Check C++11 support / enable global pedantic and Wall
##
include(DefineCXX17CompilerFlag)
DEFINE_CXX_17_COMPILER_FLAG(CXX17_FLAG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")

add_library(urcl SHARED
add_library(urcl
src/comm/tcp_socket.cpp
src/comm/tcp_server.cpp
src/control/reverse_interface.cpp
Expand Down Expand Up @@ -51,12 +55,22 @@ add_library(urcl SHARED
src/helpers.cpp
)
add_library(ur_client_library::urcl ALIAS urcl)
target_compile_options(urcl PRIVATE -Wall -Wextra -Wno-unused-parameter)
target_compile_options(urcl PUBLIC ${CXX17_FLAG})
if(WITH_ASAN)
target_compile_options(urcl PUBLIC -fsanitize=address)
target_link_options(urcl PUBLIC -fsanitize=address)

if(MSVC)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/endian)
target_link_libraries(urcl ws2_32)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
target_compile_options(urcl PRIVATE -Wall -Wextra -Wno-unused-parameter)

if(WITH_ASAN)
target_compile_options(urcl PUBLIC -fsanitize=address)
target_link_options(urcl PUBLIC -fsanitize=address)
endif()
endif()

target_compile_options(urcl PUBLIC ${CXX17_FLAG})

target_include_directories( urcl PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
Expand Down
31 changes: 17 additions & 14 deletions CMakeModules/DefineCXX17CompilerFlag.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ include(CheckCXXCompilerFlag)
macro (DEFINE_CXX_17_COMPILER_FLAG _RESULT)
if(NOT DEFINED "${_RESULT}")

if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Performing C++17 Test")
endif()

# Check for default argument (all newer compilers)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
if(COMPILER_SUPPORTS_CXX17)
set(${_RESULT} "-std=c++17" CACHE INTERNAL "C++17 flag")
if(MSVC AND MSVC_VERSION GREATER 1919)
set(${_RESULT} "/std:c++17" CACHE INTERNAL "C++17 flag")
else()
# Check for older version (before 2017)
CHECK_CXX_COMPILER_FLAG("-std=c++1z" COMPILER_SUPPORTS_CXX1Z)
if(COMPILER_SUPPORTS_CXX1Z)
set(${_RESULT} "-std=c++1z" CACHE INTERNAL "C++17 flag")
if(NOT CMAKE_REQUIRED_QUIET)
message(STATUS "Performing C++17 Test")
endif()

# Check for default argument (all newer compilers)
CHECK_CXX_COMPILER_FLAG("-std=c++17" COMPILER_SUPPORTS_CXX17)
if(COMPILER_SUPPORTS_CXX17)
set(${_RESULT} "-std=c++17" CACHE INTERNAL "C++17 flag")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
# Check for older version (before 2017)
CHECK_CXX_COMPILER_FLAG("-std=c++1z" COMPILER_SUPPORTS_CXX1Z)
if(COMPILER_SUPPORTS_CXX1Z)
set(${_RESULT} "-std=c++1z" CACHE INTERNAL "C++17 flag")
else()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++17 support. Please use a different C++ compiler.")
endif()
endif()
endif()

endif()
endmacro()
6 changes: 5 additions & 1 deletion examples/dashboard_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@

#include <ur_client_library/log.h>
#include <ur_client_library/ur/dashboard_client.h>
#include <ur_client_library/comm/socket_t.h>

#include <iostream>
#include <memory>
#include <thread>
#include <unistd.h>

using namespace urcl;

Expand Down Expand Up @@ -97,7 +97,11 @@ int main(int argc, char* argv[])
return 1;
}

#ifdef _WIN32
::Sleep(1000);
#else // _WIN32
sleep(1);
#endif // _WIN32

// Play loaded program
if (!my_dashboard->commandPlay())
Expand Down
63 changes: 63 additions & 0 deletions include/ur_client_library/comm/socket_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2024, RoboDK Inc.
*
* 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.
*/

#pragma once

#ifdef _WIN32

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#include <ws2tcpip.h>

#ifndef TCP_QUICKACK
#define TCP_QUICKACK 12
#endif

#ifdef ERROR
#undef ERROR
#endif // ERROR

typedef SOCKET socket_t;
typedef SSIZE_T ssize_t;

static inline int ur_setsockopt(socket_t s, int level, int optname, const void* optval, unsigned int optlen)
{
return ::setsockopt(s, level, optname, reinterpret_cast<const char*>(optval), static_cast<int>(optlen));
}

static inline int ur_close(socket_t s)
{
return ::closesocket(s);
}

#else // _WIN32

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

typedef int socket_t;

#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif

#define ur_setsockopt setsockopt
#define ur_close close

#endif // _WIN32
3 changes: 0 additions & 3 deletions include/ur_client_library/comm/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
*/

#pragma once
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <atomic>
#include <chrono>
#include <mutex>
Expand Down
22 changes: 9 additions & 13 deletions include/ur_client_library/comm/tcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
#ifndef UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED
#define UR_CLIENT_LIBRARY_TCP_SERVER_H_INCLUDED

#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <functional>
#include <thread>

#include "ur_client_library/comm/socket_t.h"


namespace urcl
{
namespace comm
Expand Down Expand Up @@ -178,25 +177,22 @@ class TCPServer
std::atomic<bool> keep_running_;
std::thread worker_thread_;

std::atomic<int> listen_fd_;
std::atomic<socket_t> listen_fd_;
int port_;

int maxfd_;
socket_t maxfd_;
fd_set masterfds_;
fd_set tempfds_;

uint32_t max_clients_allowed_;
std::vector<int> client_fds_;

// Pipe for the self-pipe trick (https://cr.yp.to/docs/selfpipe.html)
int self_pipe_[2];
std::vector<socket_t> client_fds_;

static const int INPUT_BUFFER_SIZE = 100;
char input_buffer_[INPUT_BUFFER_SIZE];

std::function<void(const int)> new_connection_callback_;
std::function<void(const int)> disconnect_callback_;
std::function<void(const int, char* buffer, int nbytesrecv)> message_callback_;
std::function<void(const socket_t)> new_connection_callback_;
std::function<void(const socket_t)> disconnect_callback_;
std::function<void(const socket_t, char* buffer, int nbytesrecv)> message_callback_;
};

} // namespace comm
Expand Down
8 changes: 4 additions & 4 deletions include/ur_client_library/comm/tcp_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*/

#pragma once
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <atomic>
#include <chrono>
#include <mutex>
#include <string>
#include <memory>

#include "ur_client_library/comm/socket_t.h"


namespace urcl
{
namespace comm
Expand All @@ -49,7 +49,7 @@ enum class SocketState
class TCPSocket
{
private:
std::atomic<int> socket_fd_;
std::atomic<socket_t> socket_fd_;
std::atomic<SocketState> state_;
std::chrono::milliseconds reconnection_time_;
bool reconnection_time_modified_deprecated_ = false;
Expand Down
9 changes: 9 additions & 0 deletions include/ur_client_library/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
#include <sstream>
#include "ur/version_information.h"

#ifdef _WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <WinSock2.h>
#ifdef ERROR
#undef ERROR
#endif // ERROR
#endif

namespace urcl
{
/*!
Expand Down
Loading
Loading