4
4
#include " ../socketexceptions/SocketException.hpp"
5
5
#include " ../socketexceptions/BindingException.hpp"
6
6
#include " ../socketexceptions/TimeoutException.hpp"
7
- #include " ../enums/SocketType.h"
8
7
#include " ../socketexceptions/SocketError.h"
9
8
#include " ../address/SocketAddress.h"
10
9
34
33
#include < netinet/in.h>
35
34
#include < arpa/inet.h>
36
35
#include < sys/ioctl.h>
37
- #include < bluetooth/hci.h>
38
- #include < bluetooth/hci_lib.h>
39
36
#include < unistd.h>
40
37
41
38
#endif
42
39
43
40
namespace kt
44
41
{
45
42
/* *
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.
47
44
*
48
- * @param type - Determines whether this ServerSocket is a wifi or bluetooth ServerSocket.
49
45
* @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.
50
46
* @param connectionBacklogSize - You can enter a value here to specify the length of the server connection pool. The default value is 20.
51
47
*
52
48
* @throw SocketException - If the ServerSocket is unable to be instanciated or begin listening.
53
49
* @throw BindingException - If the ServerSocket is unable to bind to the specific port specified.
54
50
*/
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)
56
52
{
57
53
this ->socketDescriptor = getInvalidSocketValue ();
58
54
this ->port = port;
59
- this ->type = type;
60
55
this ->protocolVersion = protocolVersion;
61
56
62
- if (this ->type == kt::SocketType::None)
63
- {
64
- throw SocketException (" Failed to create ServerSocket with 'None' SocketType." );
65
- }
66
-
67
57
this ->constructSocket (localHostname, connectionBacklogSize);
68
58
}
69
59
@@ -75,7 +65,6 @@ namespace kt
75
65
kt::ServerSocket::ServerSocket (const kt::ServerSocket& socket)
76
66
{
77
67
this ->port = socket.port ;
78
- this ->type = socket.type ;
79
68
this ->protocolVersion = socket.protocolVersion ;
80
69
this ->socketDescriptor = socket.socketDescriptor ;
81
70
this ->serverAddress = socket.serverAddress ;
@@ -91,7 +80,6 @@ namespace kt
91
80
kt::ServerSocket& kt::ServerSocket::operator =(const kt::ServerSocket& socket)
92
81
{
93
82
this ->port = socket.port ;
94
- this ->type = socket.type ;
95
83
this ->protocolVersion = socket.protocolVersion ;
96
84
this ->socketDescriptor = socket.socketDescriptor ;
97
85
this ->serverAddress = socket.serverAddress ;
@@ -101,73 +89,7 @@ namespace kt
101
89
102
90
void kt::ServerSocket::constructSocket (const std::optional<std::string>& localHostname, const unsigned int & connectionBacklogSize)
103
91
{
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);
171
93
}
172
94
173
95
void kt::ServerSocket::constructWifiSocket (const std::optional<std::string>& localHostname, const unsigned int & connectionBacklogSize)
@@ -248,32 +170,6 @@ namespace kt
248
170
this ->port = kt::getPortNumber (address.first .value ());
249
171
}
250
172
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
-
277
173
/* *
278
174
* Used to get the port number that the ServerSocket is listening on.
279
175
* @return An unsigned int of the port number that the ServerSocket is listening on.
@@ -334,41 +230,6 @@ namespace kt
334
230
return kt::TCPSocket (temp, hostname.value (), portNum, this ->getInternetProtocolVersion (), acceptedAddress);
335
231
}
336
232
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
-
372
233
/* *
373
234
* Closes the existing connection. If no connection is open, then it will do nothing.
374
235
*
0 commit comments