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 #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 // Constructor for UDP socket object
00055 {
00056   this->setSockHandle(this->SOCKET_FAIL);
00057   memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00058 
00059 }
00060 
00061 UdpSocket::~UdpSocket()
00062 // Destructor for UDP socket object
00063 // Closes socket
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 } //udp_socket
00145 } //industrial
00146 


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Aug 28 2015 11:11:56