CVB++ 15.0
Loading...
Searching...
No Matches
decl_server_base.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 "../../driver/driver.hpp"
15#include "../gevserver.hpp"
16#include "../../pfnc_format.hpp"
17
18namespace Cvb
19{
20 CVB_BEGIN_INLINE_NS
21
22 namespace GevServer
23 {
27 enum class State
28 {
29 Configuration, // the state after creation of the server, before starting it
30 Disconnected, // the state after a connection was torn down
31 Connected, // the state after a configured server was started
32 AcquisitionEnabled // the state after a client started acquisition on the server
33 };
34
38 class ServerBase : public std::enable_shared_from_this<ServerBase>
39 {
40 protected:
41 struct ProtectedTag
42 {
43 };
44
45 public:
50 virtual void *Handle() const noexcept = 0;
51
55 virtual GevServer::State State();
56
59
81 virtual String UserVersion() const;
82
104 virtual void SetUserVersion(const String &value);
105
117 virtual Cvb::NetworkConnection LocalEndpoint() const;
118
131 virtual Cvb::NetworkConnection RemoteEndpoint() const;
132
137 virtual NodeMapPtr NodeMap() const;
138
151 virtual void Start(const Cvb::NetworkConnection::IPAddress &address);
152
166 virtual void Start(const Cvb::NetworkConnection::IPAddressUInt &address);
167
178 virtual void Stop();
179
180 protected:
181 explicit ServerBase(bool hasStream)
182 : state_{GevServer::State::Configuration}
183 , hasStream_{hasStream}
184 {
185 }
186
187 void RegisterEvents();
188
189 std::string GetLastGSErrorMessage() const noexcept
190 {
191 std::size_t messageSize = 0;
192 auto res = CVB_CALL_CAPI(GSGetLastErrorString(nullptr, messageSize));
193 if (res || messageSize < 2)
194 return {};
195 std::vector<char> message(messageSize);
196 if (CVB_CALL_CAPI(GSGetLastErrorString(message.data(), messageSize)))
197 return {};
198 return std::string(message.data());
199 }
200
201 void NativeCall(std::function<CExports::cvbres_t()> fn) const
202 {
203 auto result = fn();
204 if (result < 0)
205 {
206 auto message = GetLastGSErrorMessage();
207 if (message.empty())
208 message = "Error";
209
210 std::stringstream stream;
211 stream << "ServerBase " << message;
212 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
213 }
214 }
215
216 template <class T>
217 T NativeCall(std::function<CExports::cvbres_t(T &value)> fn) const
218 {
219 T value;
220 auto result = fn(value);
221 if (result < 0)
222 {
224 if (message.empty())
225 message = "Error";
226
227 std::stringstream stream;
228 stream << "ServerBase " << message;
229 std::rethrow_exception(CvbException::FromCvbResult(result, stream.str()));
230 }
231 return value;
232 }
233
234 String GetInfoAsString(Info command) const;
235
236 static void __stdcall OnConnected(void *pPrivate)
237 {
238 auto server = reinterpret_cast<ServerBase *>(pPrivate);
239 server->SetState(GevServer::State::Connected);
240 }
241
242 static void __stdcall OnDisconnected(void *pPrivate)
243 {
244 auto server = reinterpret_cast<ServerBase *>(pPrivate);
245 server->SetState(GevServer::State::Disconnected);
246 }
247
248 static void __stdcall OnAcquisitionStart(void *pPrivate)
249 {
250 auto server = reinterpret_cast<ServerBase *>(pPrivate);
251 server->SetState(GevServer::State::AcquisitionEnabled);
252 }
253
254 static void __stdcall OnAcquisitionStop(void *pPrivate)
255 {
256 auto server = reinterpret_cast<ServerBase *>(pPrivate);
257 server->SetState(GevServer::State::Connected);
258 }
259
260 void SetState(const GevServer::State &state) noexcept;
261
262 const bool hasStream_;
263
264 private:
265 std::atomic<GevServer::State> state_;
266 mutable Internal::AsyncRef<GevServer::NodeMap> nodeMap_;
267 std::mutex mtx;
268 };
269 } // namespace GevServer
270 CVB_END_INLINE_NS
271} // namespace Cvb
virtual String UserVersion() const
Gets the user defined version that is appended to the device version.
Definition detail_server_base.hpp:35
virtual void Stop()
Stops this server.
Definition detail_server_base.hpp:77
virtual void SetUserVersion(const String &value)
Sets the user defined version that is appended to the device version.
Definition detail_server_base.hpp:40
virtual GevServer::DriverType DriverType()
Gets the GigE Vision driver used by this server object.
Definition detail_server_base.hpp:28
virtual Cvb::NetworkConnection LocalEndpoint() const
Gets the local end point this server is bound to.
Definition detail_server_base.hpp:46
virtual void * Handle() const noexcept=0
Returns the internal handle of this server object.
virtual void Start(const Cvb::NetworkConnection::IPAddress &address)
Starts this server and binds it to the given address .
Definition detail_server_base.hpp:62
virtual GevServer::State State()
Gets the the current state this server is in.
Definition detail_server_base.hpp:23
virtual NodeMapPtr NodeMap() const
Returns the GenApi node map of this server.
Definition detail_server_base.hpp:16
virtual Cvb::NetworkConnection RemoteEndpoint() const
Gets the remote end point this server is connected to.
Definition detail_server_base.hpp:54
Namespace for GevServer based device configuration.
Definition decl_int_swiss_knife_node.hpp:11
Info
General version and acquisition information.
Definition gevserver.hpp:231
State
These enum entries model the states the server can be in.
Definition decl_server_base.hpp:28
std::shared_ptr< NodeMap > NodeMapPtr
Convenience shared pointer for NodeMap.
Definition gevserver.hpp:56
@ String
Node is a string node (no reg).
Definition gevserver.hpp:218
std::string GetLastErrorMessage()
Returns the last error message.
Definition system_info.hpp:167
Root namespace for the Image Manager interface.
Definition version.hpp:11
Basic network connection operations.
Definition network_connection.hpp:15