CVB++ 15.0
server.hpp
1#pragma once
2
3#include "../_cexports/c_webstreaming.h"
4#include "webstreaming.hpp"
5#include "converter.hpp"
6#include "../string.hpp"
7#include "../global.hpp"
8#include "../image.hpp"
9
10namespace Cvb
11{
12
13 CVB_BEGIN_INLINE_NS
14
15 template <>
16 inline HandleGuard<WebStreaming::Server>::HandleGuard(void *handle) noexcept
17 : HandleGuard<WebStreaming::Server>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
18 {
19 }
20
21 namespace WebStreaming
22 {
23
30 class Server
31 {
32 public:
33 using GuardType = HandleGuard<Server>;
34
35 private:
36 struct PrivateTag
37 {
38 };
39
40 public:
42
51 static ServerPtr Create(const String &ipAddress, int port, const ConverterPtr &converter)
52 {
53 return Internal::DoResCallShareOut<Server>(
54 [&](void *&handle) {
55 return CVB_CALL_CAPI(CVWSCreateServerTyped(ipAddress.c_str(), static_cast<CExports::cvbval_t>(port),
56 converter->Handle(), handle));
57 },
58 ipAddress, port, converter);
59 }
60
62
70 static ServerPtr Create(const String &ipAddress, int port)
71 {
72 CExports::CVWSCONVERTER converter = nullptr;
73 CVB_CALL_CAPI_CHECKED(CVWSCreateConverter(CExports::CVWSCT_Raw, converter));
74 HandleGuard<Converter> converterGuard(converter);
75 return Internal::DoResCallShareOut<Server>(
76 [&](void *&handle) {
77 return CVB_CALL_CAPI(CVWSCreateServerTyped(ipAddress.c_str(), static_cast<CExports::cvbval_t>(port),
78 converterGuard.Handle(), handle));
79 },
80 ipAddress, port);
81 }
82
83 template <class T>
84 static ServerPtr FromHandle(HandleGuard<Server> &&guard, const String &ipAddress, int port,
85 const ConverterPtr &converter = {})
86 {
87 static_assert(std::is_same<Server, T>::value, "CVB: T must be a Server");
88 if (!guard.Handle())
89 throw std::runtime_error("server handle invalid");
90 return std::make_shared<T>(std::move(guard), ipAddress, port, converter, PrivateTag{});
91 }
92
93 Server(HandleGuard<Server> &&guard, const String &ipAddress, int port, const ConverterPtr &converter,
94 PrivateTag) noexcept
95 : handle_(std::move(guard))
96 , ipAddress_(ipAddress)
97 , port_(port)
98 , converter_(converter)
99 {
100 }
101
103
108 {
109 CExports::cvbint64_t value = -1;
110 CVB_CALL_CAPI_CHECKED(CVWSServerGetPropertyAsInteger(Handle(), CExports::CVWSSP_ActiveConnections, value));
111 return static_cast<int>(value);
112 }
113
115
122 void SendSync(const Image &image)
123 {
124 CVB_CALL_CAPI_CHECKED(CVWSStreamingSyncSendImage(Handle(), image.Handle()));
125 }
126
128
135 void SendAsync(const Image &image)
136 {
137 CVB_CALL_CAPI_CHECKED(CVWSStreamingAsyncSendImage(Handle(), image.Handle()));
138 }
139
145 void *Handle() const noexcept
146 {
147 return handle_.Handle();
148 }
149
151
156 String IPAddress() const noexcept
157 {
158 return ipAddress_;
159 }
160
162
167 int Port() const noexcept
168 {
169 return port_;
170 }
171
172 private:
173 HandleGuard<Server> handle_;
174 String ipAddress_;
175 int port_;
176 ConverterPtr converter_;
177 };
178
179 } // namespace WebStreaming
180
181 CVB_END_INLINE_NS
182
183} // namespace Cvb
The Common Vision Blox image.
Definition decl_image.hpp:45
void * Handle() const noexcept
Classic API image handle.
Definition decl_image.hpp:232
void SendAsync(const Image &image)
Sends an image asynchronously.
Definition server.hpp:135
static ServerPtr Create(const String &ipAddress, int port, const ConverterPtr &converter)
Creates a WebSocket server with given converter.
Definition server.hpp:51
String IPAddress() const noexcept
Get the IP address of the server.
Definition server.hpp:156
int ActiveConnections() const
Get the number of active connections.
Definition server.hpp:107
static ServerPtr Create(const String &ipAddress, int port)
Creates a WebSocket server.
Definition server.hpp:70
int Port() const noexcept
Get destination port.
Definition server.hpp:167
void SendSync(const Image &image)
Sends an image synchronously.
Definition server.hpp:122
void * Handle() const noexcept
Returns C-API style handle to Server Object.
Definition server.hpp:145
cvbbool_t ReleaseObject(OBJ &Object)
T make_shared(T... args)
T move(T... args)
Namespace for streaming images via web sockets.
Definition converter.hpp:20
std::shared_ptr< Server > ServerPtr
Convenience shared pointer for Server.
Definition webstreaming.hpp:34
std::shared_ptr< Converter > ConverterPtr
Convenience shared pointer for Converter.
Definition webstreaming.hpp:22
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49