tcp_server.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Southwest Research Institute
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 Southwest Research Institute, 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 #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   //int err;
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     // Initialize address data structure
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   //int socket = this->SOCKET_FAIL;
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       // The set no delay disables the NAGEL algorithm
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 } //tcp_socket
00168 } //industrial
00169 


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Aug 28 2015 11:11:56