softbus windows
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#include <softbus_daemon/core/CoreService.h>
|
||||
|
||||
#include <softbus_bus/Bus.h>
|
||||
#include <softbus_command/CommandContext.h>
|
||||
#include <softbus_command/CommandService.h>
|
||||
#include <softbus_pipeline/PipelineEngine.h>
|
||||
#include <softbus_transport/TcpUplinkServer.h>
|
||||
#include <softbus_opcua/UaAddressSpaceBuilder.h>
|
||||
#include <softbus_opcua/UaEventBridge.h>
|
||||
#include <softbus_opcua/UaMethodBinder.h>
|
||||
|
||||
@@ -1,17 +1,47 @@
|
||||
#include <softbus_edge/driver/modbus/TcpRtuSession.h>
|
||||
|
||||
#ifdef SOFTBUS_PLATFORM_WINDOWS
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace softbus::edge {
|
||||
|
||||
namespace {
|
||||
|
||||
#ifdef SOFTBUS_PLATFORM_WINDOWS
|
||||
struct WinsockInit {
|
||||
WinsockInit()
|
||||
{
|
||||
WSADATA wsaData{};
|
||||
WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
}
|
||||
~WinsockInit() { WSACleanup(); }
|
||||
};
|
||||
|
||||
int closeSocket(int fd)
|
||||
{
|
||||
return closesocket(static_cast<SOCKET>(fd));
|
||||
}
|
||||
#else
|
||||
int closeSocket(int fd)
|
||||
{
|
||||
return close(fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
uint16_t TcpRtuSession::crc16(const uint8_t* data, std::size_t len)
|
||||
{
|
||||
uint16_t crc = 0xFFFF;
|
||||
@@ -30,8 +60,11 @@ uint16_t TcpRtuSession::crc16(const uint8_t* data, std::size_t len)
|
||||
|
||||
bool TcpRtuSession::connect(const std::string& host, uint16_t port, int timeoutMs)
|
||||
{
|
||||
#ifdef SOFTBUS_PLATFORM_WINDOWS
|
||||
static WinsockInit winsockInit;
|
||||
#endif
|
||||
disconnect();
|
||||
const int fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
const int fd = static_cast<int>(socket(AF_INET, SOCK_STREAM, 0));
|
||||
if (fd < 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -40,23 +73,23 @@ bool TcpRtuSession::connect(const std::string& host, uint16_t port, int timeoutM
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, host.c_str(), &addr.sin_addr) != 1) {
|
||||
close(fd);
|
||||
closeSocket(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
const int flags = 1;
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flags, sizeof(flags));
|
||||
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&flags), sizeof(flags));
|
||||
|
||||
if (::connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) != 0) {
|
||||
close(fd);
|
||||
closeSocket(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
timeval tv {};
|
||||
tv.tv_sec = timeoutMs / 1000;
|
||||
tv.tv_usec = (timeoutMs % 1000) * 1000;
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&tv), sizeof(tv));
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&tv), sizeof(tv));
|
||||
|
||||
socketFd_ = fd;
|
||||
return true;
|
||||
@@ -65,7 +98,7 @@ bool TcpRtuSession::connect(const std::string& host, uint16_t port, int timeoutM
|
||||
void TcpRtuSession::disconnect()
|
||||
{
|
||||
if (socketFd_ >= 0) {
|
||||
close(socketFd_);
|
||||
closeSocket(socketFd_);
|
||||
socketFd_ = -1;
|
||||
}
|
||||
}
|
||||
@@ -81,12 +114,14 @@ std::optional<std::vector<uint8_t>> TcpRtuSession::transaction(const std::vector
|
||||
frame.push_back(static_cast<uint8_t>(crc & 0xFF));
|
||||
frame.push_back(static_cast<uint8_t>((crc >> 8) & 0xFF));
|
||||
|
||||
if (send(socketFd_, frame.data(), frame.size(), 0) != static_cast<ssize_t>(frame.size())) {
|
||||
if (send(socketFd_, reinterpret_cast<const char*>(frame.data()),
|
||||
static_cast<int>(frame.size()), 0)
|
||||
!= static_cast<int>(frame.size())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
uint8_t buffer[256]{};
|
||||
const ssize_t n = recv(socketFd_, buffer, sizeof(buffer), 0);
|
||||
const int n = recv(socketFd_, reinterpret_cast<char*>(buffer), sizeof(buffer), 0);
|
||||
if (n < 5) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user