PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
in_memory_ethernet.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <chrono>
11#include <condition_variable>
12#include <cstdint>
13#include <deque>
14#include <memory>
15#include <mutex>
16#include <vector>
17
19#include "profinet/util.h"
20
22{
23
25
29{
30 public:
35
37 ~InMemoryEthernetSocket() override;
38
41
44 void Send(const std::vector<std::uint8_t>& frame) override;
45
48 void SetTimeout(std::chrono::milliseconds timeout) override;
49
51 [[nodiscard]] std::vector<std::uint8_t> Recv() override;
52
55 void SetReceiveEnabled(bool enabled);
56
58 [[nodiscard]] bool ReceiveEnabled() const noexcept;
59
61 [[nodiscard]] const MacAddress& LocalMac() const noexcept
62 {
63 return localMac;
64 }
65
66 private:
67 friend class InMemoryEthernetBus;
68
70 void Enqueue(const std::vector<std::uint8_t>& frame);
71
74 std::chrono::milliseconds timeout{1};
75 mutable std::mutex mutex;
76 std::condition_variable condition;
77 std::deque<std::vector<std::uint8_t>> receiveQueue;
78 bool connected = true;
79 bool receiveEnabled = true;
80};
81
92{
93 public:
97
100
104 [[nodiscard]] std::unique_ptr<InMemoryEthernetSocket> CreateSocket(MacAddress localMac);
105
107 [[nodiscard]] std::size_t EndpointCount() const;
108
110 void ClearQueues();
111
112 private:
114
116 void Attach(InMemoryEthernetSocket* socket);
117
119 void Detach(InMemoryEthernetSocket* socket);
120
122 void Transmit(InMemoryEthernetSocket& sender,
123 const std::vector<std::uint8_t>& frame);
124
125 mutable std::mutex mutex;
126 std::vector<InMemoryEthernetSocket*> endpoints;
127};
128
129} // namespace profinet::sim
Abstraction for sending and receiving raw Ethernet frames.
Abstract interface for raw Ethernet frame transport.
Small virtual Ethernet switch suitable for deterministic tests and demos.
void Transmit(InMemoryEthernetSocket &sender, const std::vector< std::uint8_t > &frame)
Route a frame to matching endpoints.
InMemoryEthernetBus()=default
Construct an empty bus.
std::size_t EndpointCount() const
Return the number of currently connected socket endpoints.
void Attach(InMemoryEthernetSocket *socket)
Attach one socket.
InMemoryEthernetBus & operator=(const InMemoryEthernetBus &)=delete
void Detach(InMemoryEthernetSocket *socket)
Detach one socket.
std::unique_ptr< InMemoryEthernetSocket > CreateSocket(MacAddress localMac)
Create one virtual raw-Ethernet endpoint.
void ClearQueues()
Remove all queued frames without disconnecting endpoints.
InMemoryEthernetBus(const InMemoryEthernetBus &)=delete
std::vector< InMemoryEthernetSocket * > endpoints
Injectable raw-Ethernet endpoint attached to an in-memory bus.
void Enqueue(const std::vector< std::uint8_t > &frame)
Queue one frame for this endpoint.
InMemoryEthernetSocket & operator=(const InMemoryEthernetSocket &)=delete
void SetTimeout(std::chrono::milliseconds timeout) override
Set the maximum time spent waiting for a frame.
void Send(const std::vector< std::uint8_t > &frame) override
Transmit one complete Ethernet frame onto the virtual bus.
~InMemoryEthernetSocket() override
Disconnect and detach the endpoint.
InMemoryEthernetSocket(const InMemoryEthernetSocket &)=delete
bool ReceiveEnabled() const noexcept
Return whether this endpoint currently accepts incoming frames.
std::vector< std::uint8_t > Recv() override
Receive one frame or an empty vector after a timeout.
void SetReceiveEnabled(bool enabled)
Enable or disable frame delivery into this endpoint receive queue.
std::deque< std::vector< std::uint8_t > > receiveQueue
const MacAddress & LocalMac() const noexcept
Return the endpoint MAC address.
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
Definition util.h:48
Bytes frame
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.