From 3cd2bb514927b906fd4949334945060541509cbc Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Mon, 23 Dec 2024 20:12:03 +0900 Subject: [PATCH 1/2] Remove bluetooth feature from main branch since it is no where near ready and available. --- .gitignore | 5 - CMakeLists.txt | 3 - Environment-Test-Dockerfile | 5 +- README.md | 9 +- src/enums/SocketType.h | 11 - src/serversocket/ServerSocket.cpp | 145 +----------- src/serversocket/ServerSocket.h | 11 +- src/socket/BluetoothSocket.cpp | 251 --------------------- src/socket/BluetoothSocket.h | 91 -------- src/socket/Socket.cpp | 5 - src/socket/TCPSocket.h | 4 - src/socket/UDPSocket.h | 4 - tests/CMakeLists.txt | 3 +- tests/serversocket/ServerSocketTCPTest.cpp | 10 +- tests/socket/BluetoothSocketTest.cpp | 59 ----- tests/socket/ScenarioTest.cpp | 4 +- tests/socket/TCPSocketTest.cpp | 4 +- 17 files changed, 19 insertions(+), 605 deletions(-) delete mode 100644 src/enums/SocketType.h delete mode 100644 src/socket/BluetoothSocket.cpp delete mode 100644 src/socket/BluetoothSocket.h delete mode 100644 tests/socket/BluetoothSocketTest.cpp diff --git a/.gitignore b/.gitignore index f1b2f28..c39815c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,3 @@ -#Test File: -TestTCP -TestUDP -TestBluetooth - # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt index b0fbbf2..5353459 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,14 +11,12 @@ set(HEADERS src/socket/Socket.h src/socket/TCPSocket.h src/socket/UDPSocket.h - src/socket/BluetoothSocket.h src/address/SocketAddress.h src/socketexceptions/BindingException.hpp src/socketexceptions/SocketException.hpp src/socketexceptions/TimeoutException.hpp src/socketexceptions/SocketError.h - src/enums/SocketType.h src/enums/InternetProtocolVersion.h ) @@ -27,7 +25,6 @@ set(SOURCE src/socket/Socket.cpp src/socket/TCPSocket.cpp src/socket/UDPSocket.cpp - src/socket/BluetoothSocket.cpp src/socketexceptions/SocketError.cpp src/address/SocketAddress.cpp ) diff --git a/Environment-Test-Dockerfile b/Environment-Test-Dockerfile index ac73fa9..44508d4 100644 --- a/Environment-Test-Dockerfile +++ b/Environment-Test-Dockerfile @@ -4,7 +4,8 @@ FROM alpine:3.20.0 AS alpine WORKDIR /alpine -RUN apk update && apk upgrade && apk add g++ cmake make git bluez-dev glib-dev bluez +# bluez-dev bluez +RUN apk update && apk upgrade && apk add g++ cmake make git glib-dev COPY ./src ./src COPY ./tests ./tests @@ -22,7 +23,7 @@ FROM ubuntu:24.10 AS ubuntu WORKDIR /ubuntu -RUN apt update && apt upgrade -y && apt install g++ make cmake git libbluetooth-dev libglib2.0-dev bluez -y +RUN apt update && apt upgrade -y && apt install g++ make cmake git libglib2.0-dev -y COPY ./src ./src COPY ./tests ./tests diff --git a/README.md b/README.md index 463d5b2..25e0433 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Cpp-SocketLibrary -A ServerSocket and Socket library for Windows and Linux aiming to support both Wifi and Bluetooth communication. +A ServerSocket and Socket library for Windows and Linux that supports Wifi communication. ## Getting Started @@ -8,13 +8,10 @@ A ServerSocket and Socket library for Windows and Linux aiming to support both W - [CMake](https://cmake.org/download/) and `make` -The following **linux** dependencies are required: -- `libbluetooth-dev` -- `libglib2.0-dev` -- `bluez` - ### Building the Library and Running the Tests - Linux +- Make sure `libglib2.0-dev` is installed + 1. To build the library, firstly run cmake: `cmake . -B build-linux` in the root directory of the repository (`CppSocketLibrary/`). 2. Then move into the new `build-linux` folder: `cd build-linux`. 3. Then you can run `make` to build the library. diff --git a/src/enums/SocketType.h b/src/enums/SocketType.h deleted file mode 100644 index c4af493..0000000 --- a/src/enums/SocketType.h +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -namespace kt -{ - enum class SocketType - { - None, - Bluetooth, - Wifi - }; -} diff --git a/src/serversocket/ServerSocket.cpp b/src/serversocket/ServerSocket.cpp index 3d0dd81..ad54efb 100644 --- a/src/serversocket/ServerSocket.cpp +++ b/src/serversocket/ServerSocket.cpp @@ -4,7 +4,6 @@ #include "../socketexceptions/SocketException.hpp" #include "../socketexceptions/BindingException.hpp" #include "../socketexceptions/TimeoutException.hpp" -#include "../enums/SocketType.h" #include "../socketexceptions/SocketError.h" #include "../address/SocketAddress.h" @@ -34,8 +33,6 @@ #include #include #include -#include -#include #include #endif @@ -43,27 +40,20 @@ namespace kt { /** - * ServerSocket constructor. Creates a wifi/bluetooth ServerSocket and begins listening for connections. + * ServerSocket constructor. Creates a wifi ServerSocket and begins listening for connections. * - * @param type - Determines whether this ServerSocket is a wifi or bluetooth ServerSocket. * @param port - The port number for this server to communicate through. If value is not passed in a random, available port number will be assigned. * @param connectionBacklogSize - You can enter a value here to specify the length of the server connection pool. The default value is 20. * * @throw SocketException - If the ServerSocket is unable to be instanciated or begin listening. * @throw BindingException - If the ServerSocket is unable to bind to the specific port specified. */ - kt::ServerSocket::ServerSocket(const kt::SocketType type, const std::optional& localHostname, const unsigned short& port, const unsigned int& connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion) + kt::ServerSocket::ServerSocket(const std::optional& localHostname, const unsigned short& port, const unsigned int& connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion) { this->socketDescriptor = getInvalidSocketValue(); this->port = port; - this->type = type; this->protocolVersion = protocolVersion; - if (this->type == kt::SocketType::None) - { - throw SocketException("Failed to create ServerSocket with 'None' SocketType."); - } - this->constructSocket(localHostname, connectionBacklogSize); } @@ -75,7 +65,6 @@ namespace kt kt::ServerSocket::ServerSocket(const kt::ServerSocket& socket) { this->port = socket.port; - this->type = socket.type; this->protocolVersion = socket.protocolVersion; this->socketDescriptor = socket.socketDescriptor; this->serverAddress = socket.serverAddress; @@ -91,7 +80,6 @@ namespace kt kt::ServerSocket& kt::ServerSocket::operator=(const kt::ServerSocket& socket) { this->port = socket.port; - this->type = socket.type; this->protocolVersion = socket.protocolVersion; this->socketDescriptor = socket.socketDescriptor; this->serverAddress = socket.serverAddress; @@ -101,73 +89,7 @@ namespace kt void kt::ServerSocket::constructSocket(const std::optional& localHostname, const unsigned int& connectionBacklogSize) { - if (this->type == kt::SocketType::Wifi) - { - this->constructWifiSocket(localHostname, connectionBacklogSize); - } - else if (this->type == kt::SocketType::Bluetooth) - { - this->constructBluetoothSocket(connectionBacklogSize); - this->setDiscoverable(); - } - } - - void kt::ServerSocket::constructBluetoothSocket(const unsigned int& connectionBacklogSize) - { -#ifdef _WIN32 - throw kt::SocketException("ServerSocket::constructBluetoothSocket(unsigned int) is not supported on Windows."); - - /*SOCKADDR_BTH bluetoothAddress; - - this->socketDescriptor = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); - - if (this->socketDescriptor == 0) - { - throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno))); - } - - this->bluetoothAddress.addressFamily = AF_BTH; - this->bluetoothAddress.btAddr = 0; - this->bluetoothAddress.port = this->port; - - if (bind(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(SOCKADDR_BTH)) == -1) - { - throw BindingException("Error binding BT connection, the port " + std::to_string(this->port) + " is already being used: " + std::string(std::strerror(errno)) + ". WSA Error: " + std::to_string(WSAGetLastError())); - } - - if (listen(this->socketDescriptor, static_cast(connectionBacklogSize)) == -1) - { - this->close(); - throw SocketException("Error Listening on port: " + std::to_string(this->port) + ": " + std::string(std::strerror(errno))); - }*/ - -#elif __linux__ - sockaddr_rc localAddress = {0}; - this->socketDescriptor = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - - if (isInvalidSocket(this->socketDescriptor)) - { - throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno))); - } - - localAddress.rc_family = AF_BLUETOOTH; - localAddress.rc_bdaddr = ((bdaddr_t) {{0, 0, 0, 0, 0, 0}}); - localAddress.rc_channel = static_cast(this->port); - - if (bind(this->socketDescriptor, (sockaddr *)&localAddress, sizeof(localAddress)) == -1) - { - throw BindingException("Error binding BT connection, the port " + std::to_string(this->port) + " is already being used: " + std::string(std::strerror(errno))); - } - - if (listen(this->socketDescriptor, connectionBacklogSize) == -1) - { - this->close(); - throw SocketException("Error Listening on port " + std::to_string(this->port) + ": " + std::string(std::strerror(errno))); - } -#endif - - // Make discoverable - + this->constructWifiSocket(localHostname, connectionBacklogSize); } void kt::ServerSocket::constructWifiSocket(const std::optional& localHostname, const unsigned int& connectionBacklogSize) @@ -248,32 +170,6 @@ namespace kt this->port = kt::getPortNumber(address.first.value()); } - - void kt::ServerSocket::setDiscoverable() - { - throw kt::SocketException("ServerSocket::setDiscoverable() not implemented."); - -#if __linux__ - hci_dev_req req; - req.dev_id = 0; - // req.dev_id = hci_get_route(nullptr); - req.dev_opt = SCAN_PAGE | SCAN_INQUIRY; - - if (ioctl(this->socketDescriptor, HCISETSCAN, (unsigned long)&req) < 0) - { - throw SocketException("Failed to make device discoverable."); - } -#endif - } - - /** - * @return the *kt::SocketType* for this *kt::ServerSocket*. - */ - kt::SocketType kt::ServerSocket::getType() const - { - return this->type; - } - /** * Used to get the port number that the ServerSocket is listening on. * @return An unsigned int of the port number that the ServerSocket is listening on. @@ -334,41 +230,6 @@ namespace kt return kt::TCPSocket(temp, hostname.value(), portNum, this->getInternetProtocolVersion(), acceptedAddress); } - kt::BluetoothSocket kt::ServerSocket::acceptBluetoothConnection(const long& timeout) - { - if (timeout > 0) - { - int res = kt::pollSocket(this->socketDescriptor, timeout); - if (res == -1) - { - throw kt::SocketException("Failed to poll as socket is no longer valid."); - } - else if (res == 0) - { - throw kt::TimeoutException("No applicable connections could be accepted during the time period specified " + std::to_string(timeout) + " microseconds."); - } - } - - throw kt::SocketException("acceptBluetoothConnection() - Not yet implemented."); -#ifdef __linux__ - // Remove bluetooth related code - - // sockaddr_rc remoteDevice = { 0 }; - // socklen_t socketSize = sizeof(remoteDevice); - // SOCKET temp = ::accept(this->socketDescriptor, (sockaddr *) &remoteDevice, &socketSize); - // if (temp == -1) - // { - // throw SocketException("Failed to accept connection. Socket is in an invalid state."); - // } - - // if (this->type == kt::SocketType::Bluetooth) - // { - // char remoteAddress[1024] = {0}; - // ba2str(&remoteDevice.rc_bdaddr, remoteAddress); - // } -#endif - } - /** * Closes the existing connection. If no connection is open, then it will do nothing. * diff --git a/src/serversocket/ServerSocket.h b/src/serversocket/ServerSocket.h index c1e6f8c..98deb82 100644 --- a/src/serversocket/ServerSocket.h +++ b/src/serversocket/ServerSocket.h @@ -3,11 +3,7 @@ #include #include "../address/SocketAddress.h" - -#include "../socket/BluetoothSocket.h" #include "../socket/TCPSocket.h" - -#include "../enums/SocketType.h" #include "../enums/InternetProtocolVersion.h" #ifdef _WIN32 @@ -42,26 +38,21 @@ namespace kt { protected: unsigned short port = 0; - kt::SocketType type = kt::SocketType::None; kt::InternetProtocolVersion protocolVersion = kt::InternetProtocolVersion::Any; kt::SocketAddress serverAddress = {}; SOCKET socketDescriptor = getInvalidSocketValue(); - void setDiscoverable(); void constructSocket(const std::optional&, const unsigned int&); - void constructBluetoothSocket(const unsigned int&); void constructWifiSocket(const std::optional& localHostname, const unsigned int&); void initialisePortNumber(); public: - ServerSocket(const kt::SocketType, const std::optional& = std::nullopt, const unsigned short& = 0, const unsigned int& = 20, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any); + ServerSocket(const std::optional& = std::nullopt, const unsigned short& = 0, const unsigned int& = 20, const kt::InternetProtocolVersion = kt::InternetProtocolVersion::Any); ServerSocket(const kt::ServerSocket&); kt::ServerSocket& operator=(const kt::ServerSocket&); kt::TCPSocket acceptTCPConnection(const long& = 0) const; - kt::BluetoothSocket acceptBluetoothConnection(const long& = 0); - kt::SocketType getType() const; kt::InternetProtocolVersion getInternetProtocolVersion() const; unsigned short getPort() const; SOCKET getSocket() const; diff --git a/src/socket/BluetoothSocket.cpp b/src/socket/BluetoothSocket.cpp deleted file mode 100644 index df93723..0000000 --- a/src/socket/BluetoothSocket.cpp +++ /dev/null @@ -1,251 +0,0 @@ -#include "BluetoothSocket.h" -#include "../socketexceptions/SocketException.hpp" - -namespace kt -{ - void BluetoothSocket::constructBluetoothSocket() - { - throw kt::SocketException("Socket:constructBluetoothSocket() is not supported."); -#ifdef _WIN32 - - /*this->socketDescriptor = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); - if (isInvalidSocket(this->socketDescriptor)) - { - throw SocketException("Error establishing Bluetooth socket: " + std::string(std::strerror(errno))); - } - - this->bluetoothAddress.addressFamily = AF_BTH; - this->bluetoothAddress.btAddr = std::stoull(this->hostname); - this->bluetoothAddress.port = this->port; - - if (connect(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(SOCKADDR_BTH)) == -1) - { - throw SocketException("Error connecting to Bluetooth server: " + std::string(std::strerror(errno))); - }*/ - -#elif __linux__ - /*this->socketDescriptor = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - - if (isInvalidSocket(this->socketDescriptor)) - { - throw SocketException("Error establishing Bluetooth socket: " + std::string(std::strerror(errno))); - } - - this->bluetoothAddress.rc_family = AF_BLUETOOTH; - this->bluetoothAddress.rc_channel = (uint8_t)port; - str2ba(this->hostname.c_str(), &this->bluetoothAddress.rc_bdaddr); - - if (connect(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(this->bluetoothAddress)) == -1) - { - throw SocketException("Error connecting to Bluetooth server: " + std::string(std::strerror(errno))); - }*/ -#endif - } - - int BluetoothSocket::pollSocket(SOCKET socket, const long& timeout) const - { - return kt::pollSocket(socket, timeout); - } - - void BluetoothSocket::close(SOCKET socket) - { - kt::close(socket); - } - - BluetoothSocket::BluetoothSocket(const std::string& hostname, const unsigned int& port) - { - this->hostname = hostname; - this->port = port; - } - - void BluetoothSocket::close() - { - this->close(this->socketDescriptor); - } - - bool BluetoothSocket::send(const std::string&, int) - { - return false; - } - - unsigned int BluetoothSocket::getPort() const - { - return this->port; - } - - std::string BluetoothSocket::getHostname() const - { - return this->hostname; - } - - std::optional BluetoothSocket::get(const int& flags) const - { - std::string received = this->receiveAmount(1, flags); - if (received.empty()) - { - return std::nullopt; - } - return received[0]; - } - - std::string BluetoothSocket::receiveAmount(const unsigned int& amount, const int& flags) const - { - return std::string(); - } - - /** - * **In progress** - * - * Scans for bluetooth devices and returns a std::vector<std::pair<std::string, std::string>> of the device names and addresses. - * - * @param duration - The duration for which the scan should take to discover nearby bluetooth devices. - * - * @return A std::vector<std::pair<std::string, std::string>> where .first is the devices address, and .second is the device name. - */ - std::vector > BluetoothSocket::scanDevices(unsigned int duration) - { - throw kt::SocketException("Socket::scanDevices(int) is not supported."); - -#ifdef _WIN32 - - /*WSADATA wsaData; - int res = WSAStartup(MAKEWORD(2, 2), &wsaData); - if (res != 0) - { - throw SocketException("WSAStartup Failed: " + std::to_string(res)); - }*/ - - /*WSAQUERYSET wsaQuery; - HANDLE hLoopUp; - LPWSAQUERYSET pQuerySet = nullptr; - SOCKADDR_BTH tempAddress; - DWORD dwSize = 5000 * sizeof(unsigned char); - memset(&wsaQuery, 0, sizeof(WSAQUERYSET)); - wsaQuery.dwSize = sizeof(WSAQUERYSET); - wsaQuery.dwNameSpace = NS_BTH; - wsaQuery.lpcsaBuffer = nullptr; - - int res = WSALookupServiceBegin(&wsaQuery, LUP_CONTAINERS, &hLoopUp); - if (res == -1) - { - throw SocketException("Unable to search for devices. Could not begin search."); - } - - memset(&pQuerySet, 0, sizeof(WSAQUERYSET)); - pQuerySet->dwSize = sizeof(WSAQUERYSET); - pQuerySet->dwNameSpace = NS_BTH; - pQuerySet->lpBlob = nullptr; - - while (WSALookupServiceNext(hLoopUp, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pQuerySet) == 0) - { - tempAddress = ((SOCKADDR_BTH*) pQuerySet->lpcsaBuffer->RemoteAddr.lpSockaddr)->btAddr; - // std::cout << pQuerySet->lpszServiceInstanceName << " : " << GET_NAP(tempAddress) << " - " << GET_SAP(tempAddress) << " ~ " << pQuerySet->dwNameSpace << std::endl; - }*/ - -#elif __linux__ - - /*std::vector > devices; - std::pair tempPair; - - inquiry_info* ii = nullptr; - int maxResponse = 255, numberOfResponses, ownId, tempSocket, flags; - char deviceAddress[19]; - char deviceName[248]; - - ownId = hci_get_route(nullptr); - tempSocket = hci_open_dev(ownId); - if (ownId < 0 || tempSocket < 0) - { - throw SocketException("Error opening Bluetooth socket for scanning..."); - } - - flags = IREQ_CACHE_FLUSH; - ii = new inquiry_info[maxResponse * sizeof(inquiry_info)]; - - numberOfResponses = hci_inquiry(ownId, duration, maxResponse, nullptr, &ii, flags); - if (numberOfResponses < 0) - { - delete[]ii; - throw SocketException("Error scanning for bluetooth devices"); - } - - for (int i = 0; i < numberOfResponses; i++) - { - ba2str(&(ii + i)->bdaddr, deviceAddress); - memset(&deviceName, '\0', sizeof(deviceName)); - if (hci_read_remote_name(tempSocket, &(ii + i)->bdaddr, sizeof(deviceName), deviceName, 0) < 0) - { - strcpy(deviceName, "[unknown]"); - } - - tempPair = std::make_pair(deviceAddress, deviceName); - devices.push_back(tempPair); - - } - - delete[]ii; - ::close(tempSocket); - - return devices;*/ -#endif - } - - std::optional kt::BluetoothSocket::getLocalMACAddress() - { - throw kt::SocketException("Socket::getLocalMACAddress() is not supported."); - -#ifdef _WIN32 - - // Up to 20 Interfaces - /*IP_ADAPTER_INFO AdapterInfo[20]; - DWORD dwBufLen = sizeof(AdapterInfo); - DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen); - PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; - - while (std::string(pAdapterInfo->Description).find("Bluetooth") == std::string::npos) - { - pAdapterInfo = pAdapterInfo->Next; - } - - std::stringstream ss; - for (int i = 0; i < 6; i++) - { - ss << std::hex << std::setfill('0'); - ss << std::setw(2) << static_cast(pAdapterInfo->Address[i]); - - if (i != 5) - { - ss << ":"; - } - } - - return ss.str();*/ - -#elif __linux__ - // int id; - // bdaddr_t btaddr; - // char localMACAddress[18]; - - // // Get id of local device - // if ((id = hci_get_route(nullptr)) < 0) - // { - // return std::nullopt; - // } - - // // Get local bluetooth address - // if (hci_dev_req(id, &btaddr) < 0) - // { - // return std::nullopt; - // } - - // // Convert address to string - // if (ba2str(&btaddr, localMACAddress) < 0) - // { - // return std::nullopt; - // } - - // return std::optional{std::string(localMACAddress)}; -#endif - } -} - diff --git a/src/socket/BluetoothSocket.h b/src/socket/BluetoothSocket.h deleted file mode 100644 index e89f9aa..0000000 --- a/src/socket/BluetoothSocket.h +++ /dev/null @@ -1,91 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#include "../enums/SocketType.h" -#include "../enums/InternetProtocolVersion.h" -#include "../address/SocketAddress.h" -#include "../socketexceptions/SocketError.h" - -#include "Socket.h" - -#ifdef _WIN32 - -#ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN -#endif - -#ifndef _WIN32_WINNT - #define _WIN32_WINNT 0x0600 -#endif - -#include -#include -#include - -#elif __linux__ - -#include -#include -#include -#include -#include -#include - -// Typedef to match the windows typedef since they are different underlying types -typedef int SOCKET; - -#endif - -namespace kt -{ - class BluetoothSocket - { - protected: - std::string hostname; - unsigned int port; - SOCKET socketDescriptor; - -#ifdef _WIN32 - //SOCKADDR_BTH bluetoothAddress; - -#elif __linux__ - sockaddr_rc bluetoothAddress; // For Bluetooth - -#endif - - void constructBluetoothSocket(); - int pollSocket(SOCKET socket, const long& = 1000) const; - - void close(SOCKET socket); - - public: - BluetoothSocket() = default; - BluetoothSocket(const std::string&, const unsigned int&); - //BluetoothSocket(const SOCKET&, const kt::SocketType, const kt::SocketProtocol, const std::string&, const unsigned int&, const kt::InternetProtocolVersion); - - //BluetoothSocket(const kt::BluetoothSocket&); - //kt::BluetoothSocket& operator=(const kt::BluetoothSocket&); - - void close(); - - //bool ready(const unsigned long = 1000) const; - //bool connected(const unsigned long = 1000) const; - bool send(const std::string&, int = 0); - - unsigned int getPort() const; - std::string getHostname() const; - - std::optional get(const int&) const; - std::string receiveAmount(const unsigned int&, const int& = 0) const; - //std::string receiveToDelimiter(const char&, unsigned int = 0); - //std::string receiveAll(const unsigned long = 1000); - - static std::vector > scanDevices(unsigned int = 5); - static std::optional getLocalMACAddress(); - }; - -} // End namespace kt diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp index 8cfe263..8e6094f 100644 --- a/src/socket/Socket.cpp +++ b/src/socket/Socket.cpp @@ -2,7 +2,6 @@ #include "Socket.h" #include "../socketexceptions/SocketException.hpp" #include "../socketexceptions/BindingException.hpp" -#include "../enums/SocketType.h" #include "../socketexceptions/SocketError.h" #include @@ -30,10 +29,6 @@ #include #include #include -#include -#include -#include -#include #include #include #include diff --git a/src/socket/TCPSocket.h b/src/socket/TCPSocket.h index f162ca6..53916d2 100644 --- a/src/socket/TCPSocket.h +++ b/src/socket/TCPSocket.h @@ -5,7 +5,6 @@ #include #include -#include "../enums/SocketType.h" #include "../enums/InternetProtocolVersion.h" #include "../address/SocketAddress.h" #include "../socketexceptions/SocketError.h" @@ -31,9 +30,6 @@ #include #include #include -#include -#include -#include // Typedef to match the windows typedef since they are different underlying types typedef int SOCKET; diff --git a/src/socket/UDPSocket.h b/src/socket/UDPSocket.h index 0c4f63c..f29b3bc 100644 --- a/src/socket/UDPSocket.h +++ b/src/socket/UDPSocket.h @@ -6,7 +6,6 @@ #include #include -#include "../enums/SocketType.h" #include "../enums/InternetProtocolVersion.h" #include "../address/SocketAddress.h" #include "../socketexceptions/SocketError.h" @@ -32,9 +31,6 @@ #include #include #include -#include -#include -#include // Typedef to match the windows typedef since they are different underlying types typedef int SOCKET; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 44b61ae..839937a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -22,7 +22,6 @@ set(SOURCE serversocket/ServerSocketTCPTest.cpp socket/TCPSocketTest.cpp socket/UDPSocketTest.cpp - socket/BluetoothSocketTest.cpp address/SocketAddressTest.cpp @@ -43,7 +42,7 @@ if(CMAKE_HOST_UNIX) target_link_libraries(${PROJECT_NAME} PUBLIC gtest_main gtest - bluetooth + # bluetooth CppSocketLibrary # Parent project ) endif() diff --git a/tests/serversocket/ServerSocketTCPTest.cpp b/tests/serversocket/ServerSocketTCPTest.cpp index 99cf449..2a483fd 100644 --- a/tests/serversocket/ServerSocketTCPTest.cpp +++ b/tests/serversocket/ServerSocketTCPTest.cpp @@ -14,7 +14,7 @@ namespace kt protected: ServerSocket serverSocket; protected: - ServerSocketTCPTest() : serverSocket(SocketType::Wifi) {} + ServerSocketTCPTest() : serverSocket() {} // void SetUp() override { } void TearDown() override { @@ -27,7 +27,6 @@ namespace kt */ TEST_F(ServerSocketTCPTest, TestDefaultConstructor) { - ASSERT_EQ(SocketType::Wifi, serverSocket.getType()); ASSERT_NE(InternetProtocolVersion::Any, serverSocket.getInternetProtocolVersion()); ASSERT_NE(0, serverSocket.getPort()); ASSERT_FALSE(kt::isInvalidSocket(serverSocket.getSocket())); @@ -39,7 +38,7 @@ namespace kt TEST_F(ServerSocketTCPTest, TestConstructors) { EXPECT_THROW({ - ServerSocket server2(SocketType::Wifi, std::nullopt, serverSocket.getPort()); + ServerSocket server2(std::nullopt, serverSocket.getPort()); }, BindingException); } @@ -62,7 +61,6 @@ namespace kt ASSERT_EQ(serverSocket.getInternetProtocolVersion(), server2.getInternetProtocolVersion()); ASSERT_EQ(serverSocket.getPort(), server2.getPort()); ASSERT_EQ(serverSocket.getSocket(), server2.getSocket()); - ASSERT_EQ(serverSocket.getType(), server2.getType()); TCPSocket client(kt::getLocalAddress(serverSocket.getInternetProtocolVersion()), serverSocket.getPort()); @@ -83,7 +81,7 @@ namespace kt */ TEST_F(ServerSocketTCPTest, TestServerSocketAsIPV6) { - ServerSocket ipv6Server(SocketType::Wifi, std::nullopt, 0, 20, InternetProtocolVersion::IPV6); + ServerSocket ipv6Server(std::nullopt, 0, 20, InternetProtocolVersion::IPV6); TCPSocket client("0000:0000:0000:0000:0000:0000:0000:0001", ipv6Server.getPort()); TCPSocket serverClient = ipv6Server.acceptTCPConnection(); @@ -105,7 +103,7 @@ namespace kt { serverSocket.close(); - ServerSocket ipv4Server(SocketType::Wifi, std::nullopt, 0, 20, InternetProtocolVersion::IPV4); + ServerSocket ipv4Server(std::nullopt, 0, 20, InternetProtocolVersion::IPV4); ASSERT_EQ(InternetProtocolVersion::IPV4, ipv4Server.getInternetProtocolVersion()); // Attempt to connect to a local server using a IPV6 address (which is not being hosted) diff --git a/tests/socket/BluetoothSocketTest.cpp b/tests/socket/BluetoothSocketTest.cpp deleted file mode 100644 index 07f304b..0000000 --- a/tests/socket/BluetoothSocketTest.cpp +++ /dev/null @@ -1,59 +0,0 @@ - -#include -#include - -#include - -#include "../../src/socket/BluetoothSocket.h" -#include "../../src/serversocket/ServerSocket.h" - -namespace kt -{ - class DISABLED_BluetoothSocketTest : public ::testing::Test - { - protected: - ServerSocket serverSocket; - BluetoothSocket socket; - - protected: - DISABLED_BluetoothSocketTest() : serverSocket(SocketType::Bluetooth), socket(BluetoothSocket::getLocalMACAddress().value(), serverSocket.getPort()) { } - // void TearDown() override - // { - // socket.close(); - // serverSocket.close(); - // } - }; - - TEST_F(DISABLED_BluetoothSocketTest, BluetoothGetLocalMacAddress) - { - GTEST_SKIP(); - - ASSERT_NE(std::nullopt, BluetoothSocket::getLocalMACAddress()); - } - - TEST_F(DISABLED_BluetoothSocketTest, BluetoothScanDevices) - { - GTEST_SKIP(); - - std::vector > devices = BluetoothSocket::scanDevices(); - for (const std::pair& p : devices) - { - std::cout << p.first << " - " << p.second << std::endl; - } - } - - TEST_F(DISABLED_BluetoothSocketTest, BluetoothSend) - { - GTEST_SKIP(); - - BluetoothSocket server = serverSocket.acceptBluetoothConnection(); - const std::string toSend = "TestBluetooth"; - - ASSERT_TRUE(socket.send(toSend)); - - std::string response = server.receiveAmount(toSend.size()); - ASSERT_EQ(response, toSend); - - server.close(); - } -} \ No newline at end of file diff --git a/tests/socket/ScenarioTest.cpp b/tests/socket/ScenarioTest.cpp index cc93c34..f0dda3a 100644 --- a/tests/socket/ScenarioTest.cpp +++ b/tests/socket/ScenarioTest.cpp @@ -18,7 +18,7 @@ namespace kt ASSERT_TRUE(bindResult.first); ASSERT_NE(std::nullopt, socket.getListeningPort()); - kt::ServerSocket server(SocketType::Wifi, std::nullopt, socket.getListeningPort().value()); + kt::ServerSocket server(std::nullopt, socket.getListeningPort().value()); ASSERT_EQ(server.getPort(), socket.getListeningPort().value()); @@ -33,7 +33,7 @@ namespace kt */ TEST(ScenarioTest, TCPThenUDPBindSamePort) { - kt::ServerSocket server(SocketType::Wifi); + kt::ServerSocket server; kt::UDPSocket socket; std::pair bindResult = socket.bind(std::nullopt, server.getPort()); diff --git a/tests/socket/TCPSocketTest.cpp b/tests/socket/TCPSocketTest.cpp index e004fa4..0d32e1c 100644 --- a/tests/socket/TCPSocketTest.cpp +++ b/tests/socket/TCPSocketTest.cpp @@ -23,7 +23,7 @@ namespace kt TCPSocket socket; protected: - TCPSocketTest() : serverSocket(SocketType::Wifi), socket(LOCALHOST, serverSocket.getPort()) { } + TCPSocketTest() : serverSocket(), socket(LOCALHOST, serverSocket.getPort()) { } void TearDown() override { socket.close(); @@ -297,7 +297,7 @@ namespace kt TEST_F(TCPSocketTest, IPV6Address) { - ServerSocket ipv6ServerSocket(SocketType::Wifi, std::nullopt, 0, 20, InternetProtocolVersion::IPV6); + ServerSocket ipv6ServerSocket(std::nullopt, 0, 20, InternetProtocolVersion::IPV6); TCPSocket ipv6Socket("0:0:0:0:0:0:0:1", ipv6ServerSocket.getPort()); From 738227a9fd71b68a06a042e16831101447707b2f Mon Sep 17 00:00:00 2001 From: Kilemonn Date: Tue, 24 Dec 2024 21:01:24 +0900 Subject: [PATCH 2/2] Update readme with changes. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 25e0433..af708ed 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ A ServerSocket and Socket library for Windows and Linux that supports Wifi commu void tcpExample() { // Create a new Wifi ServerSocket - kt::ServerSocket server(kt::SocketType::Wifi, 56756, 20, kt::InternetProtocolVersion::IPV6); + kt::ServerSocket server(std::nullopt, 56756, 20, kt::InternetProtocolVersion::IPV6); // Create new TCP socket kt::TCPSocket client("::1", server.getPort()); @@ -70,7 +70,7 @@ void udpExample() { // The socket receiving data must first be bound kt::UDPSocket socket; - socket.bind(37893, kt::InternetProtocolVersion::IPV4); + socket.bind(std::nullopt, 37893, kt::InternetProtocolVersion::IPV4); kt::UDPSocket client; const std::string testString = "UDP test string";