Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ReadMailReallyFast
code
Commits
82f9f817
Commit
82f9f817
authored
3 years ago
by
Leon Dietrich
Committed by
Benny Baumann
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add: Document TCP client class
parent
d44283ec
No related branches found
No related tags found
1 merge request
!1
First unit tests
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/net/tcp_client.cpp
+6
-0
6 additions, 0 deletions
src/net/tcp_client.cpp
src/net/tcp_client.hpp
+95
-2
95 additions, 2 deletions
src/net/tcp_client.hpp
with
101 additions
and
2 deletions
src/net/tcp_client.cpp
+
6
−
0
View file @
82f9f817
...
...
@@ -220,6 +220,7 @@ void tcp_client::push_write_queue(::ev::io &w) {
return
;
}
this
->
data_write_active
=
true
;
iorecord
buffer
=
this
->
write_queue
.
pop_front
();
ssize_t
written
=
write
(
w
.
fd
,
buffer
.
ptr
(),
buffer
.
size
());
...
...
@@ -230,6 +231,7 @@ void tcp_client::push_write_queue(::ev::io &w) {
}
this
->
write_queue
.
push_front
(
buffer
);
this
->
data_write_active
=
false
;
}
inline
std
::
string
tcp_client
::
get_peer_address
()
{
...
...
@@ -240,4 +242,8 @@ inline uint16_t tcp_client::get_port() {
return
this
->
port
;
}
inline
bool
tcp_client
::
is_write_queue_empty
()
{
return
this
->
write_queue
.
empty
()
&&
!
this
->
data_write_active
;
}
}
This diff is collapsed.
Click to expand it.
src/net/tcp_client.hpp
+
95
−
2
View file @
82f9f817
...
...
@@ -26,8 +26,17 @@ enum class exit_status_t : uint16_t {
TIMEOUT
=
1
};
/**
* This class ressembles a TCP client.
* @class tcp_client
* @author doralitze BenBe
* @brief A raw TCP client.
*/
class
tcp_client
:
public
connection_client
,
std
::
enable_shared_from_this
<
tcp_client
>
{
public:
/**
* A callback for the destructor must match this definition.
*/
typedef
std
::
function
<
void
(
exit_status_t
)
>
destructor_cb_type
;
private:
...
...
@@ -38,20 +47,104 @@ private:
auto_fd
net_socket
;
::
ev
::
io
io
;
ioqueue
write_queue
;
bool
data_write_active
=
false
;
public:
/**
* Construct a new TCP client using an already existing socket. This might be particulary useful if you've
* got a server and accept incoming connections.
*
* @brief Connect to a TCP server
* @param destructor_cb_ The callback that should be issued when the client closes or looses it's connection
* @param socket_fd A file descriptor for an already open socket to be wrapped by this client
* @param peer_address_ The address the socket is bound to on the other end of the connection
* @param port_ The bound port
*/
tcp_client
(
const
destructor_cb_type
destructor_cb_
,
auto_fd
&&
socket_fd
,
std
::
string
peer_address_
,
uint16_t
port_
);
/**
* Construct a new TCP client using an address and port pair.
* @brief Connect to a TCP server
* @param peer_address_ The address to connect to
* @param port_ The remote port to connect to described as uint16
*/
tcp_client
(
const
std
::
string
&
peer_address_
,
const
uint16_t
port_
);
/**
* Construct a new TCP client using an address and a service description.
* @brief Connect to a TCP server
* @param peer_address_ The address to connect to
* @param service_or_port The service or port to connect with.
*/
tcp_client
(
const
std
::
string
&
peer_address_
,
const
std
::
string
&
service_or_port
);
/**
* Construct a new TCP client using an address, a service description and a socket family identifier.
* @brief Connect to a TCP server
* @param peer_address_ The address to connect to
* @param service_or_port The service or port to connect with.
* @param ip_addr_family The IP or service address family as defined in <sys/socket.h>
*/
tcp_client
(
const
std
::
string
&
peer_address_
,
const
std
::
string
&
service_or_port
,
int
ip_addr_family
);
/**
* This descructor will handle the resource deallocation and frees the socket. It also stops the
* event queue and will call the specified destructor_cb_ (if any).
* @brief The descructor
*/
virtual
~
tcp_client
();
/**
* This method sends data over the socket. Keep in mind that this method only enqueues the
* data and requests it's transmission but does not send the data directly. While this ensures
* asynchronous execution you need to check if your data has been send using the is_write_queue_empty
* method.
* @brief Send data down the socket.
* @param data The data to send
*/
virtual
void
write_data
(
const
std
::
string
&
data
);
/**
* Get the connected sockets address. Keep in mind that the lone existance of this address
* does not guaruntee the integrity of the connection.
* @brief Get the connected sockets address
* @return The peers address.
*/
std
::
string
get_peer_address
();
/**
* Use this method in order to retrieve the associated port. A port number of 0 encodes an invalid
* port state. This either means that the choosen protocol familiy does not support the concepts
* of ports or the socket is broken.
* @brief Get the bound port
* @return The port or 0
*/
uint16_t
get_port
();
/**
* Use this method in order to check if all pending write requests have been successfully transmitted.
* This method returns false if the write queue is not empty or the socket is currently transmitting data.
* Note: If the socket connection crashes with an netio exception it might be possible that this
* method returns true even if the last data send has been corrupted.
* @brief Check if all pending data has been send.
* @return true if all pending data has been send.
*/
bool
is_write_queue_empty
();
private:
void
cb_ev
(
::
ev
::
io
&
w
,
int
events
);
void
push_write_queue
(
::
ev
::
io
&
w
);
/**
* This method implements the io queue callback. It is responsible for the actual data transactions
* @param w The io handle to use
* @param events The event flag container
*/
void
cb_ev
(
::
ev
::
io
&
w
,
int
events
);
/**
* This method pushes the front of the write_queue to the socket. It will automatically advance
* the internal buffers and discard empty ones.
* @param w The io handle to use.
*/
void
push_write_queue
(
::
ev
::
io
&
w
);
};
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment