simple_socket.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Yaskawa America, Inc.
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 Yaskawa America, Inc., 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
35 #else
36 #include "simple_socket.h"
37 #include "log_wrapper.h"
38 #endif
39 
40 using namespace industrial::byte_array;
41 using namespace industrial::shared_types;
42 
43 namespace industrial
44 {
45  namespace simple_socket
46  {
47 
48  bool SimpleSocket::sendBytes(ByteArray & buffer)
49  {
50  int rc = this->SOCKET_FAIL;
51  bool rtn = false;
52 
53  if (this->isConnected())
54  {
55  // Nothing restricts the ByteArray from being larger than the what the socket
56  // can handle.
57  if (this->MAX_BUFFER_SIZE > (int)buffer.getBufferSize())
58  {
59 
60  // copy to local array, since ByteArray no longer supports
61  // direct pointer-access to data values
62  std::vector<char> localBuffer;
63  buffer.copyTo(localBuffer);
64  rc = rawSendBytes(&localBuffer[0], localBuffer.size());
65  if (this->SOCKET_FAIL != rc)
66  {
67  rtn = true;
68  }
69  else
70  {
71  rtn = false;
72  logSocketError("Socket sendBytes failed", rc, errno);
73  }
74 
75  }
76  else
77  {
78  LOG_ERROR("Buffer size: %u, is greater than max socket size: %u", buffer.getBufferSize(), this->MAX_BUFFER_SIZE);
79  rtn = false;
80  }
81 
82  }
83  else
84  {
85  rtn = false;
86  LOG_WARN("Not connected, bytes not sent");
87  }
88 
89  if (!rtn)
90  {
91  this->setConnected(false);
92  }
93 
94  return rtn;
95 
96  }
97 
98  bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes)
99  {
100  int rc = this->SOCKET_FAIL;
101  bool rtn = false;
102  shared_int remainBytes = num_bytes;
103  bool ready, error;
104 
105  // Reset the buffer (this is not required since the buffer length should
106  // ensure that we don't read any of the garbage that may be left over from
107  // a previous read), but it is good practice.
108 
109  memset(&this->buffer_, 0, sizeof(this->buffer_));
110 
111  // Doing a sanity check to determine if the byte array buffer is smaller than
112  // what can be received by the socket.
113  if (this->MAX_BUFFER_SIZE > buffer.getMaxBufferSize())
114  {
115  LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u",
116  this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize());
117  }
118  if (this->isConnected())
119  {
120  buffer.init();
121  while (remainBytes > 0)
122  {
123  // Polling the socket results in an "interruptable" socket read. This
124  // allows Control-C to break out of a socket read. Without polling,
125  // a sig-term is required to kill a program in a socket read function.
126  if (this->rawPoll(this->SOCKET_POLL_TO, ready, error))
127  {
128  if(ready)
129  {
130  rc = rawReceiveBytes(this->buffer_, remainBytes);
131  if (this->SOCKET_FAIL == rc)
132  {
133  this->logSocketError("Socket received failed", rc, errno);
134  remainBytes = 0;
135  rtn = false;
136  break;
137  }
138  else if (0 == rc)
139  {
140  LOG_WARN("Recieved zero bytes: %u", rc);
141  remainBytes = 0;
142  rtn = false;
143  break;
144  }
145  else
146  {
147  remainBytes = remainBytes - rc;
148  LOG_COMM("Byte array receive, bytes read: %u, bytes reqd: %u, bytes left: %u",
149  rc, num_bytes, remainBytes);
150  buffer.load(&this->buffer_, rc);
151  rtn = true;
152  }
153  }
154  else if(error)
155  {
156  LOG_ERROR("Socket poll returned an error");
157  rtn = false;
158  break;
159  }
160  else
161  {
162  LOG_ERROR("Uknown error from socket poll");
163  rtn = false;
164  break;
165  }
166  }
167  else
168  {
169  LOG_COMM("Socket poll timeout, trying again");
170  }
171  }
172  }
173  else
174  {
175  LOG_WARN("Not connected, bytes not sent");
176  rtn = false;
177  }
178 
179  if (!rtn)
180  {
181  this->setConnected(false);
182  }
183  return rtn;
184  }
185 
186  } //simple_socket
187 } //industrial
188 
Contains platform specific type definitions that guarantee the size of primitive data types...
Definition: shared_types.h:52
void init()
Initializes or Reinitializes an empty buffer.
Definition: byte_array.cpp:62
#define LOG_WARN(format,...)
Definition: log_wrapper.h:107
#define LOG_COMM(format,...)
Definition: log_wrapper.h:104
bool load(industrial::shared_types::shared_bool value)
loads a boolean into the byte array
Definition: byte_array.cpp:142
#define LOG_ERROR(format,...)
Definition: log_wrapper.h:108
The byte array wraps a dynamic array of bytes (i.e. char).
Definition: byte_array.h:80
unsigned int getMaxBufferSize()
gets current buffer size
Definition: byte_array.cpp:392
void copyTo(std::vector< char > &out)
Copy to std::vector, for raw-ptr access.
Definition: byte_array.cpp:98
unsigned int getBufferSize()
gets current buffer size
Definition: byte_array.cpp:387


simple_message
Author(s): Shaun Edwards
autogenerated on Sat Sep 21 2019 03:30:09