Ocean
TestPackagedTCPClient.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_TEST_TESTNETWORK_TEST_PACKAGED_TCP_CLIENT_H
9 #define META_OCEAN_TEST_TESTNETWORK_TEST_PACKAGED_TCP_CLIENT_H
10 
12 
14 
15 namespace Ocean
16 {
17 
18 namespace Test
19 {
20 
21 namespace TestNetwork
22 {
23 
24 /**
25  * This class implements test for PackagedTCPClient.
26  * @ingroup testnetwork
27  */
28 class OCEAN_TEST_NETWORK_EXPORT TestPackagedTCPClient
29 {
30  protected:
31 
32  /**
33  * Definition of a vector holding bytes.
34  */
35  typedef std::vector<uint8_t> Buffer;
36 
37  /**
38  * This class implements a receiver for clients.
39  */
40  class OCEAN_TEST_NETWORK_EXPORT ClientReceiver
41  {
42  public:
43 
44  /**
45  * Event function for receiving data.
46  * @param data The data that has been received
47  * @param size The number of bytes
48  */
49  void onReceive(const void* data, const size_t size);
50 
51  public:
52 
53  /// The memory buffers.
54  std::vector<Buffer> buffers_;
55  };
56 
57  /**
58  * This class implements a receiver for servers.
59  */
60  class OCEAN_TEST_NETWORK_EXPORT ServerReceiver
61  {
62  public:
63 
64  /**
65  * Event function for connection requests.
66  * @param senderAddress The address of the sender
67  * @param senderPort The port of the sender
68  * @param connectionId The id of the connection
69  */
70  bool onConnectionRequest(const Network::Address4& senderAddress, const Network::Port& senderPort, const Network::PackagedTCPServer::ConnectionId connectionId);
71 
72  /**
73  * Event function for a disconnected connection.
74  * @param connectionId The id of the disconnected connection
75  */
77 
78  /**
79  * Event function for receiving data.
80  * @param connectionId The id of the connection from which the data has been received
81  * @param data The data that has been received
82  * @param size The number of bytes
83  */
84  void onReceive(const Network::PackagedTCPServer::ConnectionId connectionId, const void* data, const size_t size);
85 
86  public:
87 
88  /// The id of the latest connection request.
90 
91  /// The number of connection requests.
92  unsigned int numberConnectionRequests_ = 0u;
93 
94  /// The number of disconnections.
95  unsigned int numberDisconnections_ = 0u;
96 
97  /// The memory buffers.
98  std::vector<Buffer> buffers_;
99  };
100 
101  public:
102 
103  /**
104  * Tests all PackagedTCPClient functions.
105  * @param testDuration The number of seconds for each test, with range (0, infinity)
106  * @return True, if succeeded
107  */
108  static bool test(const double testDuration);
109 
110  /**
111  * Tests sending and receiving data.
112  * @param testDuration The number of seconds for each test, with range (0, infinity)
113  * @return True, if succeeded
114  */
115  static bool testSendReceive(const double testDuration);
116 };
117 
118 }
119 
120 }
121 
122 }
123 
124 #endif // META_OCEAN_TEST_TESTNETWORK_TEST_PACKAGED_TCP_CLIENT_H
This class wraps an address number with 32 bits.
Definition: Address4.h:26
unsigned int ConnectionId
Definition of a connection id.
Definition: ConnectionOrientedServer.h:34
static constexpr ConnectionId invalidConnectionId()
Returns an invalid connection id.
Definition: ConnectionOrientedServer.h:231
This class wraps a port number with 16 bits.
Definition: Port.h:26
This class implements a receiver for clients.
Definition: TestPackagedTCPClient.h:41
std::vector< Buffer > buffers_
The memory buffers.
Definition: TestPackagedTCPClient.h:54
void onReceive(const void *data, const size_t size)
Event function for receiving data.
This class implements a receiver for servers.
Definition: TestPackagedTCPClient.h:61
bool onConnectionRequest(const Network::Address4 &senderAddress, const Network::Port &senderPort, const Network::PackagedTCPServer::ConnectionId connectionId)
Event function for connection requests.
std::vector< Buffer > buffers_
The memory buffers.
Definition: TestPackagedTCPClient.h:98
void onReceive(const Network::PackagedTCPServer::ConnectionId connectionId, const void *data, const size_t size)
Event function for receiving data.
void onConnectionDisconnected(const Network::PackagedTCPServer::ConnectionId connectionId)
Event function for a disconnected connection.
This class implements test for PackagedTCPClient.
Definition: TestPackagedTCPClient.h:29
static bool test(const double testDuration)
Tests all PackagedTCPClient functions.
static bool testSendReceive(const double testDuration)
Tests sending and receiving data.
std::vector< uint8_t > Buffer
Definition of a vector holding bytes.
Definition: TestPackagedTCPClient.h:35
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15