24 #ifdef TGT_OS_TYPE_VXWORKS 36 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN 37 #include <arpa/inet.h> 38 #include <sys/socket.h> 42 #error OS not supported for IP Socket Communications 53 const U32 timeout_seconds,
54 const U32 timeout_microseconds) {
55 FW_ASSERT(timeout_microseconds < 1000000, static_cast<FwAssertArgType>(timeout_microseconds));
77 #ifdef TGT_OS_TYPE_VXWORKS 81 struct timeval timeout;
85 if (setsockopt(socketFd, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<char*>(&timeout),
sizeof(timeout)) < 0) {
97 (void)::memset(out, 0,
sizeof(
struct in_addr));
99 #ifdef TGT_OS_TYPE_VXWORKS 100 int ip = inet_addr(ipv4_address);
106 *
static_cast<unsigned long*
>(out) = static_cast<unsigned long>(ip);
112 if (::inet_pton(AF_INET, ipv4_address, out) != 1) {
120 (void)::
close(socketDescriptor.
fd);
125 int status =
::shutdown(socketDescriptor.
fd, SHUT_RDWR);
128 this->
close(socketDescriptor);
138 socketDescriptor.
fd = -1;
154 sent = this->
sendProtocol(socketDescriptor, data + total, size - total);
156 if (((sent == -1) && (errno == EINTR)) || (sent == 0)) {
160 else if ((sent == -1) && ((errno == EBADF) || (errno == ECONNRESET))) {
164 else if (sent == -1) {
167 FW_ASSERT(sent > 0, static_cast<FwAssertArgType>(sent));
175 FW_ASSERT(total == size, static_cast<FwAssertArgType>(total), static_cast<FwAssertArgType>(size));
192 bytes_received_or_status = this->
recvProtocol(socketDescriptor, data, req_read);
194 if (bytes_received_or_status > 0) {
196 req_read =
static_cast<FwSizeType>(bytes_received_or_status);
198 }
else if (bytes_received_or_status == 0) {
203 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
207 }
else if ((errno == ECONNRESET) || (errno == EBADF)) {
234 status = setsockopt(socketFd, options.level, options.option, &options.value.intVal,
235 sizeof(options.value.intVal));
237 status = setsockopt(socketFd, options.level, options.option, &options.value.sizeVal,
238 sizeof(options.value.sizeVal));
Failed to send after configured retries.
virtual bool isValidPort(U16 port)
Check if the given port is valid for the socket.
Failed to read socket with disconnect.
Interrupted status for retries.
Failed to configure socket.
PlatformSizeType FwSizeType
SocketIpStatus recv(const SocketDescriptor &fd, U8 *const data, FwSizeType &size)
receive data from the IP socket from the given buffer
SocketIpStatus setupTimeouts(int socketFd)
setup the socket timeout properties of the opened outgoing socket
virtual SocketIpStatus configure(const char *const ipv4_address, const U16 port, const U32 send_timeout_seconds, const U32 send_timeout_microseconds)
configure the ip socket with an IPv4 address and transmission timeouts
static SocketIpStatus addressToIp4(const char *const ipv4_address, void *const out)
converts a given IPv4 address in dotted-quad form "x.x.x.x" to a network-order in_addr structure...
int fd
Used for all sockets to track the communication file descriptor.
PlatformSignedSizeType FwSignedSizeType
virtual SocketIpStatus send(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)
send data out the IP socket from the given buffer
virtual SocketIpStatus openProtocol(SocketDescriptor &fd)=0
Protocol specific open implementation, called from open.
void shutdown(const SocketDescriptor &socketDescriptor)
shutdown the socket
virtual FwSignedSizeType sendProtocol(const SocketDescriptor &socketDescriptor, const U8 *const data, const FwSizeType size)=0
Protocol specific implementation of send. Called directly with retry from send.
U16 m_port
IP address port used.
Socket operation successful.
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
SocketIpStatus setupSocketOptions(int socketFd)
setup the socket options of the input socket as defined in IpCfg.hpp
uint8_t U8
8-bit unsigned integer
static const IpSocketOptions IP_SOCKET_OPTIONS[]
virtual SocketIpStatus handleZeroReturn()
Handle zero return from recvProtocol.
char m_ipv4_address[SOCKET_MAX_IPV4_ADDRESS_SIZE]
IPv4 address (dotted-quad "x.x.x.x")
void close(const SocketDescriptor &socketDescriptor)
closes the socket
virtual FwSignedSizeType recvProtocol(const SocketDescriptor &socketDescriptor, U8 *const data, const FwSizeType size)=0
Protocol specific implementation of recv. Called directly with error handling from recv...
SocketIpStatus open(SocketDescriptor &socketDescriptor)
open the IP socket for communications
SocketIpStatus
Status enumeration for socket return values.
U32 m_timeoutMicroseconds
No data available or read operation would block.
FwSizeType string_length(const CHAR *source, FwSizeType buffer_size)
get the length of the source string