Motion Master 6.0.0-alpha.111
Next-generation motion control software
Loading...
Searching...
No Matches
spoe_connection.h
Go to the documentation of this file.
1#pragma once
2
3// One TCP connection to one SPoE server, the netX of one drive.
4//
5// The drive answers one request at a time, so requests on one connection take turns behind a
6// mutex. The control plane and the process-data exchange share a connection that way.
7//
8// A reply is matched to its request by sequence id and type. A reply that does not match is a late
9// answer to an earlier request that timed out. It is logged and discarded, so it is never returned
10// for a later request. Received bytes stay in a buffer between requests, so a reply that arrives
11// after its request timed out does not break the framing of the next one.
12//
13// Asio stays inside the implementation file, so this header does not include it.
14
15#include <chrono>
16#include <cstdint>
17#include <expected>
18#include <memory>
19#include <span>
20#include <string>
21
22#include "comm/spoe_frame.h"
23
24namespace mm::comm::spoe {
25
29 enum class Kind : uint8_t {
34 kClosed,
37 };
38
40 std::string message;
41};
42
44 public:
47
50
52 std::expected<void, std::string> connect(const std::string& host, uint16_t port,
53 std::chrono::milliseconds timeout);
54
56 void close();
57
58 bool isOpen() const;
59
61 std::expected<Frame, ConnectionError> request(MessageType type, uint16_t status,
62 std::span<const uint8_t> data,
63 std::chrono::milliseconds timeout);
64
66 uint64_t discardedReplies() const;
67
68 private:
69 struct Impl;
70 std::unique_ptr<Impl> impl_;
71};
72
73} // namespace mm::comm::spoe
Definition spoe_connection.h:43
SpoeConnection & operator=(const SpoeConnection &)=delete
void close()
Closes the connection. Waits for a request in progress to finish first.
Definition spoe_connection.cc:146
std::expected< Frame, ConnectionError > request(MessageType type, uint16_t status, std::span< const uint8_t > data, std::chrono::milliseconds timeout)
Sends one request and waits up to timeout for its reply.
Definition spoe_connection.cc:155
bool isOpen() const
Definition spoe_connection.cc:151
std::expected< void, std::string > connect(const std::string &host, uint16_t port, std::chrono::milliseconds timeout)
Opens the connection. An open connection is closed first.
Definition spoe_connection.cc:113
uint64_t discardedReplies() const
The replies discarded because they matched no request in progress.
Definition spoe_connection.cc:153
SpoeConnection(const SpoeConnection &)=delete
SpoeConnection()
Definition spoe_connection.cc:109
~SpoeConnection()
Definition spoe_connection.cc:111
Definition spoe_connection.cc:15
MessageType
The message types the firmware handles. A reply carries the type of its request.
Definition spoe_frame.h:20
std::chrono::milliseconds timeout
Definition somanet_procedures.cc:464
Definition spoe_connection.h:28
Kind
Definition spoe_connection.h:29
@ kInvalidRequest
The request could not be encoded, so nothing was sent. The connection stays open.
@ kTimeout
No matching reply arrived in time. The connection stays open.
Kind kind
Definition spoe_connection.h:39
std::string message
Definition spoe_connection.h:40
Definition spoe_connection.cc:54