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/udp_socket.h"
00034 #include "simple_message/log_wrapper.h"
00035 #include "simple_message/simple_message.h"
00036 #else
00037 #include "udp_socket.h"
00038 #include "log_wrapper.h"
00039 #include "simple_message.h"
00040 #endif
00041
00042
00043 using namespace industrial::smpl_msg_connection;
00044 using namespace industrial::byte_array;
00045 using namespace industrial::simple_message;
00046 using namespace industrial::shared_types;
00047
00048 namespace industrial
00049 {
00050 namespace udp_socket
00051 {
00052
00053 UdpSocket::UdpSocket()
00054
00055 {
00056 this->setSockHandle(this->SOCKET_FAIL);
00057 memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00058
00059 }
00060
00061 UdpSocket::~UdpSocket()
00062
00063
00064 {
00065 CLOSE(this->getSockHandle());
00066 }
00067
00068 bool UdpSocket::receiveMsg(SimpleMessage & message)
00069 {
00070 ByteArray msgBuffer;
00071
00072 bool rtn = false;
00073 shared_int size = 0;
00074
00075 rtn = this->receiveBytes(msgBuffer, 0);
00076
00077 if (rtn)
00078 {
00079 LOG_DEBUG("Receive message bytes: %u", msgBuffer.getBufferSize());
00080 if (msgBuffer.getBufferSize() >= sizeof(shared_int))
00081 {
00082 LOG_DEBUG("Unloading message length from front of the buffer");
00083 msgBuffer.unloadFront((void*)(&size), sizeof(shared_int));
00084
00085 if ( size != (shared_int) msgBuffer.getBufferSize() )
00086 {
00087 LOG_WARN("readBytes returned a message other than the expected size");
00088 }
00089 rtn = message.init(msgBuffer);
00090
00091 if (rtn)
00092 {
00093 rtn = true;
00094 }
00095 else
00096 {
00097 LOG_ERROR("Failed to initialize message");
00098 rtn = false;
00099 }
00100 }
00101 else
00102 {
00103 LOG_ERROR("Receive bytes returned small: %d message", rtn);
00104 LOG_ERROR("Possible handshake or other connection issue, setting disconnected");
00105 this->setConnected(false);
00106 rtn = false;
00107 }
00108
00109 }
00110 else
00111 {
00112 LOG_ERROR("Failed to receive message");
00113 rtn = false;
00114 }
00115
00116 return rtn;
00117 }
00118
00119 int UdpSocket::rawSendBytes(char *buffer, shared_int num_bytes)
00120 {
00121 int rc = this->SOCKET_FAIL;
00122
00123 rc = SEND_TO(this->getSockHandle(), buffer,
00124 num_bytes, 0, (sockaddr *)&this->sockaddr_,
00125 sizeof(this->sockaddr_));
00126
00127 return rc;
00128 }
00129
00130 int UdpSocket::rawReceiveBytes(char *buffer, shared_int num_bytes)
00131 {
00132 int rc = this->SOCKET_FAIL;
00133 SOCKLEN_T addrSize = 0;
00134
00135 addrSize = sizeof(this->sockaddr_);
00136
00137 rc = RECV_FROM(this->getSockHandle(), &this->buffer_[0], this->MAX_BUFFER_SIZE,
00138 0, (sockaddr *)&this->sockaddr_, &addrSize);
00139
00140 return rc;
00141 }
00142
00143
00144 }
00145 }
00146