Skip to content

Commit 572fff5

Browse files
committed
impr: monitor checks if connected before _read
impr: tcp_client protect flags and initialization methods minor readability/optimization
1 parent 91d50b3 commit 572fff5

File tree

3 files changed

+42
-68
lines changed

3 files changed

+42
-68
lines changed

src/monitor.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,12 @@ class BridgeMonitor: public Stream {
9494

9595
int peek() override {
9696
k_mutex_lock(&monitor_mutex, K_FOREVER);
97+
int out = -1;
9798
if (temp_buffer.available()) {
98-
int c = temp_buffer.peek();
99-
k_mutex_unlock(&monitor_mutex);
100-
return c;
99+
out = temp_buffer.peek();
101100
}
102101
k_mutex_unlock(&monitor_mutex);
103-
return -1;
102+
return out;
104103
}
105104

106105
size_t write(uint8_t c) override {
@@ -117,11 +116,8 @@ class BridgeMonitor: public Stream {
117116

118117
size_t written;
119118
const bool ret = bridge->call(MON_WRITE_METHOD, send_buffer).result(written);
120-
if (ret) {
121-
return written;
122-
}
123119

124-
return 0;
120+
return ret? written : 0;
125121
}
126122

127123
bool reset() {
@@ -138,24 +134,23 @@ class BridgeMonitor: public Stream {
138134

139135
if (size == 0) return;
140136

137+
k_mutex_lock(&monitor_mutex, K_FOREVER);
138+
141139
MsgPack::arr_t<uint8_t> message;
142140
RpcResult async_rpc = bridge->call(MON_READ_METHOD, size);
143-
144-
const bool ret = async_rpc.result(message);
141+
const bool ret = _connected && async_rpc.result(message);
145142

146143
if (ret) {
147-
k_mutex_lock(&monitor_mutex, K_FOREVER);
148144
for (size_t i = 0; i < message.size(); ++i) {
149145
temp_buffer.store_char(static_cast<char>(message[i]));
150146
}
151-
k_mutex_unlock(&monitor_mutex);
152147
}
153148

154149
// if (async_rpc.error.code > NO_ERR) {
155-
// k_mutex_lock(&monitor_mutex, K_FOREVER);
156150
// _connected = false;
157-
// k_mutex_unlock(&monitor_mutex);
158151
// }
152+
153+
k_mutex_unlock(&monitor_mutex);
159154
}
160155

161156
};

src/tcp_client.h

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -55,73 +55,55 @@ class BridgeTCPClient : public Client {
5555

5656
int connect(const char *host, uint16_t port) override {
5757

58-
if (_connected) return 0;
59-
60-
String hostname = host;
61-
6258
k_mutex_lock(&client_mutex, K_FOREVER);
6359

64-
const bool resp = bridge->call(TCP_CONNECT_METHOD, hostname, port).result(connection_id);
65-
66-
if (!resp) {
67-
_connected = false;
68-
k_mutex_unlock(&client_mutex);
69-
return -1;
70-
}
71-
_connected = true;
60+
String hostname = host;
61+
const bool ok = _connected || bridge->call(TCP_CONNECT_METHOD, hostname, port).result(connection_id);
62+
_connected = ok;
7263

7364
k_mutex_unlock(&client_mutex);
7465

75-
return 0;
66+
return ok? 0 : -1;
7667
}
7768

7869
int connectSSL(const char *host, uint16_t port, const char *ca_cert) {
7970

80-
if (_connected) return 0;
71+
k_mutex_lock(&client_mutex, K_FOREVER);
8172

8273
String hostname = host;
8374
String ca_cert_str = ca_cert;
8475

85-
k_mutex_lock(&client_mutex, K_FOREVER);
86-
87-
const bool resp = bridge->call(TCP_CONNECT_SSL_METHOD, hostname, port, ca_cert_str).result(connection_id);
88-
89-
if (!resp) {
90-
_connected = false;
91-
k_mutex_unlock(&client_mutex);
92-
return -1;
93-
}
94-
_connected = true;
95-
76+
const bool ok = _connected || bridge->call(TCP_CONNECT_SSL_METHOD, hostname, port, ca_cert_str).result(connection_id);
77+
_connected = ok;
9678
k_mutex_unlock(&client_mutex);
97-
return 0;
79+
80+
return ok? 0 : -1;
9881
}
9982

100-
uint32_t getId() const {
101-
return connection_id;
83+
uint32_t getId() {
84+
k_mutex_lock(&client_mutex, K_FOREVER);
85+
const uint32_t out = connection_id;
86+
k_mutex_unlock(&client_mutex);
87+
return out;
10288
}
10389

10490
size_t write(uint8_t c) override {
10591
return write(&c, 1);
10692
}
10793

108-
size_t write(const uint8_t *buf, size_t size) override {
94+
size_t write(const uint8_t *buffer, size_t size) override {
10995

110-
if (!_connected) return 0;
96+
if (!connected()) return 0;
11197

11298
MsgPack::arr_t<uint8_t> payload;
11399

114100
for (size_t i = 0; i < size; ++i) {
115-
payload.push_back(buf[i]);
101+
payload.push_back(buffer[i]);
116102
}
117103

118104
size_t written;
119-
const bool ret = bridge->call(TCP_WRITE_METHOD, connection_id, payload).result(written);
120-
if (ret) {
121-
return written;
122-
}
123-
124-
return 0;
105+
const bool ok = bridge->call(TCP_WRITE_METHOD, connection_id, payload).result(written);
106+
return ok? written : 0;
125107
}
126108

127109
int available() override {
@@ -151,12 +133,12 @@ class BridgeTCPClient : public Client {
151133

152134
int peek() override {
153135
k_mutex_lock(&client_mutex, K_FOREVER);
136+
int out = -1;
154137
if (temp_buffer.available()) {
155-
k_mutex_unlock(&client_mutex);
156-
return temp_buffer.peek();
138+
out = temp_buffer.peek();
157139
}
158140
k_mutex_unlock(&client_mutex);
159-
return -1;
141+
return out;
160142
}
161143

162144
void flush() override {
@@ -170,37 +152,35 @@ class BridgeTCPClient : public Client {
170152
void stop() override {
171153
k_mutex_lock(&client_mutex, K_FOREVER);
172154
String msg;
173-
const bool resp = bridge->call(TCP_CLOSE_METHOD, connection_id).result(msg);
174-
if (resp) {
175-
_connected = false;
155+
if (_connected) {
156+
_connected = !bridge->call(TCP_CLOSE_METHOD, connection_id).result(msg);
176157
}
177158
k_mutex_unlock(&client_mutex);
178159
}
179160

180161
uint8_t connected() override {
181-
if (_connected) return 1;
182-
return 0;
162+
k_mutex_lock(&client_mutex, K_FOREVER);
163+
const uint8_t out = _connected? 1 : 0;
164+
k_mutex_unlock(&client_mutex);
165+
return out;
183166
}
184167

185168
operator bool() override {
186169
return available() || connected();
187170
}
188171

189-
//friend class BridgeTCPServer;
190-
191172
using Print::write;
192173

193174
private:
194175
void _read(size_t size) {
195176

196-
if (size == 0 || !_connected) return;
177+
if (size == 0) return;
197178

198179
k_mutex_lock(&client_mutex, K_FOREVER);
199180

200181
MsgPack::arr_t<uint8_t> message;
201182
RpcResult async_rpc = bridge->call(TCP_READ_METHOD, connection_id, size);
202-
203-
const bool ret = async_rpc.result(message);
183+
const bool ret = _connected && async_rpc.result(message);
204184

205185
if (ret) {
206186
for (size_t i = 0; i < message.size(); ++i) {

src/tcp_server.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,12 @@ class BridgeTCPServer final: public Server {
9191
if (!client) return 0;
9292

9393
k_mutex_lock(&server_mutex, K_FOREVER);
94+
size_t written = 0;
9495
if (_connected) {
95-
size_t written = client.write(buf, size);
96-
k_mutex_unlock(&server_mutex);
97-
return written;
96+
written = client.write(buf, size);
9897
}
9998
k_mutex_unlock(&server_mutex);
100-
return 0;
99+
return written;
101100
}
102101

103102
void close() {

0 commit comments

Comments
 (0)