CVB++ 15.0
decl_server.hpp
1#pragma once
2
3#include <atomic>
4#include <iostream>
5#include <mutex>
6#include <thread>
7
8#include "../../exception.hpp"
9#include "../../global.hpp"
10#include "../../size_2d.hpp"
11#include "../../utilities/system_info.hpp"
12#include "../logical_network_interface.hpp"
13
14#include "../gevserver.hpp"
15
16#include "../../pfnc_format.hpp"
17
18namespace Cvb
19{
20 CVB_BEGIN_INLINE_NS
21
22 template <>
23 inline HandleGuard<GevServer::Server>::HandleGuard(void *handle) noexcept
24 : HandleGuard<GevServer::Server>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
25 {
26 }
27
28 namespace GevServer
29 {
31 enum class State
32 {
33 Configuration,
34 Disconnected,
35 Connected,
36 AcquisitionEnabled
37 };
38
41
43 class Server : public std::enable_shared_from_this<Server>
44 {
45 public:
61
75 static ServerPtr CreateWithConstSize(Size2D<int> size, PfncFormat pixelFormat,
77
97 static ServerPtr CreateWithConstSize(Size2D<int> size, ColorModel colorModel, DataType dataType,
99
120 static ServerPtr CreateWithVariableSize(Size2D<int> maxSize, ColorModel colorModel, DataType dataType,
122
137 static ServerPtr CreateWithVariableSize(Size2D<int> maxSize, PfncFormat pixelFormat,
139
141
147 void *Handle() const noexcept
148 {
149 return handle_.Handle();
150 }
151
156
159
181 String UserVersion() const;
182
204 void SetUserVersion(const String &value);
205
218
232
234 NodeMapPtr NodeMap() const;
235
251 StreamPtr Stream() const;
252
265 void Start(const Cvb::NetworkConnection::IPAddress &address);
266
280 void Start(const Cvb::NetworkConnection::IPAddressUInt &address);
281
292 void Stop();
293
294 protected:
295 std::string GetLastGSErrorMessage() const noexcept
296 {
297 std::size_t messageSize = 0;
298 auto res = CVB_CALL_CAPI(GSGetLastErrorString(nullptr, messageSize));
299 if (res || messageSize < 2)
300 return {};
301 std::vector<char> message(messageSize);
302 if (CVB_CALL_CAPI(GSGetLastErrorString(message.data(), messageSize)))
303 return {};
304 return std::string(message.data());
305 }
306
307 void NativeCall(std::function<CExports::cvbres_t()> fn) const
308 {
309 auto result = fn();
310 if (result < 0)
311 {
312 auto message = GetLastGSErrorMessage();
313 if (message.empty())
314 message = "Error";
315
316 std::stringstream stream;
317 stream << "Server " << message;
318 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
319 }
320 }
321
322 template <class T>
323 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
324 {
325 T value;
326 auto result = fn(value);
327 if (result < 0)
328 {
330 if (message.empty())
331 message = "Error";
332
333 std::stringstream stream;
334 stream << "Server " << message;
335 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
336 }
337 return value;
338 }
339
340 String GetInfoAsString(Info command) const;
341
342 private:
343 // Adopts the given handle for the newly created object.
344 Server(HandleGuard<Server> &&guard, bool hasStream)
345 : handle_(std::move(guard))
346 , state_{GevServer::State::Configuration}
347 , hasStream_{hasStream}
348 {
349 if (!handle_.Handle())
350 throw std::runtime_error("handle to create GevServer must not be empty");
351
352 std::size_t dummy{0};
353 NativeCall([&]() {
354 return CVB_CALL_CAPI(
355 GSRegisterEvent(Handle(), CExports::TServerEvent::SE_Connected, &Server::OnConnected, this, dummy));
356 });
357 NativeCall([&]() {
358 return CVB_CALL_CAPI(
359 GSRegisterEvent(Handle(), CExports::TServerEvent::SE_Disconnected, &Server::OnDisconnected, this, dummy));
360 });
361 NativeCall([&]() {
362 return CVB_CALL_CAPI(GSRegisterEvent(Handle(), CExports::TServerEvent::SE_AcquisitionStart,
363 &Server::OnAcquisitionStart, this, dummy));
364 });
365 NativeCall([&]() {
366 return CVB_CALL_CAPI(GSRegisterEvent(Handle(), CExports::TServerEvent::SE_AcquisitionStop,
367 &Server::OnAcquisitionStop, this, dummy));
368 });
369 }
370
371 static void __stdcall OnConnected(void *pPrivate)
372 {
373 auto server = reinterpret_cast<Server *>(pPrivate);
374 server->SetState(GevServer::State::Connected);
375 }
376
377 static void __stdcall OnDisconnected(void *pPrivate)
378 {
379 auto server = reinterpret_cast<Server *>(pPrivate);
380 server->SetState(GevServer::State::Disconnected);
381 }
382
383 static void __stdcall OnAcquisitionStart(void *pPrivate)
384 {
385 auto server = reinterpret_cast<Server *>(pPrivate);
386 server->SetState(GevServer::State::AcquisitionEnabled);
387 }
388
389 static void __stdcall OnAcquisitionStop(void *pPrivate)
390 {
391 auto server = reinterpret_cast<Server *>(pPrivate);
392 server->SetState(GevServer::State::Connected);
393 }
394
395 void SetState(const GevServer::State &state) noexcept;
396
397 void CreateNodeMap();
398
399 HandleGuard<Server> handle_;
400 std::atomic<GevServer::State> state_;
401 NodeMapPtr nodeMap_;
402 std::mutex mtx;
403 mutable Internal::AsyncRef<GevServer::Stream> gsStream_;
404 const bool hasStream_;
405 };
406 } // namespace GevServer
407 CVB_END_INLINE_NS
408} // namespace Cvb
Data type description for an image plane.
Definition data_type.hpp:23
String UserVersion() const
Gets the user defined version that is appended to the device version.
Definition detail_server.hpp:114
void Stop()
Stops this server.
Definition detail_server.hpp:156
void SetUserVersion(const String &value)
Sets the user defined version that is appended to the device version.
Definition detail_server.hpp:119
GevServer::DriverType DriverType()
Gets the GigE Vision driver used by this server object.
Definition detail_server.hpp:107
Cvb::NetworkConnection LocalEndpoint() const
Gets the local end point this server is bound to.
Definition detail_server.hpp:125
void Start(const Cvb::NetworkConnection::IPAddress &address)
Starts this server and binds it to the given address .
Definition detail_server.hpp:141
static ServerPtr CreateNonStreaming()
Creates a non-streaming GigE Vision server object.
Definition detail_server.hpp:30
static ServerPtr CreateWithVariableSize(Size2D< int > maxSize, ColorModel colorModel, DataType dataType, GevServer::DriverType driverType=GevServer::DriverType::Auto)
Creates a new Server object with a client configurable width, height and offsets.
Definition detail_server.hpp:70
GevServer::State State()
Gets the the current state this server is in.
Definition detail_server.hpp:102
NodeMapPtr NodeMap() const
Gets this servers node map.
Definition detail_server.hpp:16
Cvb::NetworkConnection RemoteEndpoint() const
Gets the remote end point this server is connected to.
Definition detail_server.hpp:133
static ServerPtr CreateWithConstSize(Size2D< int > size, PfncFormat pixelFormat, GevServer::DriverType driverType=GevServer::DriverType::Auto)
Creates a new Server object with a constant width and height.
Definition detail_server.hpp:37
StreamPtr Stream() const
Gets the Cvb::GevServer::Stream for sending Images or other data.
Definition detail_server.hpp:21
void * Handle() const noexcept
Classic API device handle.
Definition decl_server.hpp:147
Stores a pair of numbers that represents the width and the height of a subject, typically a rectangle...
Definition size_2d.hpp:20
cvbbool_t ReleaseObject(OBJ &Object)
T move(T... args)
Namespace for GevServer based device configuration.
Definition decl_int_swiss_knife_node.hpp:11
Info
General version and acquisition information.
Definition gevserver.hpp:190
std::shared_ptr< Stream > StreamPtr
Convenience shared pointer for Stream.
Definition gevserver.hpp:49
State
The possible states this GevServer can be in.
Definition decl_server.hpp:32
DriverType
GigE Vision driver to use for communication and streaming.
Definition gevserver.hpp:139
@ Auto
Auto select driver type.
Definition gevserver.hpp:140
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition gevserver.hpp:45
std::shared_ptr< Server > ServerPtr
Convenience shared pointer for GevServer.
Definition gevserver.hpp:37
@ String
Node is a string node (no reg).
Definition gevserver.hpp:177
PfncFormat
GenICam Pixel Format Naming Convention (PFNC) format values.
Definition pfnc_format.hpp:34
std::string GetLastErrorMessage()
Returns the last error message.
Definition system_info.hpp:167
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
ColorModel
Color model that this image is using.
Definition global.hpp:176
Basic network connection operations.
Definition network_connection.hpp:15