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
13CVB_BEGIN_INLINE_NS
14
15template <>
16inline HandleGuard<WebStreaming::Server>::HandleGuard(void* handle) noexcept
17 : HandleGuard<WebStreaming::Server>(handle, [](void* handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
18{
19}
20
21namespace WebStreaming
22{
23
24
31class Server
32{
33 public:
34
35 using GuardType = HandleGuard<Server>;
36
37 private:
38
39 struct PrivateTag {};
40
41 public:
42
44
53 static ServerPtr Create(const String& ipAddress, int port, const ConverterPtr& converter)
54 {
55 return Internal::DoResCallShareOut<Server>([&](void*& handle)
56 {
57 return CVB_CALL_CAPI(CVWSCreateServerTyped(ipAddress.c_str(), static_cast<CExports::cvbval_t>(port), converter->Handle(), handle));
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>([&](void*& handle)
76 {
77 return CVB_CALL_CAPI(CVWSCreateServerTyped(ipAddress.c_str(), static_cast<CExports::cvbval_t>(port), converterGuard.Handle(), handle));
78 }, ipAddress, port);
79 }
80
81 template<class T>
82 static ServerPtr FromHandle(HandleGuard<Server>&& guard, const String& ipAddress, int port, const ConverterPtr& converter = {})
83 {
84 static_assert(std::is_same<Server, T>::value, "CVB: T must be a Server");
85 if (!guard.Handle())
86 throw std::runtime_error("server handle invalid");
87 return std::make_shared<T>(std::move(guard), ipAddress, port, converter, PrivateTag{});
88 }
89
90 Server(HandleGuard<Server>&& guard, const String& ipAddress, int port, const ConverterPtr& converter, PrivateTag) noexcept
91 : handle_(std::move(guard))
92 , ipAddress_(ipAddress)
93 , port_(port)
94 , converter_(converter)
95 {
96
97 }
98
100
105 {
106 CExports::cvbint64_t value = -1;
107 CVB_CALL_CAPI_CHECKED(CVWSServerGetPropertyAsInteger(Handle(), CExports::CVWSSP_ActiveConnections, value));
108 return static_cast<int>(value);
109 }
110
112
119 void SendSync(const Image& image)
120 {
121 CVB_CALL_CAPI_CHECKED(CVWSStreamingSyncSendImage(Handle(), image.Handle()));
122 }
123
125
132 void SendAsync(const Image& image)
133 {
134 CVB_CALL_CAPI_CHECKED(CVWSStreamingAsyncSendImage(Handle(), image.Handle()));
135 }
136
142 void* Handle() const noexcept
143 {
144 return handle_.Handle();
145 }
146
148
153 String IPAddress() const noexcept
154 {
155 return ipAddress_;
156 }
157
159
164 int Port() const noexcept
165 {
166 return port_;
167 }
168
169 private:
170
171 HandleGuard<Server> handle_;
172 String ipAddress_;
173 int port_;
174 ConverterPtr converter_;
175};
176
177}
178
179CVB_END_INLINE_NS
180
181}
The Common Vision Blox image.
Definition: decl_image.hpp:45
A WebSocket server.
Definition: server.hpp:32
void SendAsync(const Image &image)
Sends an image asynchronously.
Definition: server.hpp:132
static ServerPtr Create(const String &ipAddress, int port, const ConverterPtr &converter)
Creates a WebSocket server with given converter.
Definition: server.hpp:53
String IPAddress() const noexcept
Get the IP address of the server.
Definition: server.hpp:153
int ActiveConnections() const
Get the number of active connections.
Definition: server.hpp:104
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:164
void SendSync(const Image &image)
Sends an image synchronously.
Definition: server.hpp:119
void * Handle() const noexcept
Returns C-API style handle to Server Object.
Definition: server.hpp:142
std::shared_ptr< Converter > ConverterPtr
Convenience shared pointer for Converter.
Definition: webstreaming.hpp:22
Root namespace for the Image Manager interface.
Definition: c_barcode.h:24