-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsocket.h
145 lines (116 loc) · 3.99 KB
/
socket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/************************************************************************
** This file is part of the network simulator Shawn. **
** Copyright (C) 2004-2007 by the SwarmNet (www.swarmnet.de) project **
** Shawn is free software; you can redistribute it and/or modify it **
** under the terms of the BSD License. Refer to the shawn-licence.txt **
** file in the root of the Shawn source tree for further details. **
************************************************************************/
#ifndef __SHAWN_APPS_TCPIP_SOCKET_H
#define __SHAWN_APPS_TCPIP_SOCKET_H
#ifdef SHAWN
#include <shawn_config.h>
#include "_apps_enable_cmake.h"
#ifdef ENABLE_TCPIP
#define BUILD_TCPIP
#endif
#else
#define BUILD_TCPIP
#endif
#ifdef BUILD_TCPIP
// Get Storage
#ifdef SHAWN
#include <apps/tcpip/storage.h>
#else
#include "storage.h"
#endif
#ifdef SHAWN
namespace shawn
{ class SimulationController; }
// Dummy function is called when Shawn Simulation starts. Does nothing up to now.
extern "C" void init_tcpip( shawn::SimulationController& );
#endif
#include <string>
#include <map>
#include <vector>
#include <list>
#include <deque>
#include <iostream>
#include <cstddef>
struct sockaddr_in;
namespace tcpip
{
class SocketException: public std::runtime_error
{
public:
SocketException(std::string what) : std::runtime_error(what.c_str()) {}
};
class Socket
{
friend class Response;
public:
/// Constructor that prepare to connect to host:port
Socket(std::string host, int port);
/// Constructor that prepare for accepting a connection on given port
Socket(int port);
/// Destructor
~Socket();
/// @brief Returns an free port on the system
/// @note This is done by binding a socket with port=0, getting the assigned port, and closing the socket again
static int getFreeSocketPort();
/// Connects to host_:port_
void connect();
/// Wait for a incoming connection to port_
Socket* accept(const bool create = false);
void send( const std::vector<unsigned char> &buffer);
void sendExact( const Storage & );
/// Receive up to \p bufSize available bytes from Socket::socket_
std::vector<unsigned char> receive( int bufSize = 2048 );
/// Receive a complete TraCI message from Socket::socket_
bool receiveExact( Storage &);
void close();
int port();
void set_blocking(bool);
bool is_blocking();
bool has_client_connection() const;
// If verbose, each send and received data is written to stderr
bool verbose() { return verbose_; }
void set_verbose(bool newVerbose) { verbose_ = newVerbose; }
protected:
/// Length of the message length part of a TraCI message
static const int lengthLen;
/// Receive \p len bytes from Socket::socket_
void receiveComplete(unsigned char * const buffer, std::size_t len) const;
/// Receive up to \p len available bytes from Socket::socket_
size_t recvAndCheck(unsigned char * const buffer, std::size_t len) const;
/// Print \p label and \p buffer to stderr if Socket::verbose_ is set
void printBufferOnVerbose(const std::vector<unsigned char> buffer, const std::string &label) const;
private:
void init();
static void BailOnSocketError(std::string context);
#ifdef WIN32
static std::string GetWinsockErrorString(int err);
#endif
bool atoaddr(std::string, struct sockaddr_in& addr);
bool datawaiting(int sock) const;
std::string host_;
int port_;
int socket_;
int server_socket_;
bool blocking_;
bool verbose_;
#ifdef WIN32
static bool init_windows_sockets_;
static bool windows_sockets_initialized_;
static int instance_count_;
#endif
};
} // namespace tcpip
#endif // BUILD_TCPIP
#endif
/*-----------------------------------------------------------------------
* Source $Source: $
* Version $Revision: 612 $
* Date $Date: 2011-06-14 15:16:52 +0200 (Tue, 14 Jun 2011) $
*-----------------------------------------------------------------------
* $Log:$
*-----------------------------------------------------------------------*/