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