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 #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
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
00063
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
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;
00092 udp_read_head_ = this->udp_read_buffer_;
00093 udp_read_len_ = rc;
00094 }
00095 if(num_bytes == 0 || num_bytes >= udp_read_len_)
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;
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
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
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 }
00159 }
00160