Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
Netlib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Daniel Müller
Netlib
Commits
9b6c992b
Commit
9b6c992b
authored
4 years ago
by
Daniel Müller
Browse files
Options
Downloads
Patches
Plain Diff
Add sendAll readAll functions
parent
00d0d2d0
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inc/tcpstream.hpp
+32
-0
32 additions, 0 deletions
inc/tcpstream.hpp
src/tcpstream.cpp
+43
-0
43 additions, 0 deletions
src/tcpstream.cpp
with
75 additions
and
0 deletions
inc/tcpstream.hpp
+
32
−
0
View file @
9b6c992b
...
...
@@ -106,6 +106,19 @@ public:
*/
ssize_t
send
(
const
void
*
data
,
size_t
len
);
/**
* @brief Send exactly len bytes of data into the tcp connection.
*
* If sending fails, an exception is thrown.
*
* @param data Pointer to at least len bytes that will be sent over the
* tcp connection.
* @param len The number of bytes that will be sent over the tcp connection.
*
* @return The number of bytes that were actually sent over the connection.
*/
void
sendAll
(
const
void
*
data
,
size_t
len
);
/**
* @brief Receive a maximum of len bytes of data from the tcp connection. It
* is possible that less than len bytes are received. This call will block
...
...
@@ -123,6 +136,25 @@ public:
*/
ssize_t
read
(
void
*
data
,
size_t
len
);
/**
* @brief Read exactly len bytes from the tcp stream. If the connection is
* closed before len bytes are read, the number of bytes actually read is
* returned. This will block until exactly len bytes are read or the
* connection is closed.
*
* If receiving failed or the connection is closed before at least one
* byte was received, an exception is thrown.
*
* @param data Pointer to at least len bytes where the data received over
* the tcp connection will be stored.
* @param len The number of bytes that will be received over the tcp
* connection.
*
* @return The number of bytes that were actually received over the
* connection. This can be less than len if the connection is closed.
*/
ssize_t
readAll
(
void
*
data
,
size_t
len
);
/**
* @brief Get the socket address of the connection target.
*
...
...
This diff is collapsed.
Click to expand it.
src/tcpstream.cpp
+
43
−
0
View file @
9b6c992b
...
...
@@ -92,6 +92,27 @@ ssize_t TcpStream::send(const void *data, size_t len)
return
bytes_sent
;
}
void
TcpStream
::
sendAll
(
const
void
*
data
,
size_t
len
)
{
if
(
sockfd
==
0
)
throw
std
::
runtime_error
(
"Can't write to closed socket"
);
size_t
bytesSentTotal
=
0
;
while
(
bytesSentTotal
<
len
)
{
ssize_t
bytesSent
=
::
write
(
sockfd
,
(
uint8_t
*
)
data
+
bytesSentTotal
,
len
-
bytesSentTotal
);
if
(
bytesSent
<
0
)
{
close
();
throw
std
::
runtime_error
(
"Error while writing to socket"
);
}
bytesSentTotal
+=
bytesSent
;
}
}
ssize_t
TcpStream
::
read
(
void
*
data
,
size_t
len
)
{
if
(
sockfd
==
0
)
...
...
@@ -106,6 +127,28 @@ ssize_t TcpStream::read(void *data, size_t len)
return
bytes_read
;
}
ssize_t
TcpStream
::
readAll
(
void
*
data
,
size_t
len
)
{
if
(
sockfd
==
0
)
throw
std
::
runtime_error
(
"Can't read from closed socket"
);
size_t
bytesReadTotal
=
0
;
while
(
true
)
{
ssize_t
bytesRead
=
::
read
(
sockfd
,
(
uint8_t
*
)
data
+
bytesReadTotal
,
len
-
bytesReadTotal
);
if
(
bytesRead
==
0
)
break
;
if
(
bytesRead
<
0
)
{
close
();
throw
std
::
runtime_error
(
"Error while reading from socket"
);
}
bytesReadTotal
+=
bytesRead
;
}
return
bytesReadTotal
;
}
const
SockAddr
&
TcpStream
::
getRemoteAddr
()
const
{
return
remote
;
...
...
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