19 #ifdef TGT_OS_TYPE_VXWORKS
32 #elif defined TGT_OS_TYPE_LINUX || TGT_OS_TYPE_DARWIN
33 #include <sys/socket.h>
37 #include <arpa/inet.h>
39 #error OS not supported for IP Socket Communications
59 ,m_timeoutMicroseconds(0)
72 const U32 timeout_seconds,
73 const U32 timeout_microseconds
75 this->m_sendUdp = send_udp;
76 this->m_timeoutSeconds = timeout_seconds;
77 this->m_timeoutSeconds = timeout_seconds;
87 return (-1 != this->m_socketInFd);
91 if (this->m_socketInFd != -1) {
92 (void) ::
close(this->m_socketInFd);
94 this->m_socketInFd = -1;
95 this->m_socketOutFd = -1;
102 if (this->m_socketInFd != -1) {
103 (void) ::
close(this->m_socketInFd);
105 this->m_socketInFd = -1;
107 status = this->openProtocol(SOCK_STREAM);
112 if (this->m_sendUdp && this->m_socketOutFd == -1 && (status = this->openProtocol(SOCK_DGRAM,
false)) !=
SOCK_SUCCESS) {
113 (void) ::
close(this->m_socketInFd);
117 else if (!this->m_sendUdp) {
118 this->m_socketOutFd = this->m_socketInFd;
130 struct sockaddr_in address;
133 if (isInput and this->m_socketInFd != -1) {
134 (void) ::
close(this->m_socketInFd);
135 this->m_socketInFd = -1;
136 }
else if (not isInput and this->m_socketOutFd != -1) {
137 (void) ::
close(this->m_socketOutFd);
138 this->m_socketOutFd = -1;
141 if ((socketFd = ::socket(AF_INET, protocol, 0)) == -1) {
145 address.sin_family = AF_INET;
146 address.sin_port = htons(this->m_port);
147 #if defined TGT_OS_TYPE_VXWORKS || TGT_OS_TYPE_DARWIN
148 address.sin_len =
static_cast<U8>(
sizeof(
struct sockaddr_in));
151 #ifdef TGT_OS_TYPE_VXWORKS
152 address.sin_addr.s_addr = inet_addr(this->m_hostname);
155 struct timeval timeout;
156 timeout.tv_sec = this->m_timeoutSeconds;
157 timeout.tv_usec = this->m_timeoutMicroseconds;
159 if (setsockopt(socketFd, SOL_SOCKET, SO_SNDTIMEO, (
char *)&timeout,
sizeof(timeout)) < 0) {
160 (void) ::
close(socketFd);
164 struct hostent *host_entry;
165 if ((host_entry = gethostbyname(this->m_hostname)) ==
NULL || host_entry->h_addr_list[0] ==
NULL) {
170 if (inet_pton(address.sin_family, m_hostname, &(address.sin_addr)) < 0) {
176 if (protocol != SOCK_DGRAM && connect(socketFd,
reinterpret_cast<struct sockaddr *
>(&address),
177 sizeof(address)) < 0) {
182 else if (protocol == SOCK_DGRAM) {
183 FW_ASSERT(
sizeof(this->m_state->
m_udpAddr) ==
sizeof(address),
sizeof(this->m_state->m_udpAddr),
sizeof(address));
184 memcpy(&this->m_state->
m_udpAddr, &address,
sizeof(this->m_state->m_udpAddr));
189 this->m_socketInFd = socketFd;
191 this->m_socketOutFd = socketFd;
193 Fw::Logger::logMsg(
"Connected to %s:%hu for %s using %s\n",
reinterpret_cast<POINTER_CAST
>(m_hostname), m_port,
194 reinterpret_cast<POINTER_CAST
>(isInput ?
"uplink" :
"downlink"),
195 reinterpret_cast<POINTER_CAST
>((protocol == SOCK_DGRAM) ?
"udp" :
"tcp"));
204 if (this->m_socketOutFd == -1) {
211 if (this->m_sendUdp) {
212 sent = ::sendto(this->m_socketOutFd, data + total, size - total,
SOCKET_SEND_FLAGS,
213 reinterpret_cast<struct sockaddr *
>(&this->m_state->
m_udpAddr),
sizeof(this->m_state->m_udpAddr));
220 if (sent == -1 && errno == EINTR) {
224 else if (sent == -1 && errno == EBADF) {
227 this->m_socketOutFd = -1;
231 else if (sent == -1 && errno != EINTR) {
249 if (size == -1 && errno != EINTR && errno != ECONNRESET) {
254 else if (size == -1 && errno == EINTR) {
258 else if (size == 0 || errno == ECONNRESET) {