rogue
Loading...
Searching...
No Matches
XvcServer.cpp
Go to the documentation of this file.
1
19
20#include <arpa/inet.h>
21#include <dlfcn.h>
22#include <netinet/in.h>
23#include <pthread.h>
24#include <sys/select.h>
25#include <sys/socket.h>
26
27#include <cmath>
28#include <string>
29
31
33
34rpx::XvcServer::XvcServer(uint16_t port, JtagDriver* drv, unsigned maxMsgSize) : drv_(drv), maxMsgSize_(maxMsgSize) {
35 struct sockaddr_in a;
36 int yes = 1;
37
38 a.sin_family = AF_INET;
39 a.sin_addr.s_addr = INADDR_ANY;
40 a.sin_port = htons(port);
41
42 if ((sd_ = ::socket(AF_INET, SOCK_STREAM, 0)) < 0)
43 throw(rogue::GeneralError::create("XvcServer::XvcServer()", "Failed to create socket"));
44
45 if (::setsockopt(sd_, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)))
46 throw(rogue::GeneralError::create("XvcServer::XvcServer()", "setsockopt(SO_REUSEADDR) failed"));
47
48 if (::bind(sd_, (struct sockaddr*)&a, sizeof(a)))
49 throw(rogue::GeneralError::create("XvcServer::XvcServer()", "Unable to bind Stream socket to local address"));
50
51 if (::listen(sd_, 1)) throw(rogue::GeneralError::create("XvcServer::XvcServer()", "Unable to listen on socket"));
52}
53
54rpx::XvcServer::~XvcServer() {
55 ::close(sd_);
56}
57
58void rpx::XvcServer::run(bool& threadEn, rogue::LoggingPtr log) {
59 fd_set rset;
60 int maxFd;
61 int nready;
62 struct timeval timeout;
63
64 while (threadEn) {
65 FD_ZERO(&rset);
66 FD_SET(sd_, &rset);
67
68 // 1 Second Timeout
69 timeout.tv_sec = 1;
70 timeout.tv_usec = 0;
71
72 nready = ::select(sd_ + 1, &rset, NULL, NULL, &timeout);
73
74 if (nready > 0 && FD_ISSET(sd_, &rset)) {
75 try {
76 XvcConnection conn(sd_, drv_, maxMsgSize_);
77 conn.run();
78 } catch (rogue::GeneralError& e) {
79 log->debug("Sub-connection failed");
80 }
81 }
82 }
83}
Generic Rogue exception type.
static GeneralError create(std::string src, const char *fmt,...)
Creates a formatted error instance.
Base transport driver for the AxisToJtag firmware protocol.
Definition JtagDriver.h:68
Manages one TCP client connection speaking the XVC protocol.
virtual void run()
Runs command processing loop for this connection.
std::shared_ptr< rogue::Logging > LoggingPtr
Shared pointer alias for Logging.
Definition Logging.h:159