socket.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
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
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
00037 extern "C" void init_tcpip( shawn::SimulationController& );
00038 #endif
00039
00040
00041 #ifdef WIN32
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
00052
00053 struct in_addr;
00054
00055 namespace tcpip
00056 {
00057
00058 class SocketException: public std::exception
00059 {
00060 private:
00061 std::string what_;
00062 public:
00063 SocketException( std::string what ) throw()
00064 {
00065 what_ = what;
00066
00067 }
00068
00069 virtual const char* what() const throw()
00070 {
00071 return what_.c_str();
00072 }
00073
00074 ~SocketException() throw() {}
00075 };
00076
00077 class Socket
00078 {
00079 friend class Response;
00080 public:
00082 Socket(std::string host, int port);
00083
00085 Socket(int port);
00086
00088 ~Socket();
00089
00091 void connect() throw( SocketException );
00092
00094 void accept() throw( SocketException );
00095
00096 void send( const std::vector<unsigned char> ) throw( SocketException );
00097 void sendExact( const Storage & ) throw( SocketException );
00098 std::vector<unsigned char> receive( int bufSize = 2048 ) throw( SocketException );
00099 bool receiveExact( Storage &) throw( SocketException );
00100 void close();
00101 int port();
00102 void set_blocking(bool) throw( SocketException );
00103 bool is_blocking() throw();
00104 bool has_client_connection() const;
00105
00106
00107 bool verbose() { return verbose_; }
00108 void set_verbose(bool newVerbose) { verbose_ = newVerbose; }
00109
00110 private:
00111 void init();
00112 void BailOnSocketError( std::string ) const throw( SocketException );
00113 #ifdef WIN32
00114 std::string GetWinsockErrorString(int err) const;
00115 #endif
00116 bool atoaddr(std::string, struct in_addr& addr);
00117 bool datawaiting(int sock) const throw();
00118
00119 std::string host_;
00120 int port_;
00121 int socket_;
00122 int server_socket_;
00123 bool blocking_;
00124
00125 bool verbose_;
00126 #ifdef WIN32
00127 static bool init_windows_sockets_;
00128 static bool windows_sockets_initialized_;
00129 static int instance_count_;
00130 #endif
00131 };
00132
00133 }
00134
00135 #endif // BUILD_TCPIP
00136
00137 #endif
00138
00139
00140
00141
00142
00143
00144
00145