Eclipse 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 
75  static int getFreeSocketPort();
76 
78  void connect();
79 
81  Socket* accept(const bool create = false);
82 
83  void send( const std::vector<unsigned char> &buffer);
84  void sendExact( const Storage & );
86  std::vector<unsigned char> receive( int bufSize = 2048 );
88  bool receiveExact( Storage &);
89  void close();
90  int port();
91  void set_blocking(bool);
92  bool is_blocking();
93  bool has_client_connection() const;
94 
95  // If verbose, each send and received data is written to stderr
96  bool verbose() { return verbose_; }
97  void set_verbose(bool newVerbose) { verbose_ = newVerbose; }
98 
99  protected:
101  static const int lengthLen;
102 
104  void receiveComplete(unsigned char * const buffer, std::size_t len) const;
106  size_t recvAndCheck(unsigned char * const buffer, std::size_t len) const;
108  void printBufferOnVerbose(const std::vector<unsigned char> buffer, const std::string &label) const;
109 
110  private:
111  void init();
112  static void BailOnSocketError(std::string context);
113 #ifdef WIN32
114  static std::string GetWinsockErrorString(int err);
115 #endif
116  bool atoaddr(std::string, struct sockaddr_in& addr);
117  bool datawaiting(int sock) const;
118 
119  std::string host_;
120  int port_;
121  int socket_;
123  bool blocking_;
124 
125  bool verbose_;
126 #ifdef WIN32
127  static bool init_windows_sockets_;
128  static bool windows_sockets_initialized_;
129  static int instance_count_;
130 #endif
131  };
132 
133 } // namespace tcpip
134 
135 #endif // BUILD_TCPIP
136 
137 #endif
138 
139 /*-----------------------------------------------------------------------
140 * Source $Source: $
141 * Version $Revision: 612 $
142 * Date $Date: 2011-06-14 15:16:52 +0200 (Tue, 14 Jun 2011) $
143 *-----------------------------------------------------------------------
144 * $Log:$
145 *-----------------------------------------------------------------------*/
tcpip::Socket::is_blocking
bool is_blocking()
Definition: socket.cpp:568
tcpip::Socket::receiveComplete
void receiveComplete(unsigned char *const buffer, std::size_t len) const
Receive len bytes from Socket::socket_.
Definition: socket.cpp:469
tcpip::Socket::receive
std::vector< unsigned char > receive(int bufSize=2048)
Receive up to bufSize available bytes from Socket::socket_.
Definition: socket.cpp:502
tcpip::Socket
Definition: socket.h:61
tcpip::Socket::getFreeSocketPort
static int getFreeSocketPort()
Returns an free port on the system.
Definition: socket.cpp:118
tcpip
Definition: socket.cpp:62
tcpip::Socket::blocking_
bool blocking_
Definition: socket.h:123
tcpip::Socket::close
void close()
Definition: socket.cpp:382
tcpip::Socket::set_verbose
void set_verbose(bool newVerbose)
Definition: socket.h:97
tcpip::Socket::connect
void connect()
Connects to host_:port_.
Definition: socket.cpp:358
tcpip::SocketException::SocketException
SocketException(std::string what)
Definition: socket.h:57
tcpip::Socket::printBufferOnVerbose
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:484
tcpip::Socket::accept
Socket * accept(const bool create=false)
Wait for a incoming connection to port_.
Definition: socket.cpp:260
tcpip::Socket::init
void init()
Definition: socket.cpp:100
tcpip::Socket::port
int port()
Definition: socket.cpp:191
tcpip::Socket::datawaiting
bool datawaiting(int sock) const
Definition: socket.cpp:200
tcpip::Socket::Response
friend class Response
Definition: socket.h:62
tcpip::Socket::verbose_
bool verbose_
Definition: socket.h:125
tcpip::Socket::host_
std::string host_
Definition: socket.h:119
tcpip::Socket::send
void send(const std::vector< unsigned char > &buffer)
Definition: socket.cpp:400
tcpip::Socket::has_client_connection
bool has_client_connection() const
Definition: socket.cpp:559
tcpip::Socket::verbose
bool verbose()
Definition: socket.h:96
tcpip::SocketException
Definition: socket.h:55
tcpip::Socket::Socket
Socket(std::string host, int port)
Constructor that prepare to connect to host:port.
Definition: socket.cpp:73
tcpip::Socket::lengthLen
static const int lengthLen
Length of the message length part of a TraCI message.
Definition: socket.h:101
tcpip::Socket::BailOnSocketError
static void BailOnSocketError(std::string context)
Definition: socket.cpp:177
storage.h
tcpip::Socket::socket_
int socket_
Definition: socket.h:121
tcpip::Socket::recvAndCheck
size_t recvAndCheck(unsigned char *const buffer, std::size_t len) const
Receive up to len available bytes from Socket::socket_.
Definition: socket.cpp:449
tcpip::Socket::set_blocking
void set_blocking(bool)
Definition: socket.cpp:331
tcpip::Socket::server_socket_
int server_socket_
Definition: socket.h:122
tcpip::Socket::port_
int port_
Definition: socket.h:120
tcpip::Socket::sendExact
void sendExact(const Storage &)
Definition: socket.cpp:430
tcpip::Socket::atoaddr
bool atoaddr(std::string, struct sockaddr_in &addr)
Definition: socket.cpp:225
tcpip::Storage
Definition: storage.h:38
tcpip::Socket::receiveExact
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
Definition: socket.cpp:527
tcpip::Socket::~Socket
~Socket()
Destructor.
Definition: socket.cpp:147