Finish socket operations

This commit is contained in:
Christoph Hagen
2023-12-05 21:31:11 +01:00
parent 9b49c3565d
commit 4c23565b9c
6 changed files with 121 additions and 55 deletions

View File

@ -31,9 +31,6 @@ struct EthernetConfiguration {
// The IP address of the DNS server, if DHCP fails
uint8_t manualDnsAddress[4];
uint32_t socketHeartbeatIntervalMs;
uint32_t socketHeartbeatTimeoutMs;
uint8_t socketHeartbeatFailureReconnectCount;
};
struct KeyConfiguration {
@ -88,23 +85,11 @@ private:
currentChallengeExpiry = 0;
}
/**
* @brief Send an error Response over the web socket.
*
* @param result The error result to send
* @param discardMessage Indicate if the stored message should be cleared.
*
* Note: Only clear the message if no other operation is in progress.
*/
void sendErrorResponseToServer(MessageResult result, bool discardMessage = true);
void ensureWebSocketConnection();
// MARK: Local client callbacks
void handleLocalMessage(AsyncWebServerRequest *request);
bool convertHexMessageToBinary(const char* str);
void handleServerMessage(uint8_t* payload, size_t length);
// MARK: Socket Callbacks
/**
* @brief Callback to send an error back to the server via the web socket.
@ -115,13 +100,60 @@ private:
*/
void sendServerError(MessageResult event);
void processMessage(SignedMessage* message);
MessageResult verifyAndProcessReceivedMessage(SignedMessage* message);
void handleServerMessage(uint8_t* payload, size_t length);
// MARK: Message processing
/**
* @brief Process a received message (local or socket).
*
* @param message The message to process.
*
* Note: Prepares the response in the outgoing message buffer.
*/
void processMessage(SignedMessage* message);
/**
* @brief Prepare a server challenge for a local or socket message.
*
* @param message The message to respond to
*
* Note: Prepares the response in the outgoing message buffer.
*/
void prepareChallenge(Message* message);
/**
* @brief Complete an unlock request for a local or socket message.
*
* @param message The message to respond to
*
* Note: Prepares the response in the outgoing message buffer.
*/
void completeUnlockRequest(Message* message);
// MARK: Responses
/**
* @brief Prepare the outgoing message buffer for both socket and local responses.
*
* @param event The resulting state to transmit
* @param message An optional message to echo
*/
void prepareResponseBuffer(MessageResult event, Message* message = NULL);
/**
* @brief Send the prepared outgoing message to a locally connected client
*
* @param request The original request of the client
*/
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
/**
* @brief Send the prepared outgoing message to the server
*/
void sendPreparedResponseToServer();
void prepareChallenge(Message* message);
void completeUnlockRequest(Message* message);
// MARK: Helper
bool convertHexMessageToBinary(const char* str);
};

View File

@ -28,6 +28,10 @@ struct ServerConfiguration {
uint32_t reconnectTime;
uint32_t socketHeartbeatIntervalMs;
uint32_t socketHeartbeatTimeoutMs;
uint8_t socketHeartbeatFailureReconnectCount;
};
class ServerConnectionCallbacks {
@ -51,11 +55,13 @@ public:
*/
void configure(ServerConfiguration configuration, ServerConnectionCallbacks* callbacks);
void connect();
void disconnect();
void loop();
/**
* @brief Call this function regularly to handle socket operations.
*
* Connecting and disconnecting is done automatically.
*
*/
void loop(uint32_t millis);
/**
* @brief Send a response message over the socket
@ -65,11 +71,24 @@ public:
*/
void sendResponse(uint8_t* buffer, uint16_t length);
bool isSocketConnected() {
private:
uint32_t currentTime;
bool socketIsConnected() {
return webSocket.isConnected();
}
private:
void connect();
void disconnect();
bool shouldReconnect = true;
uint32_t nextReconnectAttemptMs = 0;
void didDisconnect();
void didConnect();
ServerConfiguration configuration;