forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocketServiceHandler.h
More file actions
99 lines (86 loc) · 4.28 KB
/
RSocketServiceHandler.h
File metadata and controls
99 lines (86 loc) · 4.28 KB
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
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <folly/Expected.h>
#include "rsocket/RSocketConnectionEvents.h"
#include "rsocket/RSocketException.h"
#include "rsocket/RSocketParameters.h"
#include "rsocket/RSocketResponder.h"
#include "rsocket/RSocketServerState.h"
#include "rsocket/RSocketStats.h"
#include "rsocket/internal/Common.h"
namespace rsocket {
// This is a convenience function which is used to create a simplified
// RSocketServiceHandler.
using OnNewSetupFn =
folly::Function<std::shared_ptr<RSocketResponder>(const SetupParameters&)>;
// This struct holds all the necessary information needed by the RSocketServer
// to initiate a connection with a client.
struct RSocketConnectionParams {
explicit RSocketConnectionParams(
std::shared_ptr<RSocketResponder> _responder,
std::shared_ptr<RSocketStats> _stats = RSocketStats::noop(),
std::shared_ptr<RSocketConnectionEvents> _connectionEvents = nullptr)
: responder(std::move(_responder)),
stats(std::move(_stats)),
connectionEvents(std::move(_connectionEvents)) {}
std::shared_ptr<RSocketResponder> responder;
std::shared_ptr<RSocketStats> stats;
std::shared_ptr<RSocketConnectionEvents> connectionEvents;
};
// This class has to be implemented by the application. The methods can be
// called from different threads and it is the application's responsibility to
// ensure thread-safety.
//
// A RSocketServiceHandler instance should be passed as parameter to the
// RSocketServer::start() methods. The passed instance will apply globally to
// all connections handled by the server.
//
// If the application wishes to preserve per connection information at the
// transport level (say HTTP Auth, TCP options) and use it at the application
// layer OR if the application wishes to have per-connection
// RSocketServiceHandler, then it should pass an instance of
// RSocketServiceHandler to the RSocketServer::acceptConnection() method. In
// this case it does not have to worry about thread safety.
//
class RSocketServiceHandler {
public:
RSocketServiceHandler() = default;
virtual ~RSocketServiceHandler() = default;
RSocketServiceHandler(RSocketServiceHandler&&) = default; // move
RSocketServiceHandler& operator=(RSocketServiceHandler&&) = default; // move
// This method gets called for each client that connects to the server. The
// application has to implement this method. If the application does not
// want to accept this connection, it should return a RSocketException. If
// the application returns a RSocketException, then an ERROR frame with code
// REJECTED_SETUP is sent to the client. The exception message is sent as
// payload.
virtual folly::Expected<RSocketConnectionParams, RSocketException> onNewSetup(
const SetupParameters&) = 0;
// This method gets called after some state is created for each client. The
// application should preserve the RSocketServerState if it wants to resume
// the connection later.
virtual void onNewRSocketState(
std::shared_ptr<RSocketServerState>,
ResumeIdentificationToken);
// This method gets called when a client tries to resume. The application
// should return the corresponding RSocketServerState for the given resumeId
// if it wants to resume. The application should return a RSocketException
// if it does not want to resume. If the application returns a
// RSocketException, then an ERROR frame with code REJECTED_RESUME is sent to
// the client. The exception message is sent as payload.
virtual folly::Expected<std::shared_ptr<RSocketServerState>, RSocketException>
onResume(ResumeIdentificationToken);
// This method gives a fine-grained control to the application during
// resumption (provided it agreed to resume in the onResume() call above).
// It tells the application what streams will be resumed in clean/dirty
// state. If the application is okay with resuming the given clean/dirty
// streams, it should return true.
virtual bool canResume(
const std::vector<StreamId>& /* cleanStreamIds */,
const std::vector<StreamId>& /* dirtyStreamIds */,
ResumeIdentificationToken);
// Convenience constructor to create a simple RSocketServiceHandler.
static std::shared_ptr<RSocketServiceHandler> create(
OnNewSetupFn onNewSetupFn);
};
}