5
5
* Author: kolban
6
6
*/
7
7
8
+ #include < errno.h>
8
9
#include < esp_log.h>
9
10
#include < lwip/sockets.h>
10
11
#include < stdint.h>
12
+ #include < string.h>
11
13
#include < string>
12
- // #include "/opt/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/5.2.0/string"
13
14
14
15
#include " FreeRTOS.h"
15
- #include " SockServ.h"
16
16
#include " sdkconfig.h"
17
+ #include " SockServ.h"
17
18
18
19
static char tag[] = " SockServ" ;
19
20
21
+
20
22
/* *
21
- *
23
+ * Create an instance of the class.
24
+ * We won't actually start listening for clients until after the start() method has been called.
25
+ * @param [in] port The TCP/IP port number on which we will listen for incoming connection requests.
26
+ */
27
+ SockServ::SockServ (uint16_t port) {
28
+ this ->port = port;
29
+ clientSock = -1 ;
30
+ } // SockServ
31
+
32
+
33
+ /* *
34
+ * Accept an incoming connection.
35
+ * Block waiting for an incoming connection and accept it when it arrives.
22
36
*/
23
37
void SockServ::acceptTask (void *data) {
24
38
@@ -28,54 +42,71 @@ void SockServ::acceptTask(void *data) {
28
42
while (1 ) {
29
43
socklen_t clientAddressLength = sizeof (clientAddress);
30
44
int tempSock = ::accept (pSockServ->sock , (struct sockaddr *)&clientAddress, &clientAddressLength);
45
+ if (tempSock == -1 ) {
46
+ ESP_LOGE (tag, " close(): %s" , strerror (errno));
47
+ }
31
48
ESP_LOGD (tag, " accept() - New socket" );
32
49
if (pSockServ->clientSock != -1 ) {
33
- ::close (pSockServ->clientSock);
50
+ int rc = ::close (pSockServ->clientSock );
51
+ if (rc == -1 ) {
52
+ ESP_LOGE (tag, " close(): %s" , strerror (errno));
53
+ }
34
54
}
35
55
pSockServ->clientSock = tempSock;
36
56
}
37
- }
38
-
39
-
40
- SockServ::SockServ (uint16_t port) {
41
- this ->port = port;
42
- clientSock = -1 ;
43
- }
57
+ } // acceptTask
44
58
45
59
46
60
/* *
47
61
* Start listening for new partner connections.
48
62
*/
49
63
void SockServ::start () {
50
64
sock = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
65
+ if (sock == -1 ) {
66
+ ESP_LOGE (tag, " socket(): %s" , strerror (errno));
67
+ }
51
68
struct sockaddr_in serverAddress;
52
69
serverAddress.sin_family = AF_INET;
53
70
serverAddress.sin_addr .s_addr = htonl (INADDR_ANY);
54
71
serverAddress.sin_port = htons (port);
55
- ::bind (sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
56
- ::listen (sock, 5 );
72
+ int rc = ::bind (sock, (struct sockaddr *)&serverAddress, sizeof (s
B41A
erverAddress));
73
+ if (rc == -1 ) {
74
+ ESP_LOGE (tag, " bind(): %s" , strerror (errno));
75
+ }
76
+ rc = ::listen (sock, 5 );
77
+ if (rc == -1 ) {
78
+ ESP_LOGE (tag, " listen(): %s" , strerror (errno));
79
+ }
57
80
ESP_LOGD (tag, " Now listening on port %d" , port);
58
81
FreeRTOS::startTask (acceptTask, " acceptTask" , this );
59
- }
82
+ } // start
83
+
60
84
61
85
/* *
62
86
* Stop listening for new partner connections.
63
87
*/
64
88
void SockServ::stop () {
65
- ::close (sock);
66
- }
89
+ int rc = ::close (sock);
90
+ if (rc == -1 ) {
91
+ ESP_LOGE (tag, " close(): %s" , strerror (errno));
92
+ }
93
+ } // stop
94
+
67
95
68
96
/* *
69
97
* Send data to any connected partners.
70
98
* @param[in] data A sequence of bytes to send to the partner.
71
99
* @param[in] length The length of the sequence of bytes to send to the partner.
72
100
*/
73
101
void SockServ::sendData (uint8_t *data, size_t length) {
74
- if (clientSock == - 1 ) {
102
+ if (connectedCount () == 0 ) {
75
103
return ;
76
104
}
77
- ::send (clientSock, data, length, 0 );
78
- }
105
+ int rc = ::send (clientSock, data, length, 0 );
106
+ if (rc == -1 ) {
107
+ ESP_LOGE (tag, " send(): %s" , strerror (errno));
108
+ }
109
+ } // sendData
79
110
80
111
81
112
/* *
@@ -84,4 +115,16 @@ void SockServ::sendData(uint8_t *data, size_t length) {
84
115
*/
85
116
void SockServ::sendData (std::string str) {
86
117
sendData ((uint8_t *)str.data (), str.size ());
118
+ } // sendData
119
+
120
+
121
+ /* *
122
+ * Determine the number of connected partners.
123
+ * @return The number of connected partners.
124
+ */
125
+ int SockServ::connectedCount () {
126
+ if (clientSock == -1 ) {
127
+ return 0 ;
128
+ }
129
+ return 1 ;
87
130
}
0 commit comments