SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
socket.h
Go to the documentation of this file.
1 /************************************************************************
2  ** This file is part of the network simulator Shawn. **
3  ** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project **
4  ** Shawn is free software; you can redistribute it and/or modify it **
5  ** under the terms of the BSD License. Refer to the shawn-licence.txt **
6  ** file in the root of the Shawn source tree for further details. **
7  ************************************************************************/
8 
9 #ifndef __SHAWN_APPS_TCPIP_SOCKET_H
10 #define __SHAWN_APPS_TCPIP_SOCKET_H
11 
12 #ifdef SHAWN
13  #include <shawn_config.h>
14  #include "_apps_enable_cmake.h"
15  #ifdef ENABLE_TCPIP
16  #define BUILD_TCPIP
17  #endif
18 #else
19  #define BUILD_TCPIP
20 #endif
21 
22 
23 #ifdef BUILD_TCPIP
24 
25 // Get Storage
26 #ifdef SHAWN
27  #include <apps/tcpip/storage.h>
28 #else
29  #include "storage.h"
30 #endif
31 
32 #ifdef SHAWN
33  namespace shawn
34  { class SimulationController; }
35 
36  // Dummy function is called when Shawn Simulation starts. Does nothing up to now.
37  extern "C" void init_tcpip( shawn::SimulationController& );
38 #endif
39 
40 // Disable exception handling warnings
41 #ifdef _MSC_VER
42  #pragma warning( disable : 4290 )
43 #endif
44 
45 #include <string>
46 #include <map>
47 #include <vector>
48 #include <list>
49 #include <deque>
50 #include <iostream>
51 #include <cstddef>
52 
53 
54 struct sockaddr_in;
55 
56 namespace tcpip
57 {
58 
59  class SocketException: public std::exception
60  {
61  private:
62  std::string what_;
63  public:
64  SocketException( std::string what ) throw()
65  {
66  what_ = what;
67  //std::cerr << "tcpip::SocketException: " << what << std::endl << std::flush;
68  }
69 
70  virtual const char* what() const throw()
71  {
72  return what_.c_str();
73  }
74 
75  ~SocketException() throw() {}
76  };
77 
78  class Socket
79  {
80  friend class Response;
81  public:
83  Socket(std::string host, int port);
84 
86  Socket(int port);
87 
89  ~Socket();
90 
92  void connect() throw( SocketException );
93 
95  void accept() throw( SocketException );
96 
97  void send( const std::vector<unsigned char> &buffer) throw( SocketException );
98  void sendExact( const Storage & ) throw( SocketException );
100  std::vector<unsigned char> receive( int bufSize = 2048 ) throw( SocketException );
102  bool receiveExact( Storage &) throw( SocketException );
103  void close();
104  int port();
105  void set_blocking(bool) throw( SocketException );
106  bool is_blocking() throw();
107  bool has_client_connection() const;
108 
109  // If verbose, each send and received data is written to stderr
110  bool verbose() { return verbose_; }
111  void set_verbose(bool newVerbose) { verbose_ = newVerbose; }
112 
113  protected:
115  static const int lengthLen;
116 
118  void receiveComplete(unsigned char * const buffer, std::size_t len) const;
120  size_t recvAndCheck(unsigned char * const buffer, std::size_t len) const;
122  void printBufferOnVerbose(const std::vector<unsigned char> buffer, const std::string &label) const;
123 
124  private:
125  void init();
126  void BailOnSocketError( std::string ) const throw( SocketException );
127 #ifdef WIN32
128  std::string GetWinsockErrorString(int err) const;
129 #endif
130  bool atoaddr(std::string, struct sockaddr_in& addr);
131  bool datawaiting(int sock) const throw();
132 
133  std::string host_;
134  int port_;
135  int socket_;
137  bool blocking_;
138 
139  bool verbose_;
140 #ifdef WIN32
141  static bool init_windows_sockets_;
142  static bool windows_sockets_initialized_;
143  static int instance_count_;
144 #endif
145  };
146 
147 } // namespace tcpip
148 
149 #endif // BUILD_TCPIP
150 
151 #endif
152 
153 /*-----------------------------------------------------------------------
154 * Source $Source: $
155 * Version $Revision: 612 $
156 * Date $Date: 2011-06-14 15:16:52 +0200 (Tue, 14 Jun 2011) $
157 *-----------------------------------------------------------------------
158 * $Log:$
159 *-----------------------------------------------------------------------*/
Definition: socket.cpp:61
bool verbose()
Definition: socket.h:110
SocketException(std::string what)
Definition: socket.h:64
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
Definition: socket.cpp:497
int server_socket_
Definition: socket.h:136
void printBufferOnVerbose(const std::vector< unsigned char > buffer, const std::string &label) const
Print label and buffer to stderr if Socket::verbose_ is set.
Definition: socket.cpp:453
std::vector< unsigned char > receive(int bufSize=2048)
Receive up to bufSize available bytes from Socket::socket_.
Definition: socket.cpp:471
void accept()
Wait for a incoming connection to port_.
Definition: socket.cpp:231
bool blocking_
Definition: socket.h:137
friend class Response
Definition: socket.h:80
static const int lengthLen
Length of the message length part of a TraCI message.
Definition: socket.h:115
Socket(std::string host, int port)
Constructor that prepare to connect to host:port.
Definition: socket.cpp:73
void connect()
Connects to host_:port_.
Definition: socket.cpp:324
bool datawaiting(int sock) const
Definition: socket.cpp:171
std::string host_
Definition: socket.h:133
~Socket()
Destructor.
Definition: socket.cpp:117
std::string what_
Definition: socket.h:62
void BailOnSocketError(std::string) const
Definition: socket.cpp:147
size_t recvAndCheck(unsigned char *const buffer, std::size_t len) const
Receive up to len available bytes from Socket::socket_.
Definition: socket.cpp:418
virtual const char * what() const
Definition: socket.h:70
void send(const std::vector< unsigned char > &buffer)
Definition: socket.cpp:367
void sendExact(const Storage &)
Definition: socket.cpp:398
bool is_blocking()
Definition: socket.cpp:539
bool atoaddr(std::string, struct sockaddr_in &addr)
Definition: socket.cpp:196
void receiveComplete(unsigned char *const buffer, std::size_t len) const
Receive len bytes from Socket::socket_.
Definition: socket.cpp:438
int port()
Definition: socket.cpp:162
void init()
Definition: socket.cpp:100
bool verbose_
Definition: socket.h:139
bool has_client_connection() const
Definition: socket.cpp:530
int socket_
Definition: socket.h:135
void set_blocking(bool)
Definition: socket.cpp:296
void close()
Definition: socket.cpp:349
void set_verbose(bool newVerbose)
Definition: socket.h:111