15 #include "pandabase.h"
17 #include "queuedConnectionManager.h"
18 #include "queuedConnectionListener.h"
19 #include "queuedConnectionReader.h"
20 #include "connectionWriter.h"
21 #include "netAddress.h"
22 #include "connection.h"
23 #include "netDatagram.h"
35 void receive_data(
const Datagram &data);
36 void receive_line(
string line);
51 size_t newline = _received.find(
'\n', next);
52 while (newline != string::npos) {
55 if (newline > 0 && _received[newline - 1] ==
'\r') {
58 receive_line(_received.substr(last, newline - last));
59 if (next < _received.size() && _received[next] ==
'\r') {
62 newline = _received.find(
'\n', next);
64 _received = _received.substr(next);
68 receive_line(
string line) {
69 cerr <<
"received: " << line <<
"\n";
71 size_t size = line.size();
72 while (size > 0 && isspace(line[size - 1])) {
75 if (size != line.size()) {
76 line = line.substr(0, size);
93 main(
int argc,
char *argv[]) {
95 nout <<
"fake_http_server port\n";
99 int port = atoi(argv[1]);
101 PT(
Connection) rendezvous = cm.open_TCP_server_rendezvous(port, 5);
103 if (rendezvous.is_null()) {
104 nout <<
"Cannot grab port " << port <<
".\n";
108 nout <<
"Listening for connections on port " << port <<
"\n";
111 listener.add_connection(rendezvous);
116 reader.set_raw_mode(1);
117 writer.set_raw_mode(1);
119 bool shutdown =
false;
122 while (listener.new_connection_available()) {
126 if (listener.get_new_connection(rv, address, new_connection)) {
127 nout <<
"Got connection from " << address <<
"\n";
128 reader.add_connection(new_connection);
129 clients.insert(Clients::value_type(new_connection,
ClientState(new_connection)));
136 if (cm.get_reset_connection(connection)) {
137 nout <<
"Lost connection from "
138 << connection->get_address() <<
"\n";
139 clients.erase(connection);
145 while (reader.data_available()) {
147 if (reader.get_data(datagram)) {
148 PT(
Connection) client = datagram.get_connection();
149 Clients::iterator ci = clients.find(client);
150 if (ci == clients.end()) {
151 nout <<
"Received data from unexpected client " << (
void *)client
155 state.receive_data(datagram);
This is our own Panda specialization on the default STL map.
string get_message() const
Returns the datagram's data as a string.
bool close_connection(const PT(Connection)&connection)
Terminates a UDP or TCP socket previously opened.
A specific kind of Datagram, especially for sending across or receiving from a network.
This flavor of ConnectionManager will queue up all of the reset-connection messages from the Connecti...
bool reset_connection_available() const
Returns true if one of the readers/writers/listeners reported a connection reset recently.
This class handles threaded delivery of datagrams to various TCP or UDP sockets.
This flavor of ConnectionReader will read from its sockets and queue up all of the datagrams read for...
Represents a single TCP or UDP socket for input or output.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Represents a network address to which UDP packets may be sent or to which a TCP socket may be bound...
This flavor of ConnectionListener will queue up all of the TCP connections it established for later d...