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 #ifdef ROS
00032 #include "simple_message/socket/tcp_server.h"
00033 #include "simple_message/log_wrapper.h"
00034 #endif
00035
00036 #ifdef MOTOPLUS
00037 #include "tcp_server.h"
00038 #include "log_wrapper.h"
00039 #endif
00040
00041
00042 namespace industrial
00043 {
00044 namespace tcp_server
00045 {
00046
00047 TcpServer::TcpServer()
00048 {
00049 this->setSockHandle(this->SOCKET_FAIL);
00050 this->setSrvrHandle(this->SOCKET_FAIL);
00051 memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00052 }
00053
00054 TcpServer::~TcpServer()
00055 {
00056 CLOSE(this->getSockHandle());
00057 CLOSE(this->getSrvrHandle());
00058 }
00059
00060 bool TcpServer::init(int port_num)
00061 {
00062 int rc;
00063 bool rtn;
00064 const int reuse_addr = 1;
00065
00066 SOCKLEN_T addrSize = 0;
00067
00068 rc = SOCKET(AF_INET, SOCK_STREAM, 0);
00069 if (this->SOCKET_FAIL != rc)
00070 {
00071 this->setSrvrHandle(rc);
00072 LOG_DEBUG("Socket created, rc: %d", rc);
00073 LOG_DEBUG("Socket handle: %d", this->getSrvrHandle());
00074
00075
00076 SET_REUSE_ADDR(this->getSrvrHandle(), reuse_addr);
00077
00078
00079 memset(&this->sockaddr_, 0, sizeof(this->sockaddr_));
00080 this->sockaddr_.sin_family = AF_INET;
00081 this->sockaddr_.sin_addr.s_addr = INADDR_ANY;
00082 this->sockaddr_.sin_port = HTONS(port_num);
00083
00084 addrSize = sizeof(this->sockaddr_);
00085 rc = BIND(this->getSrvrHandle(), (sockaddr *)&(this->sockaddr_), addrSize);
00086
00087 if (this->SOCKET_FAIL != rc)
00088 {
00089 LOG_INFO("Server socket successfully initialized");
00090
00091 rc = LISTEN(this->getSrvrHandle(), 1);
00092
00093 if (this->SOCKET_FAIL != rc)
00094 {
00095 LOG_INFO("Socket in listen mode");
00096 rtn = true;
00097 }
00098 else
00099 {
00100 LOG_ERROR("Failed to set socket to listen");
00101 rtn = false;
00102 }
00103 }
00104 else
00105 {
00106 LOG_ERROR("Failed to bind socket, rc: %d", rc);
00107 CLOSE(this->getSrvrHandle());
00108 rtn = false;
00109 }
00110
00111 }
00112 else
00113 {
00114 LOG_ERROR("Failed to create socket, rc: %d", rc);
00115 rtn = false;
00116 }
00117
00118 return rtn;
00119 }
00120
00121 bool TcpServer::makeConnect()
00122 {
00123 bool rtn = false;
00124 int rc = this->SOCKET_FAIL;
00125
00126 int disableNodeDelay = 1;
00127 int err = 0;
00128
00129 if (!this->isConnected())
00130 {
00131 this->setConnected(false);
00132 if (this->SOCKET_FAIL != this->getSockHandle())
00133 {
00134 CLOSE(this->getSockHandle());
00135 this->setSockHandle(this->SOCKET_FAIL);
00136 }
00137
00138 rc = ACCEPT(this->getSrvrHandle(), NULL, NULL);
00139
00140 if (this->SOCKET_FAIL != rc)
00141 {
00142 this->setSockHandle(rc);
00143 LOG_INFO("Client socket accepted");
00144
00145
00146 rc = SET_NO_DELAY(this->getSockHandle(), disableNodeDelay);
00147 err = errno;
00148 if (this->SOCKET_FAIL == rc)
00149 {
00150 LOG_WARN("Failed to set no socket delay, errno: %d, sending data can be delayed by up to 250ms", err);
00151 }
00152 this->setConnected(true);
00153 rtn = true;
00154 }
00155 else
00156 {
00157 LOG_ERROR("Failed to accept for client connection");
00158 rtn = false;
00159 }
00160 }
00161 else
00162 {
00163 LOG_WARN("Tried to connect when socket already in connected state");
00164 }
00165
00166 return rtn;
00167
00168 }
00169
00170 }
00171 }
00172