SUMO - Simulation of Urban MObility
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 #include <string>
41 #include <map>
42 #include <vector>
43 #include <list>
44 #include <deque>
45 #include <iostream>
46 #include <cstddef>
47 
48 
49 struct sockaddr_in;
50 
51 namespace tcpip
52 {
53 
54  class SocketException: public std::runtime_error
55  {
56  public:
57  SocketException(std::string what) : std::runtime_error(what.c_str()) {}
58  };
59 
60  class Socket
61  {
62  friend class Response;
63  public:
65  Socket(std::string host, int port);
66 
68  Socket(int port);
69 
71  ~Socket();
72 
74  void connect();
75 
77  Socket* accept(const bool create = false);
78 
79  void send( const std::vector<unsigned char> &buffer);
80  void sendExact( const Storage & );
82  std::vector<unsigned char> receive( int bufSize = 2048 );
84  bool receiveExact( Storage &);
85  void close();
86  int port();
87  void set_blocking(bool);
88  bool is_blocking();
89  bool has_client_connection() const;
90 
91  // If verbose, each send and received data is written to stderr
92  bool verbose() { return verbose_; }
93  void set_verbose(bool newVerbose) { verbose_ = newVerbose; }
94 
95  protected:
97  static const int lengthLen;
98 
100  void receiveComplete(unsigned char * const buffer, std::size_t len) const;
102  size_t recvAndCheck(unsigned char * const buffer, std::size_t len) const;
104  void printBufferOnVerbose(const std::vector<unsigned char> buffer, const std::string &label) const;
105 
106  private:
107  void init();
108  void BailOnSocketError(std::string context) const;
109 #ifdef WIN32
110  std::string GetWinsockErrorString(int err) const;
111 #endif
112  bool atoaddr(std::string, struct sockaddr_in& addr);
113  bool datawaiting(int sock) const;
114 
115  std::string host_;
116  int port_;
117  int socket_;
119  bool blocking_;
120 
121  bool verbose_;
122 #ifdef WIN32
123  static bool init_windows_sockets_;
124  static bool windows_sockets_initialized_;
125  static int instance_count_;
126 #endif
127  };
128 
129 } // namespace tcpip
130 
131 #endif // BUILD_TCPIP
132 
133 #endif
134 
135 /*-----------------------------------------------------------------------
136 * Source $Source: $
137 * Version $Revision: 612 $
138 * Date $Date: 2011-06-14 15:16:52 +0200 (Tue, 14 Jun 2011) $
139 *-----------------------------------------------------------------------
140 * $Log:$
141 *-----------------------------------------------------------------------*/
Definition: socket.cpp:61
bool verbose()
Definition: socket.h:92
SocketException(std::string what)
Definition: socket.h:57
int server_socket_
Definition: socket.h:118
bool blocking_
Definition: socket.h:119
static const int lengthLen
Length of the message length part of a TraCI message.
Definition: socket.h:97
std::string host_
Definition: socket.h:115
bool verbose_
Definition: socket.h:121
int socket_
Definition: socket.h:117
void set_verbose(bool newVerbose)
Definition: socket.h:93