Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
TCP Protocol
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
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
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
BIAS
Receivers
TCP Protocol
Compare revisions
45db714bb522f68f3da6df314815bec6034b4039 to 267288ef3bd4cbf468c9730fba29e4a07402f0b3
Compare revisions
Changes are shown as if the
source
revision was being merged into the
target
revision.
Learn more about comparing revisions.
Source
bias/connection-protocols/tcp-protocol
Select target project
No results found
267288ef3bd4cbf468c9730fba29e4a07402f0b3
Select Git revision
Branches
main
old
Tags
v1.0
Swap
Target
bias/connection-protocols/tcp-protocol
Select target project
bias/connection-protocols/tcp-protocol
1 result
45db714bb522f68f3da6df314815bec6034b4039
Select Git revision
Branches
main
old
Tags
v1.0
Show changes
Only incoming changes from source
Include changes to target since source was created
Compare
Commits on Source (1)
added stop handler
· 267288ef
Valerio Pastore
authored
1 year ago
267288ef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/TCP_Protocol.h
+1
-0
1 addition, 0 deletions
include/TCP_Protocol.h
src/TCP_Protocol.cpp
+35
-6
35 additions, 6 deletions
src/TCP_Protocol.cpp
with
36 additions
and
6 deletions
include/TCP_Protocol.h
View file @
267288ef
...
...
@@ -25,6 +25,7 @@ protected:
int
cli_sock
;
struct
sockaddr_in
servaddr
,
cliaddr
;
bool
checkConnFlag
=
true
;
int
m_connectToCli
(
std
::
string
ip
,
int
port
);
void
resetPacket
(
inaf
::
oasbo
::
PacketLib
::
BasePacket
&
pack
,
int
bytes
);
bool
split_ip_port
(
const
std
::
string
&
ip_port
,
std
::
string
&
ip
,
...
...
This diff is collapsed.
Click to expand it.
src/TCP_Protocol.cpp
View file @
267288ef
...
...
@@ -9,6 +9,10 @@
#include
<ctime>
#include
<iomanip>
#include
<fcntl.h>
#include
<chrono>
#include
<thread>
using
namespace
inaf
::
oasbo
::
Receivers
;
...
...
@@ -66,6 +70,7 @@ int TCPProtocol::connectToClient() {
}
int
TCPProtocol
::
closeConnectionToClient
()
{
this
->
checkConnFlag
=
false
;
if
(
cli_sock
!=
-
1
)
{
::
close
(
cli_sock
);
cli_sock
=
-
1
;
...
...
@@ -86,7 +91,7 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[TCP Receiver]
\t
"
<<
"TCP socket on "
<<
host
<<
" failed"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
)
;
return
-
1
;
}
servaddr
.
sin_family
=
AF_INET
;
servaddr
.
sin_addr
.
s_addr
=
::
inet_addr
(
ip
.
c_str
());
...
...
@@ -99,13 +104,9 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
<<
"]
\t
[TCP Receiver]
\t
"
<<
"TCP CONNECTION on "
<<
host
<<
" Bind failed"
<<
std
::
endl
;
srv_sock
=
-
1
;
exit
(
EXIT_FAILURE
)
;
return
-
1
;
}
}
time_t
now
=
time
(
nullptr
);
std
::
cout
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[TCP Receiver]
\t
"
<<
"Waiting for connection on "
<<
host
<<
std
::
endl
;
if
(
listen
(
srv_sock
,
1
)
<
0
)
{
time_t
now
=
time
(
nullptr
);
...
...
@@ -115,6 +116,34 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
return
-
1
;
}
time_t
now
=
time
(
nullptr
);
std
::
cout
<<
"["
<<
std
::
put_time
(
localtime
(
&
now
),
"%Y-%m-%d %H:%M:%S"
)
<<
"]
\t
[TCP Receiver]
\t
"
<<
"Waiting for connection on "
<<
host
<<
std
::
endl
;
fd_set
read_fds
;
FD_ZERO
(
&
read_fds
);
FD_SET
(
srv_sock
,
&
read_fds
);
struct
timeval
timeout
;
timeout
.
tv_sec
=
5
;
timeout
.
tv_usec
=
0
;
int
select_status
;
while
(
this
->
checkConnFlag
)
{
select_status
=
select
(
srv_sock
+
1
,
&
read_fds
,
NULL
,
NULL
,
&
timeout
);
if
(
select_status
>
0
)
{
break
;
// we have data, we can accept now
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
1
));
// otherwise (i.e. select_status==0) timeout, continue
}
if
(
!
this
->
checkConnFlag
)
{
// Interrupted
closeConnectionToClient
();
return
-
1
;
}
socklen_t
clientAddressSize
=
sizeof
(
cliaddr
);
if
((
cli_sock
=
accept
(
srv_sock
,
(
struct
sockaddr
*
)
&
cliaddr
,
(
socklen_t
*
)
&
clientAddressSize
))
<
0
)
{
...
...
This diff is collapsed.
Click to expand it.