FairMQ  1.3.9
C++ Message Passing Framework
Socket.h
1 /********************************************************************************
2  * Copyright (C) 2018 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_OFI_SOCKET_H
10 #define FAIR_MQ_OFI_SOCKET_H
11 
12 #include <FairMQSocket.h>
13 #include <FairMQMessage.h>
14 #include <fairmq/ofi/Context.h>
15 #include <fairmq/ofi/Control.pb.h>
16 
17 #include <boost/asio.hpp>
18 #include <memory> // unique_ptr
19 #include <netinet/in.h>
20 #include <rdma/fabric.h>
22 
23 namespace fair
24 {
25 namespace mq
26 {
27 namespace ofi
28 {
29 
36 class Socket final : public fair::mq::Socket
37 {
38  public:
39  Socket(Context& factory, const std::string& type, const std::string& name, const std::string& id = "", FairMQTransportFactory* fac);
40  Socket(const Socket&) = delete;
41  Socket operator=(const Socket&) = delete;
42 
43  auto GetId() -> std::string { return fId; }
44 
45  auto Bind(const std::string& address) -> bool override;
46  auto Connect(const std::string& address) -> bool override;
47 
48  auto Send(MessagePtr& msg, int timeout = 0) -> int override;
49  auto Receive(MessagePtr& msg, int timeout = 0) -> int override;
50  auto Send(std::vector<MessagePtr>& msgVec, int timeout = 0) -> int64_t override;
51  auto Receive(std::vector<MessagePtr>& msgVec, int timeout = 0) -> int64_t override;
52 
53  auto TrySend(MessagePtr& msg) -> int override;
54  auto TryReceive(MessagePtr& msg) -> int override;
55  auto TrySend(std::vector<MessagePtr>& msgVec) -> int64_t override;
56  auto TryReceive(std::vector<MessagePtr>& msgVec) -> int64_t override;
57 
58  auto GetSocket() const -> void* { return fControlSocket; }
59 
60  void SetLinger(const int value) override;
61  int GetLinger() const override;
62  void SetSndBufSize(const int value) override;
63  int GetSndBufSize() const override;
64  void SetRcvBufSize(const int value) override;
65  int GetRcvBufSize() const override;
66  void SetSndKernelSize(const int value) override;
67  int GetSndKernelSize() const override;
68  void SetRcvKernelSize(const int value) override;
69  int GetRcvKernelSize() const override;
70 
71  auto Close() -> void override;
72 
73  auto SetOption(const std::string& option, const void* value, size_t valueSize) -> void override;
74  auto GetOption(const std::string& option, void* value, size_t* valueSize) -> void override;
75 
76  auto GetBytesTx() const -> unsigned long override { return fBytesTx; }
77  auto GetBytesRx() const -> unsigned long override { return fBytesRx; }
78  auto GetMessagesTx() const -> unsigned long override { return fMessagesTx; }
79  auto GetMessagesRx() const -> unsigned long override { return fMessagesRx; }
80 
81  static auto GetConstant(const std::string& constant) -> int;
82 
83  ~Socket() override;
84 
85  private:
86  void* fControlSocket;
87  void* fMonitorSocket;
88  fid_ep* fDataEndpoint;
89  fid_cq* fDataCompletionQueueTx;
90  fid_cq* fDataCompletionQueueRx;
91  std::string fId;
92  std::atomic<unsigned long> fBytesTx;
93  std::atomic<unsigned long> fBytesRx;
94  std::atomic<unsigned long> fMessagesTx;
95  std::atomic<unsigned long> fMessagesRx;
96  Context& fContext;
97  fi_addr_t fRemoteDataAddr;
98  sockaddr_in fLocalDataAddr;
99  bool fWaitingForControlPeer;
100  boost::asio::io_service::strand fIoStrand;
101 
102  int fSndTimeout;
103  int fRcvTimeout;
104 
105  auto SendImpl(MessagePtr& msg, const int flags, const int timeout) -> int;
106  auto ReceiveImpl(MessagePtr& msg, const int flags, const int timeout) -> int;
107  auto SendImpl(std::vector<MessagePtr>& msgVec, const int flags, const int timeout) -> int64_t;
108  auto ReceiveImpl(std::vector<MessagePtr>& msgVec, const int flags, const int timeout) -> int64_t;
109 
110  auto InitDataEndpoint() -> void;
111  auto WaitForControlPeer() -> void;
112  auto AnnounceDataAddress() -> void;
113  auto SendControlMessage(std::unique_ptr<ControlMessage> ctrl) -> void;
114  auto ReceiveControlMessage() -> std::unique_ptr<ControlMessage>;
115  auto ProcessDataAddressAnnouncement(std::unique_ptr<ControlMessage> ctrl) -> void;
116  auto ConnectControlSocket(Context::Address address) -> void;
117  auto BindControlSocket(Context::Address address) -> void;
118 }; /* class Socket */
119 
120 // helper function to clean up the object holding the data after it is transported.
121 void free_string(void* /*data*/, void* hint);
122 
123 struct SilentSocketError : SocketError { using SocketError::SocketError; };
124 
125 } /* namespace ofi */
126 } /* namespace mq */
127 } /* namespace fair */
128 
129 #endif /* FAIR_MQ_OFI_SOCKET_H */
Transport-wide context.
Definition: Context.h:38
Definition: FairMQSocket.h:74
Definition: FairMQTransportFactory.h:28
Definition: Socket.h:36
Definition: FairMQSocket.h:19
Definition: Context.h:54
Tools for interfacing containers to the transport via polymorphic allocators.
Definition: DeviceRunner.h:23
Definition: Socket.h:123

privacy