simple_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/simple_socket.h"
00034 #include "simple_message/log_wrapper.h"
00035 #endif
00036 
00037 #ifdef MOTOPLUS
00038 #include "simple_socket.h"
00039 #include "log_wrapper.h"
00040 #endif
00041 
00042 
00043 using namespace industrial::byte_array;
00044 using namespace industrial::shared_types;
00045 
00046 namespace industrial
00047 {
00048 namespace simple_socket
00049 {
00050 
00051 
00052 bool SimpleSocket::sendBytes(ByteArray & buffer)
00053 {
00054   int rc = this->SOCKET_FAIL;
00055   bool rtn = false;
00056 
00057   if (this->isConnected())
00058   {
00059     // Nothing restricts the ByteArray from being larger than the what the socket
00060     // can handle.
00061     if (this->MAX_BUFFER_SIZE > (int)buffer.getBufferSize())
00062     {
00063     
00064       rc = rawSendBytes(buffer.getRawDataPtr(), buffer.getBufferSize());
00065       if (this->SOCKET_FAIL != rc)
00066       {
00067         rtn = true;
00068       }
00069       else
00070       {
00071         rtn = false;
00072         logSocketError("Socket sendBytes failed", rc);
00073       }
00074       
00075       }
00076     else
00077     {
00078       LOG_ERROR("Buffer size: %u, is greater than max socket size: %u", buffer.getBufferSize(), this->MAX_BUFFER_SIZE);
00079       rtn = false;
00080     }
00081 
00082   }
00083   else
00084   {
00085     rtn = false;
00086     LOG_WARN("Not connected, bytes not sent");
00087   }
00088 
00089   if (!rtn)
00090     {
00091       this->setConnected(false);
00092     }
00093 
00094   return rtn;
00095   
00096 }
00097 
00098 bool SimpleSocket::receiveBytes(ByteArray & buffer, shared_int num_bytes)
00099 {
00100   int rc = this->SOCKET_FAIL;
00101   bool rtn = false;
00102   
00103   // Reset the buffer (this is not required since the buffer length should
00104   // ensure that we don't read any of the garbage that may be left over from
00105   // a previous read), but it is good practice.
00106 
00107   memset(&this->buffer_, 0, sizeof(this->buffer_));
00108 
00109   // Doing a sanity check to determine if the byte array buffer is larger than
00110   // what can be sent in the socket.  This should not happen and might be indicative
00111   // of some code synchronization issues between the client and server base.
00112   if (this->MAX_BUFFER_SIZE < (int)buffer.getMaxBufferSize())
00113   {
00114     LOG_WARN("Socket buffer max size: %u, is larger than byte array buffer: %u",
00115              this->MAX_BUFFER_SIZE, buffer.getMaxBufferSize());
00116   }
00117   if (this->isConnected())
00118   {
00119     rc = rawReceiveBytes(this->buffer_, num_bytes);
00120 
00121     if (this->SOCKET_FAIL != rc)
00122     {
00123       if (rc > 0)
00124       {
00125         LOG_COMM("Byte array receive, bytes read: %u", rc);
00126         buffer.init(&this->buffer_[0], rc);
00127         rtn = true;
00128       }
00129       else
00130       {
00131         LOG_WARN("Recieved zero bytes: %u", rc);
00132         rtn = false;
00133       }
00134     }
00135     else
00136     {
00137       this->logSocketError("Socket received failed", rc);
00138       rtn = false;
00139     }
00140   }
00141   else
00142   {
00143     rtn = false;
00144     LOG_WARN("Not connected, bytes not sent");
00145   }
00146 
00147   if (!rtn)
00148   {
00149     this->setConnected(false);
00150   }
00151   return rtn;
00152 }
00153 
00154 bool SimpleSocket::isReadyReceive(int timeout)
00155 {
00156   timeval time;
00157   fd_set read, write, except;
00158   int rc = this->SOCKET_FAIL;
00159   bool rtn = false;
00160   
00161   // The select function uses the timeval data structure
00162   time.tv_sec = timeout/1000;
00163   time.tv_usec = (timeout%1000)*1000;
00164   
00165   FD_ZERO(&read);
00166   FD_ZERO(&write);
00167   FD_ZERO(&except);
00168   
00169   FD_SET(this->getSockHandle(), &read);
00170   
00171   rc = SELECT(this->getSockHandle() + 1, &read, &write, &except, &time);
00172   
00173   if (this->SOCKET_FAIL != rc)
00174   {
00175     if (0==rc)
00176     {
00177       LOG_DEBUG("Socket select timed out");
00178       rtn = false;
00179     }
00180     else
00181     {
00182       LOG_DEBUG("Data is ready for reading");
00183       rtn = true;
00184     }
00185   }
00186   else
00187   {
00188     this->logSocketError("Socket select function failed", rc);
00189     rtn = false;
00190   }
00191   
00192   return rtn;
00193 }
00194 
00195 
00196 
00197 } //simple_socket
00198 } //industrial
00199 


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Jan 3 2014 11:26:56