44#include " ../socketexceptions/SocketException.hpp"
55#include " ../socketexceptions/BindingException.hpp"
66#include " ../socketexceptions/TimeoutException.hpp"
7- #include " ../enums/SocketType.h"
87#include " ../socketexceptions/SocketError.h"
98#include " ../address/SocketAddress.h"
109
3433#include < netinet/in.h>
3534#include < arpa/inet.h>
3635#include < sys/ioctl.h>
37- #include < bluetooth/hci.h>
38- #include < bluetooth/hci_lib.h>
3936#include < unistd.h>
4037
4138#endif
4239
4340namespace kt
4441{
4542 /* *
46- * ServerSocket constructor. Creates a wifi/bluetooth ServerSocket and begins listening for connections.
43+ * ServerSocket constructor. Creates a wifi ServerSocket and begins listening for connections.
4744 *
48- * @param type - Determines whether this ServerSocket is a wifi or bluetooth ServerSocket.
4945 * @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.
5046 * @param connectionBacklogSize - You can enter a value here to specify the length of the server connection pool. The default value is 20.
5147 *
5248 * @throw SocketException - If the ServerSocket is unable to be instanciated or begin listening.
5349 * @throw BindingException - If the ServerSocket is unable to bind to the specific port specified.
5450 */
55- kt::ServerSocket::ServerSocket (const kt::SocketType type, const std::optional<std::string>& localHostname, const unsigned short & port, const unsigned int & connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion)
51+ kt::ServerSocket::ServerSocket (const std::optional<std::string>& localHostname, const unsigned short & port, const unsigned int & connectionBacklogSize, const kt::InternetProtocolVersion protocolVersion)
5652 {
5753 this ->socketDescriptor = getInvalidSocketValue ();
5854 this ->port = port;
59- this ->type = type;
6055 this ->protocolVersion = protocolVersion;
6156
62- if (this ->type == kt::SocketType::None)
63- {
64- throw SocketException (" Failed to create ServerSocket with 'None' SocketType." );
65- }
66-
6757 this ->constructSocket (localHostname, connectionBacklogSize);
6858 }
6959
@@ -75,7 +65,6 @@ namespace kt
7565 kt::ServerSocket::ServerSocket (const kt::ServerSocket& socket)
7666 {
7767 this ->port = socket.port ;
78- this ->type = socket.type ;
7968 this ->protocolVersion = socket.protocolVersion ;
8069 this ->socketDescriptor = socket.socketDescriptor ;
8170 this ->serverAddress = socket.serverAddress ;
@@ -91,7 +80,6 @@ namespace kt
9180 kt::ServerSocket& kt::ServerSocket::operator =(const kt::ServerSocket& socket)
9281 {
9382 this ->port = socket.port ;
94- this ->type = socket.type ;
9583 this ->protocolVersion = socket.protocolVersion ;
9684 this ->socketDescriptor = socket.socketDescriptor ;
9785 this ->serverAddress = socket.serverAddress ;
@@ -101,73 +89,7 @@ namespace kt
10189
10290 void kt::ServerSocket::constructSocket (const std::optional<std::string>& localHostname, const unsigned int & connectionBacklogSize)
10391 {
104- if (this ->type == kt::SocketType::Wifi)
105- {
106- this ->constructWifiSocket (localHostname, connectionBacklogSize);
107- }
108- else if (this ->type == kt::SocketType::Bluetooth)
109- {
110- this ->constructBluetoothSocket (connectionBacklogSize);
111- this ->setDiscoverable ();
112- }
113- }
114-
115- void kt::ServerSocket::constructBluetoothSocket (const unsigned int & connectionBacklogSize)
116- {
117- #ifdef _WIN32
118- throw kt::SocketException (" ServerSocket::constructBluetoothSocket(unsigned int) is not supported on Windows." );
119-
120- /* SOCKADDR_BTH bluetoothAddress;
121-
122- this->socketDescriptor = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
123-
124- if (this->socketDescriptor == 0)
125- {
126- throw SocketException("Error establishing BT server socket: " + std::string(std::strerror(errno)));
127- }
128-
129- this->bluetoothAddress.addressFamily = AF_BTH;
130- this->bluetoothAddress.btAddr = 0;
131- this->bluetoothAddress.port = this->port;
132-
133- if (bind(this->socketDescriptor, (sockaddr*)&this->bluetoothAddress, sizeof(SOCKADDR_BTH)) == -1)
134- {
135- 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()));
136- }
137-
138- if (listen(this->socketDescriptor, static_cast<int>(connectionBacklogSize)) == -1)
139- {
140- this->close();
141- throw SocketException("Error Listening on port: " + std::to_string(this->port) + ": " + std::string(std::strerror(errno)));
142- }*/
143-
144- #elif __linux__
145- sockaddr_rc localAddress = {0 };
146- this ->socketDescriptor = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
147-
148- if (isInvalidSocket (this ->socketDescriptor ))
149- {
150- throw SocketException (" Error establishing BT server socket: " + std::string (std::strerror (errno)));
151- }
152-
153- localAddress.rc_family = AF_BLUETOOTH;
154- localAddress.rc_bdaddr = ((bdaddr_t ) {{0 , 0 , 0 , 0 , 0 , 0 }});
155- localAddress.rc_channel = static_cast <uint8_t >(this ->port );
156-
157- if (bind (this ->socketDescriptor , (sockaddr *)&localAddress, sizeof (localAddress)) == -1 )
158- {
159- throw BindingException (" Error binding BT connection, the port " + std::to_string (this ->port ) + " is already being used: " + std::string (std::strerror (errno)));
160- }
161-
162- if (listen (this ->socketDescriptor , connectionBacklogSize) == -1 )
163- {
164- this ->close ();
165- throw SocketException (" Error Listening on port " + std::to_string (this ->port ) + " : " + std::string (std::strerror (errno)));
166- }
167- #endif
168-
169- // Make discoverable
170-
92+ this ->constructWifiSocket (localHostname, connectionBacklogSize);
17193 }
17294
17395 void kt::ServerSocket::constructWifiSocket (const std::optional<std::string>& localHostname, const unsigned int & connectionBacklogSize)
@@ -248,32 +170,6 @@ namespace kt
248170 this ->port = kt::getPortNumber (address.first .value ());
249171 }
250172
251-
252- void kt::ServerSocket::setDiscoverable ()
253- {
254- throw kt::SocketException (" ServerSocket::setDiscoverable() not implemented." );
255-
256- #if __linux__
257- hci_dev_req req;
258- req.dev_id = 0 ;
259- // req.dev_id = hci_get_route(nullptr);
260- req.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
261-
262- if (ioctl (this ->socketDescriptor , HCISETSCAN, (unsigned long )&req) < 0 )
263- {
264- throw SocketException (" Failed to make device discoverable." );
265- }
266- #endif
267- }
268-
269- /* *
270- * @return the *kt::SocketType* for this *kt::ServerSocket*.
271- */
272- kt::SocketType kt::ServerSocket::getType () const
273- {
274- return this ->type ;
275- }
276-
277173 /* *
278174 * Used to get the port number that the ServerSocket is listening on.
279175 * @return An unsigned int of the port number that the ServerSocket is listening on.
@@ -334,41 +230,6 @@ namespace kt
334230 return kt::TCPSocket (temp, hostname.value (), portNum, this ->getInternetProtocolVersion (), acceptedAddress);
335231 }
336232
337- kt::BluetoothSocket kt::ServerSocket::acceptBluetoothConnection (const long & timeout)
338- {
339- if (timeout > 0 )
340- {
341- int res = kt::pollSocket (this ->socketDescriptor , timeout);
342- if (res == -1 )
343- {
344- throw kt::SocketException (" Failed to poll as socket is no longer valid." );
345- }
346- else if (res == 0 )
347- {
348- throw kt::TimeoutException (" No applicable connections could be accepted during the time period specified " + std::to_string (timeout) + " microseconds." );
349- }
350- }
351-
352- throw kt::SocketException (" acceptBluetoothConnection() - Not yet implemented." );
353- #ifdef __linux__
354- // Remove bluetooth related code
355-
356- // sockaddr_rc remoteDevice = { 0 };
357- // socklen_t socketSize = sizeof(remoteDevice);
358- // SOCKET temp = ::accept(this->socketDescriptor, (sockaddr *) &remoteDevice, &socketSize);
359- // if (temp == -1)
360- // {
361- // throw SocketException("Failed to accept connection. Socket is in an invalid state.");
362- // }
363-
364- // if (this->type == kt::SocketType::Bluetooth)
365- // {
366- // char remoteAddress[1024] = {0};
367- // ba2str(&remoteDevice.rc_bdaddr, remoteAddress);
368- // }
369- #endif
370- }
371-
372233 /* *
373234 * Closes the existing connection. If no connection is open, then it will do nothing.
374235 *
0 commit comments