udp_socket.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Yaskawa America, Inc.
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *       * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *       * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *       * Neither the name of the Yaskawa America, Inc., nor the names
00016  *       of its contributors may be used to endorse or promote products derived
00017  *       from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
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 // Constructor for UDP socket object
00057 {
00058   this->setSockHandle(this->SOCKET_FAIL);
00059   memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00060 
00061 }
00062 
00063 UdpSocket::~UdpSocket()
00064 // Destructor for UDP socket object
00065 // Closes socket
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 } //udp_socket
00147 } //industrial
00148 


simple_message
Author(s): Shaun Edwards
autogenerated on Mon Oct 6 2014 00:54:18