MOTION  0.01
Framework for mixed-protocol multi-party computation
tcp_transport.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2020 Lennart Braun
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <map>
26 #include <memory>
27 #include <string_view>
28 #include <vector>
29 
30 #include "transport.h"
31 
33 
34 namespace detail {
35 
36 struct TcpTransportImplementation;
37 
38 } // namespace detail
39 
40 class TcpTransport : public Transport {
41  public:
42  TcpTransport(std::unique_ptr<detail::TcpTransportImplementation> implementation);
43  TcpTransport(TcpTransport&& other);
44 
45  // Destructor needs to be defined in implementation due to pimpl
46  ~TcpTransport();
47 
48  void SendMessage(std::vector<std::uint8_t>&& message) override;
49  void SendMessage(const std::vector<std::uint8_t>& message) override;
50 
51  bool Available() const override;
52  std::optional<std::vector<std::uint8_t>> ReceiveMessage() override;
53  void ShutdownSend() override;
54  void Shutdown() override;
55 
56  private:
57  bool is_connected_;
58  std::unique_ptr<detail::TcpTransportImplementation> implementation_;
59 };
60 
61 using TcpConnectionConfiguration = std::pair<std::string, std::uint16_t>;
62 using TcpPartiesConfiguration = std::vector<TcpConnectionConfiguration>;
63 
64 // Helper class to establish point-to-point TCP connections among a set of
65 // parties. Given the ID of the local party and a collection of host and port
66 // for all parties, connections are created as follows: This party tries to
67 // connect to all parties with smaller IDs, and it accepts connections from the
68 // parties with larger IDs.
70  public:
71  TcpSetupHelper(std::size_t my_id, const TcpPartiesConfiguration& parties_configuration);
72 
73  // Destructor needs to be defined in implementation due to pimpl
75 
76  // Try to establish connections as described above.
77  // Throws a std::runtime_error if something goes wrong.
78  std::vector<std::unique_ptr<Transport>> SetupConnections();
79 
80  private:
82 
83  std::size_t my_id_;
84  std::size_t number_of_parties_;
85  const TcpPartiesConfiguration parties_configuration_;
86  std::unique_ptr<TcpSetupImplementation> implementation_;
87 };
88 
89 } // namespace encrypto::motion::communication
to_string
std::string to_string(Provider p)
Definition: benchmark_providers.h:44
encrypto::motion::communication::TcpConnectionConfiguration
std::pair< std::string, std::uint16_t > TcpConnectionConfiguration
Definition: tcp_transport.h:61
encrypto::motion::communication::TcpTransport::Shutdown
void Shutdown() override
Definition: tcp_transport.cpp:80
encrypto::motion::communication::TransportStatistics::number_of_messages_sent
std::size_t number_of_messages_sent
Definition: transport.h:33
encrypto::motion::communication::TcpTransport::Available
bool Available() const override
Definition: tcp_transport.cpp:68
encrypto::motion::communication::u32tou8
static void u32tou8(std::uint32_t v, std::uint8_t *result)
Definition: tcp_transport.cpp:89
encrypto::motion::communication::detail::TcpTransportImplementation::TcpTransportImplementation
TcpTransportImplementation(std::shared_ptr< boost::asio::io_context > io_context, tcp::socket &&socket)
Definition: tcp_transport.cpp:50
encrypto::motion::communication::TcpTransport::TcpTransport
TcpTransport(std::unique_ptr< detail::TcpTransportImplementation > implementation)
Definition: tcp_transport.cpp:60
encrypto::motion::communication::TcpSetupHelper::TcpSetupHelper
TcpSetupHelper(std::size_t my_id, const TcpPartiesConfiguration &parties_configuration)
Definition: tcp_transport.cpp:174
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation
Definition: tcp_transport.cpp:159
encrypto::motion::communication::Transport
Definition: transport.h:41
encrypto::motion::communication::TransportStatistics::number_of_messages_received
std::size_t number_of_messages_received
Definition: transport.h:34
transport.h
encrypto::motion::communication::detail::TcpTransportImplementation::socket_
boost::asio::ip::tcp::socket socket_
Definition: tcp_transport.cpp:54
encrypto::motion::communication::detail::TcpTransportImplementation::io_context_
std::shared_ptr< boost::asio::io_context > io_context_
Definition: tcp_transport.cpp:53
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::number_of_parties_
std::size_t number_of_parties_
Definition: tcp_transport.cpp:165
encrypto::motion::communication::TcpSetupHelper::~TcpSetupHelper
~TcpSetupHelper()
encrypto::motion::communication::detail::TcpTransportImplementation::socket_mutex_
std::shared_mutex socket_mutex_
Definition: tcp_transport.cpp:55
encrypto::motion::communication::TcpSetupHelper
Definition: tcp_transport.h:69
encrypto::motion::communication::TcpTransport::SendMessage
void SendMessage(std::vector< std::uint8_t > &&message) override
Definition: tcp_transport.cpp:87
encrypto::motion::communication::u8tou32
static std::uint32_t u8tou32(std::array< std::uint8_t, sizeof(std::uint32_t)> &v)
Definition: tcp_transport.cpp:117
encrypto::motion::communication::TcpPartiesConfiguration
std::vector< TcpConnectionConfiguration > TcpPartiesConfiguration
Definition: tcp_transport.h:62
encrypto::motion::communication::TcpTransport
Definition: tcp_transport.h:40
encrypto::motion::communication::detail::TcpTransportImplementation
Definition: tcp_transport.cpp:49
encrypto::motion::communication::TransportStatistics::number_of_bytes_received
std::size_t number_of_bytes_received
Definition: transport.h:36
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::sockets_
std::map< std::size_t, tcp::socket > sockets_
Definition: tcp_transport.cpp:171
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::bind_port_
std::uint16_t bind_port_
Definition: tcp_transport.cpp:169
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::my_id_
std::size_t my_id_
Definition: tcp_transport.cpp:164
encrypto::motion::communication::TcpTransport::ShutdownSend
void ShutdownSend() override
Definition: tcp_transport.cpp:74
encrypto::motion::communication::TcpTransport::ReceiveMessage
std::optional< std::vector< std::uint8_t > > ReceiveMessage() override
Definition: tcp_transport.cpp:125
encrypto::motion::communication
Definition: backend.h:37
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::bind_address_
boost::asio::ip::address bind_address_
Definition: tcp_transport.cpp:168
encrypto::motion::communication::TcpTransport::~TcpTransport
~TcpTransport()
encrypto::motion::communication::Transport::statistics_
TransportStatistics statistics_
Definition: transport.h:70
tcp_transport.h
encrypto::motion::communication::TcpSetupHelper::TcpSetupImplementation::io_context_
std::shared_ptr< boost::asio::io_context > io_context_
Definition: tcp_transport.cpp:170
encrypto::motion::communication::TransportStatistics::number_of_bytes_sent
std::size_t number_of_bytes_sent
Definition: transport.h:35
encrypto::motion::communication::TcpSetupHelper::SetupConnections
std::vector< std::unique_ptr< Transport > > SetupConnections()
Definition: tcp_transport.cpp:203