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 #ifndef FLATHEADERS
00033 #include "simple_message/socket/simple_socket.h"
00034 #include "simple_message/log_wrapper.h"
00035 #else
00036 #include "simple_socket.h"
00037 #include "log_wrapper.h"
00038 #endif
00039
00040 using namespace industrial::byte_array;
00041 using namespace industrial::shared_types;
00042
00043 namespace industrial
00044 {
00045 namespace simple_socket
00046 {
00047
00048 bool SimpleSocket::sendBytes(ByteArray & buffer)
00049 {
00050 int rc = this->SOCKET_FAIL;
00051 bool rtn = false;
00052
00053 if (this->isConnected())
00054 {
00055
00056
00057 if (this->MAX_BUFFER_SIZE > (int)buffer.getBufferSize())
00058 {
00059
00060 rc = rawSendBytes(buffer.getRawDataPtr(), buffer.getBufferSize());
00061 if (this->SOCKET_FAIL != rc)
00062 {
00063 rtn = true;
00064 }
00065 else
00066 {
00067 rtn = false;
00068 logSocketError("Socket sendBytes failed", rc, errno);
00069 }
00070
00071 }
00072 else
00073 {
00074 LOG_ERROR("Buffer size: %u, is greater than max socket size: %u", buffer.getBufferSize(), this->MAX_BUFFER_SIZE);
00075 rtn = false;
00076 }
00077
00078 }
00079 else
00080 {
00081 rtn = false;
00082 LOG_WARN("Not connected, bytes not sent");
00083 }
00084
00085 if (!rtn)
00086 {
00087 this->setConnected(false);
00088 }
00089
00090 return rtn;
00091
00092 }
00093
00094 bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes)
00095 {
00096 int rc = this->SOCKET_FAIL;
00097 bool rtn = false;
00098 shared_int remainBytes = num_bytes;
00099 bool ready, error;
00100
00101
00102
00103
00104
00105 memset(&this->buffer_, 0, sizeof(this->buffer_));
00106
00107
00108
00109
00110 if (this->MAX_BUFFER_SIZE < (int)buffer.getMaxBufferSize())
00111 {
00112 LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u",
00113 this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize());
00114 }
00115 if (this->isConnected())
00116 {
00117 buffer.init();
00118 while (remainBytes > 0)
00119 {
00120
00121
00122
00123 if (this->rawPoll(this->SOCKET_POLL_TO, ready, error))
00124 {
00125 if(ready)
00126 {
00127 rc = rawReceiveBytes(this->buffer_, remainBytes);
00128 if (this->SOCKET_FAIL == rc)
00129 {
00130 this->logSocketError("Socket received failed", rc, errno);
00131 remainBytes = 0;
00132 rtn = false;
00133 break;
00134 }
00135 else if (0 == rc)
00136 {
00137 LOG_WARN("Recieved zero bytes: %u", rc);
00138 remainBytes = 0;
00139 rtn = false;
00140 break;
00141 }
00142 else
00143 {
00144 remainBytes = remainBytes - rc;
00145 LOG_COMM("Byte array receive, bytes read: %u, bytes reqd: %u, bytes left: %u",
00146 rc, num_bytes, remainBytes);
00147 buffer.load(&this->buffer_, rc);
00148 rtn = true;
00149 }
00150 }
00151 else if(error)
00152 {
00153 LOG_ERROR("Socket poll returned an error");
00154 rtn = false;
00155 break;
00156 }
00157 else
00158 {
00159 LOG_ERROR("Uknown error from socket poll");
00160 rtn = false;
00161 break;
00162 }
00163 }
00164 else
00165 {
00166 LOG_COMM("Socket poll timeout, trying again");
00167 }
00168 }
00169 }
00170 else
00171 {
00172 LOG_WARN("Not connected, bytes not sent");
00173 rtn = false;
00174 }
00175
00176 if (!rtn)
00177 {
00178 this->setConnected(false);
00179 }
00180 return rtn;
00181 }
00182
00183 }
00184 }
00185