Skip to content
GitLab
Explore
Sign in
BIAS
Receivers
TCP Protocol
Compare revisions
b955553fff662a97a49bc1f2169de4aea75a2e4a to 01ae23a804f8b1628a0e3b21e2cb61dd7482d4f0
Commits on Source (3)
adding doxygen
· c0deb5ca
Valerio Pastore
authored
Jan 12, 2024
c0deb5ca
.
· 5e5f967a
Valerio Pastore
authored
Jan 12, 2024
5e5f967a
Merge branch 'dev' into 'main'
· 01ae23a8
Valerio Pastore
authored
Jan 14, 2024
Dev See merge request
!3
01ae23a8
Hide whitespace changes
Inline
Side-by-side
Base-DAQ
@
a00f9a27
Compare
8a0ea2d0
...
a00f9a27
Subproject commit
8
a0
ea2d0e699863df5fe1c91caf2d7b0855957be
Subproject commit a0
0f9a27afbf5f75dab7db2368b9b9b6fcb395e1
include/TCP_Protocol.h
View file @
01ae23a8
#pragma once
#include
<Base_Receiver.h>
#include
<Base_Configurator.h>
/**
* @brief The namespace inaf::oasbo::Receivers contains classes related to receivers in the ASTRI DAQ system.
*/
namespace
inaf
::
oasbo
::
Receivers
{
/**
* @brief The TCPProtocol class is a derived class of BaseReceiver and represents a TCP protocol receiver.
* check the Base_Receiver.h file for more information.
*/
class
TCPProtocol
:
public
BaseReceiver
{
protected:
/**
* @brief Constructs a TCPProtocol object with the specified IP address and port number.
* @param ip The IP address to connect to.
* @param prt The port number to connect to.
*/
TCPProtocol
(
std
::
string
ip
,
int
prt
);
/**
* @brief Constructs a TCPProtocol object with default values for IP address and port number.
*/
TCPProtocol
();
std
::
string
ip
;
int
port
;
int
srv_sock
;
int
cli_sock
;
struct
sockaddr_in
servaddr
,
cliaddr
;
bool
checkConnFlag
=
true
;
std
::
string
ip
;
/**< The IP address to connect to. */
int
port
;
/**< The port number to connect to. */
int
srv_sock
;
/**< The server socket. */
int
cli_sock
;
/**< The client socket. */
struct
sockaddr_in
servaddr
;
/**< The server address. */
struct
sockaddr_in
cliaddr
;
/**< The client address. */
bool
checkConnFlag
=
true
;
/**< Flag that tells if it has to check for incoming connection. Set to false when calling closeConnectionToClient */
/**
* @brief Helper function. Connects to the client with the specified IP address and port number.
* @param ip The IP address to connect to.
* @param port The port number to connect to.
* @return The result of the connection attempt.
*/
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
,
int
&
port
);
/**
* @brief Resets the first bytes of the packet to 0.
* @param pack The packet to reset.
* @param bytes The number of bytes to reset.
*/
void
resetPacket
(
inaf
::
oasbo
::
Packets
::
BasePacket
&
pack
,
int
bytes
);
/**
* @brief Splits the IP address and port number from the specified string.
* @param ip_port The string containing the IP address and port number.
* @param ip The extracted IP address.
* @param port The extracted port number.
* @return True if the split was successful, false otherwise.
*/
bool
split_ip_port
(
const
std
::
string
&
ip_port
,
std
::
string
&
ip
,
int
&
port
);
public:
/**
* @brief Destroys the TCPProtocol object.
*/
~
TCPProtocol
();
/**
* @brief Gets the host name.
* @return The host name.
*/
std
::
string
getHost
()
override
;
/**
* @brief Sets the host name.
* @param host The host name to set.
*/
void
setHost
(
std
::
string
host
)
override
;
/**
* @brief Sets the IP address.
* @param ip The IP address to set.
*/
void
setIp
(
std
::
string
);
std
::
string
getIp
(){
return
this
->
ip
;}
/**
* @brief Gets the IP address.
* @return The IP address.
*/
std
::
string
getIp
()
{
return
this
->
ip
;
}
/**
* @brief Sets the port number.
* @param port The port number to set.
*/
void
setPort
(
int
);
int
getPort
(){
return
this
->
port
;}
/**
* @brief Gets the port number.
* @return The port number.
*/
int
getPort
()
{
return
this
->
port
;
}
int
connectToClient
()
override
;
int
closeConnectionToClient
()
override
;
bool
isConnectedToClient
()
const
override
;
int
receiveFromClient
(
PacketLib
::
BasePacket
&
)
override
;
int
receiveFromClient
(
Packets
::
BasePacket
&
)
override
;
friend
class
TCPProtocolBuilder
;
};
/**
* @brief The TCPProtocolBuilder class is a builder class for TCPProtocol objects.
*/
class
TCPProtocolBuilder
{
protected:
TCPProtocol
*
protocol
;
TCPProtocol
*
protocol
;
/**< The TCPProtocol object being built. */
public:
std
::
string
config_target
{
"tcpreceiver"
};
std
::
string
ip_key
{
"ip"
};
std
::
string
port_key
{
"port"
};
std
::
string
config_target
{
"tcpreceiver"
};
/**< The configuration target. */
std
::
string
ip_key
{
"ip"
};
/**< The IP configuration key. */
std
::
string
port_key
{
"port"
};
/**< The port configuration key. */
/**
* @brief Constructs a TCPProtocolBuilder object.
*/
TCPProtocolBuilder
();
/**
* @brief Constructs a TCPProtocolBuilder object with the specified IP address and port number.
* @param ip The IP address to set.
* @param port The port number to set.
*/
TCPProtocolBuilder
(
std
::
string
ip
,
int
port
);
/**
* @brief Destroys the TCPProtocolBuilder object.
*/
~
TCPProtocolBuilder
();
/**
* @brief Resets the builder to its initial state.
*/
void
reset
();
/**
* @brief Configures the builder from the specified configurator.
* @param conf The configurator to use.
* @return The configured TCPProtocolBuilder object.
*/
TCPProtocolBuilder
*
configFrom
(
Configurators
::
BaseConfigurator
&
conf
);
/**
* @brief Sets the IP address.
* @param ip The IP address to set.
* @return The TCPProtocolBuilder object.
*/
TCPProtocolBuilder
*
setIp
(
std
::
string
ip
);
/**
* @brief Sets the port number.
* @param port The port number to set.
* @return The TCPProtocolBuilder object.
*/
TCPProtocolBuilder
*
setPort
(
int
port
);
/**
* @brief Gets the built TCPProtocol object.
* @return The built TCPProtocol object.
*/
TCPProtocol
*
getReceiver
();
};
}
}
// namespace inaf::oasbo::Receivers
src/TCP_Protocol.cpp
View file @
01ae23a8
...
...
@@ -27,12 +27,12 @@ TCPProtocol::TCPProtocol(std::string ip, int prt) :
TCPProtocol
::~
TCPProtocol
()
{
closeConnectionToClient
();
if
(
srv_sock
!=
-
1
){
if
(
srv_sock
!=
-
1
)
{
::
close
(
srv_sock
);
}
}
int
TCPProtocol
::
receiveFromClient
(
Packet
Lib
::
BasePacket
&
pack
)
{
int
TCPProtocol
::
receiveFromClient
(
Packet
s
::
BasePacket
&
pack
)
{
ssize_t
headerSize
=
pack
.
getHeaderSize
();
uint8_t
*
buff
=
new
uint8_t
[
pack
.
getPacketStructureByteSize
()];
ssize_t
rec
=
0
;
...
...
@@ -56,7 +56,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
to_be_rcv
-=
rec
;
}
if
(
!
this
->
checkConnFlag
){
// the connection has been terminated
if
(
!
this
->
checkConnFlag
)
{
// the connection has been terminated
closeConnectionToClient
();
delete
[]
buff
;
return
0
;
...
...
@@ -64,7 +64,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
// entire header received...
pack
.
copyToMemory
(
buff
,
headerSize
);
if
(
!
pack
.
hasRecognizedHeader
())
{
if
(
!
pack
.
hasRecognizedHeader
())
{
// header not recognized
resetPacket
(
pack
,
headerSize
);
delete
[]
buff
;
return
-
1
;
...
...
@@ -73,7 +73,7 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
// receive the rest of the packet
size_t
payload_and_tail_size
=
pack
.
getPayloadSize
()
+
pack
.
getTailSize
();
to_be_rcv
=
payload_and_tail_size
;
while
(
this
->
checkConnFlag
&&
to_be_rcv
>
0
)
{
// go ahead until the rest
o
of the packet received or connection terminates
while
(
this
->
checkConnFlag
&&
to_be_rcv
>
0
)
{
// go ahead until the rest of the packet received or connection terminates
rec
=
::
recv
(
this
->
cli_sock
,
&
buff
[
headerSize
],
to_be_rcv
,
MSG_DONTWAIT
);
if
(
rec
==
0
)
{
// connection closed
...
...
@@ -91,14 +91,13 @@ int TCPProtocol::receiveFromClient(PacketLib::BasePacket &pack) {
to_be_rcv
-=
rec
;
}
if
(
!
this
->
checkConnFlag
){
// the connection has been terminated
if
(
!
this
->
checkConnFlag
)
{
// the connection has been terminated
closeConnectionToClient
();
delete
[]
buff
;
return
0
;
}
pack
.
copyToMemory
(
&
buff
[
headerSize
],
payload_and_tail_size
,
headerSize
);
pack
.
copyToMemory
(
&
buff
[
headerSize
],
payload_and_tail_size
,
headerSize
);
delete
[]
buff
;
return
headerSize
+
payload_and_tail_size
;
...
...
@@ -197,7 +196,7 @@ int TCPProtocol::m_connectToCli(std::string ip, int port) {
return
cli_sock
;
}
void
TCPProtocol
::
resetPacket
(
Packet
Lib
::
BasePacket
&
pack
,
int
bytes
)
{
void
TCPProtocol
::
resetPacket
(
Packet
s
::
BasePacket
&
pack
,
int
bytes
)
{
uint8_t
*
buff
=
new
uint8_t
[
bytes
];
std
::
memset
(
buff
,
0
,
bytes
);
int
toBeReset
=
std
::
min
(
...
...