-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathModbusServerTCPasync.h
More file actions
93 lines (73 loc) · 2.19 KB
/
Copy pathModbusServerTCPasync.h
File metadata and controls
93 lines (73 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// =================================================================================================
// eModbus: Copyright 2020 by Michael Harwerth, Bert Melis and the contributors to eModbus
// MIT license - see license.md for details
// =================================================================================================
#ifndef _MODBUS_SERVER_TCP_ASYNC_H
#define _MODBUS_SERVER_TCP_ASYNC_H
#include "options.h"
#if defined(ESP32) || defined(ESP8266)
#include <list>
#include <queue>
#if USE_MUTEX
#include <mutex> // NOLINT
#endif
#include <vector>
#include <Arduino.h> // for millis()
#if defined(ESP32)
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESPAsyncTCP.h>
#endif
#include "ModbusServer.h"
#if USE_MUTEX
using std::lock_guard;
#endif
class ModbusServerTCPasync : public ModbusServer {
private:
class mb_client {
friend class ModbusServerTCPasync;
public:
mb_client(ModbusServerTCPasync* s, AsyncClient* c);
~mb_client();
private:
void onData(uint8_t* data, size_t len);
void onPoll();
void onDisconnect();
void addResponseToOutbox(ModbusMessage* response);
void handleOutbox();
ModbusServerTCPasync* server;
AsyncClient* client;
uint32_t lastActiveTime;
ModbusMessage* message;
Modbus::Error error;
std::queue<ModbusMessage*> outbox;
#if USE_MUTEX
std::mutex obLock; // outbox protection
#endif
};
public:
// Constructor
ModbusServerTCPasync();
// Destructor: closes the connections
~ModbusServerTCPasync();
// activeClients: return number of clients currently employed
uint16_t activeClients();
// start: create task with TCP server to accept requests
bool start(uint16_t port, uint8_t maxClients, uint32_t timeout, int coreID = -1);
// stop: drop all connections and kill server task
bool stop();
// isRunning: return true is server is running
bool isRunning();
protected:
void onClientConnect(AsyncClient* client);
void onClientDisconnect(mb_client* client);
AsyncServer* server;
std::list<mb_client*> clients;
uint8_t maxNoClients;
uint32_t idle_timeout;
#if USE_MUTEX
std::mutex cListLock; // client list protection
#endif
};
#endif
#endif