w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rw11VirtTermPty.cpp
Go to the documentation of this file.
1// $Id: Rw11VirtTermPty.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2013-2019 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2019-02-23 1114 1.0.5 use std::bind instead of lambda
8// 2018-12-15 1082 1.0.4 use lambda instead of boost::bind
9// 2018-10-27 1059 1.0.3 coverity fixup (uncaught exception in dtor)
10// 2017-04-15 875 1.0.2 Open(): set default scheme
11// 2017-04-07 868 1.0.1 Dump(): add detail arg
12// 2013-03-06 495 1.0 Initial version
13// 2013-02-24 492 0.1 First draft
14// ---------------------------------------------------------------------------
15
19#define _XOPEN_SOURCE 600
20
21#include <stdlib.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <unistd.h>
25
26#include <functional>
27
28#include "librtools/RosFill.hpp"
29#include "Rw11VirtTermPty.hpp"
30
31using namespace std;
32using namespace std::placeholders;
33
39// all method definitions in namespace Retro
40namespace Retro {
41
42//------------------------------------------+-----------------------------------
44
46 : Rw11VirtTerm(punit),
47 fFd(-1)
48{}
49
50//------------------------------------------+-----------------------------------
52
54{
55 if (fFd>=2) {
56 Rtools::Catch2Cerr(__func__,
57 [this](){ Server().RemovePollHandler(fFd); } );
58 ::close(fFd);
59 }
60}
61
62//------------------------------------------+-----------------------------------
64
65bool Rw11VirtTermPty::Open(const std::string& url, RerrMsg& emsg)
66{
67 if (!fUrl.Set(url, "", "pty", emsg)) return false;
68
69 int fd = posix_openpt(O_RDWR);
70 if (fd < 0) {
71 emsg.InitErrno("Rw11VirtTermPty::Open", "posix_openpt() failed: ", errno);
72 return false;
73 }
74
75 int irc = grantpt(fd);
76 if (irc < 0) {
77 emsg.InitErrno("Rw11VirtTermPty::Open", "grantpt() failed: ", errno);
78 ::close(fd);
79 return false;
80 }
81
82 irc = unlockpt(fd);
83 if (irc < 0) {
84 emsg.InitErrno("Rw11VirtTermPty::Open", "unlockpt() failed: ", errno);
85 ::close(fd);
86 return false;
87 }
88
89 char* pname = ptsname(fd);
90 if (pname == nullptr) {
91 emsg.InitErrno("Rw11VirtTermPty::Open", "ptsname() failed: ", errno);
92 ::close(fd);
93 return false;
94 }
95
96 fFd = fd;
97 fChannelId = pname;
98
100 fFd, POLLIN);
101
102 return true;
103}
104
105//------------------------------------------+-----------------------------------
107
108bool Rw11VirtTermPty::Snd(const uint8_t* data, size_t count, RerrMsg& emsg)
109{
111 ssize_t irc = write(fFd, data, count);
112 if (irc != ssize_t(count)) {
113 emsg.InitErrno("Rw11VirtTermPty::Snd", "write() failed: ", errno);
114 return false;
115 }
116 fStats.Inc(kStatNVTSndByt, double(count));
117 return true;
118}
119
120//------------------------------------------+-----------------------------------
122
124{
126 // bail-out and cancel handler if poll returns an error event
127 if (pfd.revents & (~pfd.events)) return -1;
128
129 uint8_t buf[1024];
130 ssize_t irc = read(fFd, buf, 1024);
131
132 if (irc > 0) {
133 fRcvCb(buf, size_t(irc));
134 fStats.Inc(kStatNVTRcvByt, double(irc));
135 }
136
137 return 0;
138}
139
140//------------------------------------------+-----------------------------------
142
143void Rw11VirtTermPty::Dump(std::ostream& os, int ind, const char* text,
144 int detail) const
145{
146 RosFill bl(ind);
147 os << bl << (text?text:"--") << "Rw11VirtTermPty @ " << this << endl;
148
149 os << bl << " fFd: " << fFd << endl;
150 Rw11VirtTerm::Dump(os, ind+2, "", detail);
151 return;
152}
153
154
155} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void InitErrno(const std::string &meth, const std::string &text, int errnum)
FIXME_docs.
Definition: RerrMsg.cpp:84
void RemovePollHandler(int fd, short events, bool nothrow=false)
FIXME_docs.
void AddPollHandler(pollhdl_t &&pollhdl, int fd, short events=POLLIN)
FIXME_docs.
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
bool Set(const std::string &url, const std::string &optlist, RerrMsg &emsg)
FIXME_docs.
Definition: RparseUrl.cpp:55
void Inc(size_t ind, double val=1.)
FIXME_docs.
Definition: Rstats.ipp:29
FIXME_docs.
Definition: Rw11Unit.hpp:39
virtual bool Snd(const uint8_t *data, size_t count, RerrMsg &emsg)
FIXME_docs.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
int RcvPollHandler(const pollfd &pfd)
FIXME_docs.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
Rw11VirtTermPty(Rw11Unit *punit)
Default constructor.
int fFd
fd for pty master side
rcvcbfo_t fRcvCb
receive callback fobj
std::string fChannelId
channel id
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
RparseUrl fUrl
Definition: Rw11Virt.hpp:66
Rstats fStats
statistics
Definition: Rw11Virt.hpp:68
RlinkServer & Server() const
FIXME_docs.
Definition: Rw11Virt.ipp:55
void Catch2Cerr(const char *msg, std::function< void()> func)
FIXME_docs.
Definition: Rtools.cpp:170
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47