Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarTcp.hpp
blob: f4b90734b7f62844c51b2641a5ca997a375ada70 (plain)
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
#pragma once

#include "StarIODevice.hpp"
#include "StarSocket.hpp"
#include "StarThread.hpp"

namespace Star {

STAR_CLASS(TcpSocket);
STAR_CLASS(TcpServer);

class TcpSocket : public Socket {
public:
  static TcpSocketPtr connectTo(HostAddressWithPort const& address);
  static TcpSocketPtr listen(HostAddressWithPort const& address);

  TcpSocketPtr accept();

  // Must be called after connect.  Sets TCP_NODELAY option.
  void setNoDelay(bool noDelay);

  size_t receive(char* data, size_t len);
  size_t send(char const* data, size_t len);

  HostAddressWithPort localAddress() const;
  HostAddressWithPort remoteAddress() const;

private:
  TcpSocket(NetworkMode networkMode);
  TcpSocket(NetworkMode networkMode, SocketImplPtr impl);

  void connect(HostAddressWithPort const& address);

  HostAddressWithPort m_remoteAddress;
};

// Simple class to listen for and open TcpSocket instances.
class TcpServer {
public:
  typedef function<void(TcpSocketPtr socket)> AcceptCallback;

  TcpServer(HostAddressWithPort const& address);
  // Listens to all interfaces.
  TcpServer(uint16_t port);
  ~TcpServer();

  void stop();
  bool isListening() const;

  // Blocks until next connection available for the given timeout.  Throws
  // ServerClosed if close() is called.  Cannot be called if AcceptCallback is
  // set.
  TcpSocketPtr accept(unsigned timeout);

  // Rather than calling and blocking on accept(), if an AcceptCallback is set
  // here, it will be called whenever a new connection is available.
  // Exceptions thrown from the callback function will be caught and logged,
  // and will cause the server to close.  The timeout here is the timeout that
  // is passed to accept in the loop, the longer the timeout the slower it will
  // shutdown on a call to close.
  void setAcceptCallback(AcceptCallback callback, unsigned timeout = 20);

private:
  mutable Mutex m_mutex;

  AcceptCallback m_callback;
  ThreadFunction<void> m_callbackThread;
  HostAddressWithPort m_hostAddress;
  TcpSocketPtr m_listenSocket;
};

}