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