fastcgi++
A C++ FastCGI/Web API
transceiver.hpp
Go to the documentation of this file.
1 
10 /*******************************************************************************
11 * Copyright (C) 2017 Eddie Carle [eddie@isatec.ca] *
12 * *
13 * This file is part of fastcgi++. *
14 * *
15 * fastcgi++ is free software: you can redistribute it and/or modify it under *
16 * the terms of the GNU Lesser General Public License as published by the Free *
17 * Software Foundation, either version 3 of the License, or (at your option) *
18 * any later version. *
19 * *
20 * fastcgi++ is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for *
23 * more details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with fastcgi++. If not, see <http://www.gnu.org/licenses/>. *
27 *******************************************************************************/
28 
29 #ifndef FASTCGIPP_TRANSCEIVER_HPP
30 #define FASTCGIPP_TRANSCEIVER_HPP
31 
32 #include <map>
33 #include <list>
34 #include <queue>
35 #include <algorithm>
36 #include <map>
37 #include <functional>
38 #include <memory>
39 #include <atomic>
40 #include <mutex>
41 #include <thread>
42 
43 #include <fastcgi++/protocol.hpp>
44 #include "fastcgi++/block.hpp"
45 
47 namespace Fastcgipp
48 {
50 
59  {
60  public:
62 
68  void handler();
69 
71 
78  void stop();
79 
81 
88  void terminate();
89 
91 
94  void start();
95 
97  void join();
98 
100 
106  void send(const Socket& socket, Block&& data, bool kill);
107 
109 
115  Transceiver(
116  const std::function<void(Protocol::RequestId, Message&&)>
117  sendMessage);
118 
119  ~Transceiver();
120 
122 
128  bool listen()
129  {
130  return m_sockets.listen();
131  }
132 
134 
148  bool listen(
149  const char* name,
150  uint32_t permissions = 0xffffffffUL,
151  const char* owner = nullptr,
152  const char* group = nullptr)
153  {
154  return m_sockets.listen(name, permissions, owner, group);
155  }
156 
158 
169  bool listen(
170  const char* interface,
171  const char* service)
172  {
173  return m_sockets.listen(interface, service);
174  }
175 
176  private:
178  std::map<Socket, Block> m_receiveBuffers;
179 
181  struct Record
182  {
183  const Socket socket;
184  const Block data;
185  const char* read;
186  const bool kill;
187 
189  const Socket& socket_,
190  Block&& data_,
191  bool kill_):
192  socket(socket_),
193  data(std::move(data_)),
194  read(data.begin()),
195  kill(kill_)
196  {}
197  };
198 
200  std::deque<std::unique_ptr<Record>> m_sendBuffer;
201 
204 
206  const std::function<void(Protocol::RequestId, Message&&)> m_sendMessage;
207 
210 
212 
215  inline bool transmit();
216 
218  inline void receive(Socket& socket);
219 
221  std::atomic_bool m_terminate;
222 
224  std::atomic_bool m_stop;
225 
227  std::thread m_thread;
228 
230  void cleanupSocket(const Socket& socket);
231 
232 #if FASTCGIPP_LOG_LEVEL > 3
233  std::atomic_ullong m_connectionKillCount;
235 
237  std::atomic_ullong m_connectionRDHupCount;
238 
240  std::atomic_ullong m_recordsSent;
241 
243  std::atomic_ullong m_recordsQueued;
244 
246  std::atomic_ullong m_recordsReceived;
247 #endif
248  };
249 }
250 
251 #endif
Topmost namespace for the fastcgi++ library.
void send(const Socket &socket, Block &&data, bool kill)
Queue up a block of data for transmission.
bool listen(const char *interface, const char *service)
Listen to a TCP port.
std::deque< std::unique_ptr< Record > > m_sendBuffer
Buffer for transmitting data
void handler()
General transceiver handler.
Definition: transceiver.cpp:75
void join()
Block until a stop() or terminate() is called and completed.
void cleanupSocket(const Socket &socket)
Cleanup a dead socket.
Data structure used to pass messages to requests.
Definition: message.hpp:46
STL namespace.
SocketGroup m_sockets
Listen for connections with this.
Transceiver(const std::function< void(Protocol::RequestId, Message &&)> sendMessage)
Constructor.
bool listen()
Listen to the default Fastcgi socket.
Definition: sockets.cpp:201
std::thread m_thread
Thread our handler is running in.
Declares everything for relating to the FastCGI protocol itself.
const std::function< void(Protocol::RequestId, Message &&)> m_sendMessage
Function to call to pass messages to requests.
std::atomic_bool m_terminate
True when handler() should be terminating.
void terminate()
Call from any thread to terminate the handler() thread.
Definition: transceiver.cpp:94
std::mutex mutex
Thread safe the logging mechanism.
Definition: log.cpp:102
Simple FastCGI record to queue up for transmission.
void start()
Call from any thread to start the handler() thread.
std::map< Socket, Block > m_receiveBuffers
Container associating sockets with their receive buffers.
Class for representing an OS level I/O socket.
Definition: sockets.hpp:83
void receive(Socket &socket)
Receive data on the specified socket.
A unique identifier for each FastCGI request.
Definition: protocol.hpp:71
Handles low level communication with "the other side".
Definition: transceiver.hpp:58
Record(const Socket &socket_, Block &&data_, bool kill_)
bool listen()
Listen to the default Fastcgi socket.
void stop()
Call from any thread to stop the handler() thread.
Definition: transceiver.cpp:88
std::atomic_bool m_stop
True when handler() should be stopping.
Class for representing an OS level socket that listens for connections.
Definition: sockets.hpp:287
bool transmit()
Transmit all buffered data possible.
Definition: transceiver.cpp:32
Declares the Block data structure.
bool listen(const char *name, uint32_t permissions=0xffffffffUL, const char *owner=nullptr, const char *group=nullptr)
Listen to a named socket.
std::mutex m_sendBufferMutex
Thread safe the send buffer.
Data structure to hold a block of raw data.
Definition: block.hpp:44