SUMO - Simulation of Urban MObility
|
00001 /************************************************************************ 00002 ** This file is part of the network simulator Shawn. ** 00003 ** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project ** 00004 ** Shawn is free software; you can redistribute it and/or modify it ** 00005 ** under the terms of the BSD License. Refer to the shawn-licence.txt ** 00006 ** file in the root of the Shawn source tree for further details. ** 00007 ************************************************************************/ 00008 00009 #ifndef __SHAWN_APPS_TCPIP_SOCKET_H 00010 #define __SHAWN_APPS_TCPIP_SOCKET_H 00011 00012 #ifdef SHAWN 00013 #include <shawn_config.h> 00014 #include "_apps_enable_cmake.h" 00015 #ifdef ENABLE_TCPIP 00016 #define BUILD_TCPIP 00017 #endif 00018 #else 00019 #define BUILD_TCPIP 00020 #endif 00021 00022 00023 #ifdef BUILD_TCPIP 00024 00025 // Get Storage 00026 #ifdef SHAWN 00027 #include <apps/tcpip/storage.h> 00028 #else 00029 #include "storage.h" 00030 #endif 00031 00032 #ifdef SHAWN 00033 namespace shawn 00034 { class SimulationController; } 00035 00036 // Dummy function is called when Shawn Simulation starts. Does nothing up to now. 00037 extern "C" void init_tcpip( shawn::SimulationController& ); 00038 #endif 00039 00040 // Disable exception handling warnings 00041 #ifdef _MSC_VER 00042 #pragma warning( disable : 4290 ) 00043 #endif 00044 00045 #include <string> 00046 #include <map> 00047 #include <vector> 00048 #include <list> 00049 #include <deque> 00050 #include <iostream> 00051 #include <cstddef> 00052 00053 00054 struct in_addr; 00055 00056 namespace tcpip 00057 { 00058 00059 class SocketException: public std::exception 00060 { 00061 private: 00062 std::string what_; 00063 public: 00064 SocketException( std::string what ) throw() 00065 { 00066 what_ = what; 00067 //std::cerr << "tcpip::SocketException: " << what << std::endl << std::flush; 00068 } 00069 00070 virtual const char* what() const throw() 00071 { 00072 return what_.c_str(); 00073 } 00074 00075 ~SocketException() throw() {} 00076 }; 00077 00078 class Socket 00079 { 00080 friend class Response; 00081 public: 00083 Socket(std::string host, int port); 00084 00086 Socket(int port); 00087 00089 ~Socket(); 00090 00092 void connect() throw( SocketException ); 00093 00095 void accept() throw( SocketException ); 00096 00097 void send( const std::vector<unsigned char> &buffer) throw( SocketException ); 00098 void sendExact( const Storage & ) throw( SocketException ); 00100 std::vector<unsigned char> receive( int bufSize = 2048 ) throw( SocketException ); 00102 bool receiveExact( Storage &) throw( SocketException ); 00103 void close(); 00104 int port(); 00105 void set_blocking(bool) throw( SocketException ); 00106 bool is_blocking() throw(); 00107 bool has_client_connection() const; 00108 00109 // If verbose, each send and received data is written to stderr 00110 bool verbose() { return verbose_; } 00111 void set_verbose(bool newVerbose) { verbose_ = newVerbose; } 00112 00113 protected: 00115 static const int lengthLen; 00116 00118 void receiveComplete(unsigned char * const buffer, std::size_t len) const; 00120 size_t recvAndCheck(unsigned char * const buffer, std::size_t len) const; 00122 void printBufferOnVerbose(const std::vector<unsigned char> buffer, const std::string &label) const; 00123 00124 private: 00125 void init(); 00126 void BailOnSocketError( std::string ) const throw( SocketException ); 00127 #ifdef WIN32 00128 std::string GetWinsockErrorString(int err) const; 00129 #endif 00130 bool atoaddr(std::string, struct in_addr& addr); 00131 bool datawaiting(int sock) const throw(); 00132 00133 std::string host_; 00134 int port_; 00135 int socket_; 00136 int server_socket_; 00137 bool blocking_; 00138 00139 bool verbose_; 00140 #ifdef WIN32 00141 static bool init_windows_sockets_; 00142 static bool windows_sockets_initialized_; 00143 static int instance_count_; 00144 #endif 00145 }; 00146 00147 } // namespace tcpip 00148 00149 #endif // BUILD_TCPIP 00150 00151 #endif 00152 00153 /*----------------------------------------------------------------------- 00154 * Source $Source: $ 00155 * Version $Revision: 612 $ 00156 * Date $Date: 2011-06-14 15:16:52 +0200 (Di, 14. Jun 2011) $ 00157 *----------------------------------------------------------------------- 00158 * $Log:$ 00159 *-----------------------------------------------------------------------*/