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 #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   //int err;
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     // Initialize address data structure
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   //int socket = this->SOCKET_FAIL;
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       // The set no delay disables the NAGEL algorithm
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 } //tcp_socket
00171 } //industrial
00172 


simple_message
Author(s): Shaun Edwards
autogenerated on Fri Jan 3 2014 11:26:56