tcp_socket.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Southwest Research Institute
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * * Neither the name of the Southwest Research Institute, nor the names
16  * of its contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef FLATHEADERS
37 #else
38 #include "tcp_socket.h"
39 #include "log_wrapper.h"
40 #include "simple_message.h"
41 #include "shared_types.h"
42 #endif
43 
44 using namespace industrial::smpl_msg_connection;
45 using namespace industrial::byte_array;
46 using namespace industrial::simple_message;
47 using namespace industrial::shared_types;
48 
49 namespace industrial
50 {
51 namespace tcp_socket
52 {
53 
54 TcpSocket::TcpSocket()
55 {
56 }
57 
58 TcpSocket::~TcpSocket()
59 // Closes socket
60 {
61  LOG_DEBUG("Destructing TCPSocket");
62  CLOSE(this->getSockHandle());
63 }
64 
65 int TcpSocket::rawSendBytes(char *buffer, shared_int num_bytes)
66 {
67  int rc = this->SOCKET_FAIL;
68 
69  rc = SEND(this->getSockHandle(), buffer, num_bytes, 0);
70 
71  return rc;
72 }
73 
74 int TcpSocket::rawReceiveBytes(char *buffer, shared_int num_bytes)
75 {
76  int rc = this->SOCKET_FAIL;
77 
78  rc = RECV(this->getSockHandle(), buffer, num_bytes, 0);
79 
80  return rc;
81 }
82 
83 bool TcpSocket::rawPoll(int timeout, bool & ready, bool & error)
84 {
85  timeval time;
86  fd_set read, write, except;
87  int rc = this->SOCKET_FAIL;
88  bool rtn = false;
89  ready = false;
90  error = false;
91 
92  // The select function uses the timeval data structure
93  time.tv_sec = timeout / 1000;
94  time.tv_usec = (timeout % 1000) * 1000;
95 
96  FD_ZERO(&read);
97  FD_ZERO(&write);
98  FD_ZERO(&except);
99 
100  FD_SET(this->getSockHandle(), &read);
101  FD_SET(this->getSockHandle(), &except);
102 
103  rc = SELECT(this->getSockHandle() + 1, &read, &write, &except, &time);
104 
105  if (this->SOCKET_FAIL != rc) {
106  if (0 == rc)
107  rtn = false;
108  else {
109  if (FD_ISSET(this->getSockHandle(), &read)) {
110  ready = true;
111  rtn = true;
112  }
113  else if(FD_ISSET(this->getSockHandle(), &except)) {
114  error = true;
115  rtn = true;
116  }
117  else {
118  LOG_WARN("Select returned, but no flags are set");
119  rtn = false;
120  }
121  }
122  } else {
123  this->logSocketError("Socket select function failed", rc, errno);
124  rtn = false;
125  }
126  return rtn;
127 }
128 
129 } //tcp_socket
130 } //industrial
131 
LOG_WARN
#define LOG_WARN(format,...)
Definition: log_wrapper.h:133
industrial::shared_types
Contains platform specific type definitions that guarantee the size of primitive data types.
Definition: shared_types.h:52
industrial
Definition: byte_array.h:45
simple_message.h
industrial::smpl_msg_connection
Definition: smpl_msg_connection.h:48
LOG_DEBUG
#define LOG_DEBUG(format,...)
Definition: log_wrapper.h:131
industrial::simple_message
Definition: simple_message.h:49
shared_types.h
log_wrapper.h
industrial::shared_types::shared_int
int shared_int
Definition: shared_types.h:62
industrial::byte_array
Definition: byte_array.h:56
tcp_socket.h


simple_message
Author(s): Shaun Edwards
autogenerated on Wed Mar 2 2022 00:24:53