Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef ROS
00033 #include "simple_message/socket/simple_socket.h"
00034 #include "simple_message/log_wrapper.h"
00035 #endif
00036
00037 #ifdef MOTOPLUS
00038 #include "simple_socket.h"
00039 #include "log_wrapper.h"
00040 #endif
00041
00042
00043 using namespace industrial::byte_array;
00044 using namespace industrial::shared_types;
00045
00046 namespace industrial
00047 {
00048 namespace simple_socket
00049 {
00050
00051
00052 bool SimpleSocket::sendBytes(ByteArray & buffer)
00053 {
00054 int rc = this->SOCKET_FAIL;
00055 bool rtn = false;
00056
00057 if (this->isConnected())
00058 {
00059
00060
00061 if (this->MAX_BUFFER_SIZE > (int)buffer.getBufferSize())
00062 {
00063
00064 rc = rawSendBytes(buffer.getRawDataPtr(), buffer.getBufferSize());
00065 if (this->SOCKET_FAIL != rc)
00066 {
00067 rtn = true;
00068 }
00069 else
00070 {
00071 rtn = false;
00072 logSocketError("Socket sendBytes failed", rc);
00073 }
00074
00075 }
00076 else
00077 {
00078 LOG_ERROR("Buffer size: %u, is greater than max socket size: %u", buffer.getBufferSize(), this->MAX_BUFFER_SIZE);
00079 rtn = false;
00080 }
00081
00082 }
00083 else
00084 {
00085 rtn = false;
00086 LOG_WARN("Not connected, bytes not sent");
00087 }
00088
00089 if (!rtn)
00090 {
00091 this->setConnected(false);
00092 }
00093
00094 return rtn;
00095
00096 }
00097
00098 bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes)
00099 {
00100 int rc = this->SOCKET_FAIL;
00101 bool rtn = false;
00102
00103
00104
00105
00106
00107 memset(&this->buffer_, 0, sizeof(this->buffer_));
00108
00109
00110
00111
00112 if (this->MAX_BUFFER_SIZE < (int)buffer.getMaxBufferSize())
00113 {
00114 LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u",
00115 this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize());
00116 }
00117 if (this->isConnected())
00118 {
00119 rc = rawReceiveBytes(this->buffer_, num_bytes);
00120
00121 if (this->SOCKET_FAIL != rc)
00122 {
00123 if (rc > 0)
00124 {
00125 LOG_COMM("Byte array receive, bytes read: %u", rc);
00126 buffer.init(&this->buffer_[0], rc);
00127 rtn = true;
00128 }
00129 else
00130 {
00131 LOG_WARN("Recieved zero bytes: %u", rc);
00132 rtn = false;
00133 }
00134 }
00135 else
00136 {
00137 this->logSocketError("Socket received failed", rc);
00138 rtn = false;
00139 }
00140 }
00141 else
00142 {
00143 rtn = false;
00144 LOG_WARN("Not connected, bytes not sent");
00145 }
00146
00147 if (!rtn)
00148 {
00149 this->setConnected(false);
00150 }
00151 return rtn;
00152 }
00153
00154 bool SimpleSocket::isReadyReceive(int timeout)
00155 {
00156 timeval time;
00157 fd_set read, write, except;
00158 int rc = this->SOCKET_FAIL;
00159 bool rtn = false;
00160
00161
00162 time.tv_sec = timeout/1000;
00163 time.tv_usec = (timeout%1000)*1000;
00164
00165 FD_ZERO(&read);
00166 FD_ZERO(&write);
00167 FD_ZERO(&except);
00168
00169 FD_SET(this->getSockHandle(), &read);
00170
00171 rc = SELECT(this->getSockHandle() + 1, &read, &write, &except, &time);
00172
00173 if (this->SOCKET_FAIL != rc)
00174 {
00175 if (0==rc)
00176 {
00177 LOG_DEBUG("Socket select timed out");
00178 rtn = false;
00179 }
00180 else
00181 {
00182 LOG_DEBUG("Data is ready for reading");
00183 rtn = true;
00184 }
00185 }
00186 else
00187 {
00188 this->logSocketError("Socket select function failed", rc);
00189 rtn = false;
00190 }
00191
00192 return rtn;
00193 }
00194
00195
00196
00197 }
00198 }
00199