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   memset(&this->udp_read_buffer_, 0, sizeof(this->udp_read_buffer_));
00057   udp_read_head_ = this->udp_read_buffer_;
00058   udp_read_len_ = 0;
00059 }
00060 
00061 UdpSocket::~UdpSocket()
00062 // Destructor for UDP socket object
00063 // Closes socket
00064 {
00065   CLOSE(this->getSockHandle());
00066 }
00067 
00068 int UdpSocket::rawSendBytes(char *buffer, shared_int num_bytes)
00069 {
00070   int rc = this->SOCKET_FAIL;
00071 
00072   rc = SEND_TO(this->getSockHandle(), buffer,
00073         num_bytes, 0, (sockaddr *)&this->sockaddr_,
00074         sizeof(this->sockaddr_));
00075   
00076   return rc;
00077 }
00078 
00079 int UdpSocket::rawReceiveBytes(char *buffer, shared_int num_bytes)
00080 {
00081   int rc, len_cpy;
00082   SOCKLEN_T addrSize;
00083 
00084   if(udp_read_len_ == 0) {
00085     // there is currently no data in the temporary buffer, do a socket read
00086     addrSize = sizeof(this->sockaddr_);
00087 
00088     rc = RECV_FROM(this->getSockHandle(), &this->udp_read_buffer_[0], this->MAX_BUFFER_SIZE,
00089         0, (sockaddr *)&this->sockaddr_, &addrSize);
00090     if(rc <= 0)
00091       return 0; // either we had an error or read no data, don't update the buffer
00092     udp_read_head_ = this->udp_read_buffer_;
00093     udp_read_len_ = rc;
00094   }
00095   if(num_bytes == 0 || num_bytes >= udp_read_len_) // read all data available
00096     len_cpy = udp_read_len_;
00097   else
00098     len_cpy = num_bytes;
00099   memcpy(buffer, udp_read_head_, len_cpy);
00100   udp_read_head_ += len_cpy; // shift pointer in buffer
00101   udp_read_len_ -= len_cpy;
00102   return len_cpy;
00103 }
00104 
00105 bool UdpSocket::rawPoll(int timeout, bool & ready, bool & error)
00106 {
00107   if(udp_read_len_ > 0) {
00108     // we still have data in the buffer, we can read without socket calls
00109     ready = true;
00110     error = false;
00111     return true;
00112   }
00113 
00114   timeval time;
00115   fd_set read, write, except;
00116   int rc = this->SOCKET_FAIL;
00117   bool rtn = false;
00118   ready = false;
00119   error = false;
00120 
00121   // The select function uses the timeval data structure
00122   time.tv_sec = timeout / 1000;
00123   time.tv_usec = (timeout % 1000) * 1000;
00124 
00125   FD_ZERO(&read);
00126   FD_ZERO(&write);
00127   FD_ZERO(&except);
00128 
00129   FD_SET(this->getSockHandle(), &read);
00130   FD_SET(this->getSockHandle(), &except);
00131 
00132   rc = SELECT(this->getSockHandle() + 1, &read, &write, &except, &time);
00133 
00134   if (this->SOCKET_FAIL != rc) {
00135     if (0 == rc)
00136       rtn = false;
00137     else {
00138       if (FD_ISSET(this->getSockHandle(), &read)) {
00139         ready = true;
00140         rtn = true;
00141       }
00142       else if(FD_ISSET(this->getSockHandle(), &except)) {
00143         error = true;
00144         rtn = true;
00145       }
00146       else {
00147         LOG_WARN("Select returned, but no flags are set");
00148         rtn = false;
00149       }
00150     }
00151   } else {
00152     this->logSocketError("Socket select function failed", rc, errno);
00153     rtn = false;
00154   }
00155   return rtn;
00156 }
00157 
00158 } //udp_socket
00159 } //industrial
00160 


simple_message
Author(s): Shaun Edwards
autogenerated on Sat Jun 8 2019 20:43:23