RpcRequestContext.h
1 #pragma once
2 
3 #include "stormancer/BuildConfig.h"
4 
5 #include "stormancer/Scene.h"
6 #include "stormancer/Streams/bytestream.h"
8 #include "stormancer/IScenePeer.h"
9 #include "stormancer/Tasks.h"
10 #include "stormancer/Serializer.h"
11 #include "stormancer/RPC/Constants.h"
12 
13 namespace Stormancer
14 {
15 
16  template<typename T = IScenePeer>
18  {
19  public:
20 
21  friend class RpcService;
22 
23 #pragma region public_methods
24 
25  RpcRequestContext(T* peer, std::weak_ptr<Scene> scene, uint16 id, bool ordered, ibytestream& inputStream, pplx::cancellation_token ct)
26  : _peer(peer)
27  , _scene(scene)
28  , _inputStream(inputStream)
29  , _id(id)
30  , _ordered(ordered)
31  , _cancellationToken(ct)
32  {
33  }
34 
36  {
37  }
38 
39  T* remotePeer()
40  {
41  return _peer;
42  }
43 
44  pplx::cancellation_token cancellationToken()
45  {
46  return _cancellationToken;
47  }
48 
49  ibytestream& inputStream()
50  {
51  return _inputStream;
52  }
53 
54  template<typename TObj>
55  TObj readObject()
56  {
57  Serializer serializer;
58  return serializer.deserializeOne<TObj>(_inputStream);
59  }
60 
61  void sendValue(const StreamWriter& streamWriter, PacketPriority priority = PacketPriority::MEDIUM_PRIORITY)
62  {
63  auto scene = _scene.lock();
64 
65  if (!scene)
66  {
67  throw std::runtime_error("The scene is invalid");
68  }
69 
70  scene->send(Stormancer::rpc::nextRouteName, [this, streamWriter](obytestream& stream)
71  {
72  writeRequestId(stream);
73  if (streamWriter)
74  {
75  streamWriter(stream);
76  }
77  }, priority, (_ordered ? PacketReliability::RELIABLE_ORDERED : PacketReliability::RELIABLE), _rpcClientChannelIdentifier);
78 
79  _msgSent = 1;
80  }
81 
82  template<typename TValue>
83  void sendValueTemplated(const TValue& value, PacketPriority priority = PacketPriority::MEDIUM_PRIORITY)
84  {
85  sendValue([&value](obytestream& stream)
86  {
87  Serializer().serialize(stream, value);
88  }, priority);
89  }
90 
91  void sendError(std::string errorMsg) const
92  {
93  auto scene = _scene.lock();
94 
95  if (!scene)
96  {
97  throw std::runtime_error("The scene is invalid");
98  }
99 
100  scene->send(Stormancer::rpc::errorRouteName, [this, errorMsg](obytestream& stream)
101  {
102  writeRequestId(stream);
103  _serializer.serialize(stream, errorMsg);
105  }
106 
107 #pragma endregion
108 
109  private:
110 
111 #pragma region private_methods
112 
113  void sendComplete() const
114  {
115  auto scene = _scene.lock();
116 
117  if (!scene)
118  {
119  throw std::runtime_error("The scene is invalid");
120  }
121 
122  scene->send(Stormancer::rpc::completeRouteName, [this](obytestream& stream)
123  {
124  stream << _msgSent;
125  writeRequestId(stream);
127  }
128 
129  void writeRequestId(obytestream& stream) const
130  {
131  stream << _id;
132  }
133 
134 #pragma endregion
135 
136 #pragma region private_members
137 
138  T* _peer = nullptr;
139  std::weak_ptr<Scene> _scene;
140  ibytestream& _inputStream;
141  uint16 _id;
142  bool _ordered;
143  byte _msgSent = 0;
144  pplx::cancellation_token _cancellationToken;
145  Serializer _serializer;
146  const std::string _rpcClientChannelIdentifier = "RPC_client";
147 
148 #pragma endregion
149  };
150 
151  using RpcRequestContext_ptr = std::shared_ptr<RpcRequestContext<IScenePeer>>;
152 }
Definition: RpcRequestContext.h:17
Definition: RpcService.h:25
@ RELIABLE
The message is sent reliably, but not necessarily in any order. Same overhead as UNRELIABLE.
Definition: PacketPriority.h:56
Definition: obytestream.h:13
Definition: ibytestream.h:11
@ MEDIUM_PRIORITY
Definition: PacketPriority.h:32
PacketPriority
These enumerations are used to describe when packets are delivered.
Definition: PacketPriority.h:21
This file contains enumerations for packet priority and reliability enumerations.
Definition: Serializer.h:11
@ RELIABLE_ORDERED
Definition: PacketPriority.h:60
TOutput deserializeOne(ibytestream &stream) const
Definition: Serializer.h:34