diff --git a/src/softbus_daemon/src/CoreService.cpp b/src/softbus_daemon/src/CoreService.cpp index c87761d..c6a679d 100644 --- a/src/softbus_daemon/src/CoreService.cpp +++ b/src/softbus_daemon/src/CoreService.cpp @@ -1,6 +1,10 @@ #include +#include #include +#include +#include +#include #include #include #include diff --git a/src/softbus_edge/src/driver/modbus/TcpRtuSession.cpp b/src/softbus_edge/src/driver/modbus/TcpRtuSession.cpp index 674c6f2..799cf1b 100644 --- a/src/softbus_edge/src/driver/modbus/TcpRtuSession.cpp +++ b/src/softbus_edge/src/driver/modbus/TcpRtuSession.cpp @@ -1,17 +1,47 @@ #include +#ifdef SOFTBUS_PLATFORM_WINDOWS +#include +#include +#else #include #include #include #include #include #include +#endif #include #include 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(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(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(&flags), sizeof(flags)); if (::connect(fd, reinterpret_cast(&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(&tv), sizeof(tv)); + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast(&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> TcpRtuSession::transaction(const std::vector frame.push_back(static_cast(crc & 0xFF)); frame.push_back(static_cast((crc >> 8) & 0xFF)); - if (send(socketFd_, frame.data(), frame.size(), 0) != static_cast(frame.size())) { + if (send(socketFd_, reinterpret_cast(frame.data()), + static_cast(frame.size()), 0) + != static_cast(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(buffer), sizeof(buffer), 0); if (n < 5) { return std::nullopt; }