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/tcp_socket.h"
00034 #include "simple_message/log_wrapper.h"
00035 #include "simple_message/simple_message.h"
00036 #include "simple_message/shared_types.h"
00037 #else
00038 #include "tcp_socket.h"
00039 #include "log_wrapper.h"
00040 #include "simple_message.h"
00041 #include "shared_types.h"
00042 #endif
00043
00044 using namespace industrial::smpl_msg_connection;
00045 using namespace industrial::byte_array;
00046 using namespace industrial::simple_message;
00047 using namespace industrial::shared_types;
00048
00049 namespace industrial
00050 {
00051 namespace tcp_socket
00052 {
00053
00054 TcpSocket::TcpSocket()
00055 {
00056 }
00057
00058 TcpSocket::~TcpSocket()
00059
00060 {
00061 LOG_DEBUG("Destructing TCPSocket");
00062 CLOSE(this->getSockHandle());
00063 }
00064
00065 int TcpSocket::rawSendBytes(char *buffer, shared_int num_bytes)
00066 {
00067 int rc = this->SOCKET_FAIL;
00068
00069 rc = SEND(this->getSockHandle(), buffer, num_bytes, 0);
00070
00071 return rc;
00072 }
00073
00074 int TcpSocket::rawReceiveBytes(char *buffer, shared_int num_bytes)
00075 {
00076 int rc = this->SOCKET_FAIL;
00077
00078 rc = RECV(this->getSockHandle(), buffer, num_bytes, 0);
00079
00080 return rc;
00081 }
00082
00083 bool TcpSocket::rawPoll(int timeout, bool & ready, bool & error)
00084 {
00085 timeval time;
00086 fd_set read, write, except;
00087 int rc = this->SOCKET_FAIL;
00088 bool rtn = false;
00089 ready = false;
00090 error = false;
00091
00092
00093 time.tv_sec = timeout / 1000;
00094 time.tv_usec = (timeout % 1000) * 1000;
00095
00096 FD_ZERO(&read);
00097 FD_ZERO(&write);
00098 FD_ZERO(&except);
00099
00100 FD_SET(this->getSockHandle(), &read);
00101 FD_SET(this->getSockHandle(), &except);
00102
00103 rc = SELECT(this->getSockHandle() + 1, &read, &write, &except, &time);
00104
00105 if (this->SOCKET_FAIL != rc) {
00106 if (0 == rc)
00107 rtn = false;
00108 else {
00109 if (FD_ISSET(this->getSockHandle(), &read)) {
00110 ready = true;
00111 rtn = true;
00112 }
00113 else if(FD_ISSET(this->getSockHandle(), &except)) {
00114 error = true;
00115 rtn = true;
00116 }
00117 else {
00118 LOG_WARN("Select returned, but no flags are set");
00119 rtn = false;
00120 }
00121 }
00122 } else {
00123 this->logSocketError("Socket select function failed", rc, errno);
00124 rtn = false;
00125 }
00126 return rtn;
00127 }
00128
00129 }
00130 }
00131