mirror of https://github.com/n7tae/tcd.git
parent
3d3ff08eda
commit
08ca65ec2f
@ -1,219 +0,0 @@
|
|||||||
|
|
||||||
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
|
|
||||||
// Copyright © 2021 Thomas A. Early N7TAE
|
|
||||||
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/un.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "UnixPacketSocket.h"
|
|
||||||
|
|
||||||
CUnixPacket::CUnixPacket() : m_fd(-1), m_host(NULL) {}
|
|
||||||
|
|
||||||
bool CUnixPacket::Receive(std::vector<uint8_t> &Buffer, unsigned timeout)
|
|
||||||
{
|
|
||||||
// socket valid ?
|
|
||||||
if ( 0 > m_fd )
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// control socket
|
|
||||||
fd_set FdSet;
|
|
||||||
FD_ZERO(&FdSet);
|
|
||||||
FD_SET(m_fd, &FdSet);
|
|
||||||
struct timeval tv;
|
|
||||||
tv.tv_sec = timeout / 1000;
|
|
||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
|
||||||
|
|
||||||
auto rval = select(m_fd + 1, &FdSet, 0, 0, &tv);
|
|
||||||
if (rval < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "select error on Unix socket " << m_name << ": " << strerror(errno) << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rval > 0)
|
|
||||||
{
|
|
||||||
uint8_t buf[USB3XXX_MAXPACKETSIZE];
|
|
||||||
auto len = read(m_fd, buf, USB3XXX_MAXPACKETSIZE);
|
|
||||||
if (len <= 0)
|
|
||||||
return true;
|
|
||||||
Buffer.resize(len);
|
|
||||||
memcpy(Buffer.data(), buf, len);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Restart();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
bool CUnixPacket::Write(const void *buffer, const ssize_t size)
|
|
||||||
{
|
|
||||||
if (0 > m_fd)
|
|
||||||
return true;
|
|
||||||
ssize_t written = write(m_fd, buffer, size);
|
|
||||||
if (written != size)
|
|
||||||
{
|
|
||||||
if (-1 == written)
|
|
||||||
{
|
|
||||||
std::cerr << "Write error on '" << m_name << "': " << strerror(errno) << std::endl;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cout << "Write error on '" << m_name << "': Only wrote " << written << " of " << size << " bytes" << std::endl;
|
|
||||||
}
|
|
||||||
return Restart();
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CUnixPacket::Restart()
|
|
||||||
{
|
|
||||||
if (! m_host->IsRunning())
|
|
||||||
return true;
|
|
||||||
std::cout << "Restarting '" << m_name << "'... " << std::endl;
|
|
||||||
Close();
|
|
||||||
std::string name(m_name);
|
|
||||||
return Open(name.c_str(), m_host);
|
|
||||||
}
|
|
||||||
|
|
||||||
int CUnixPacket::GetFD()
|
|
||||||
{
|
|
||||||
return m_fd;
|
|
||||||
}
|
|
||||||
|
|
||||||
CUnixPacketServer::CUnixPacketServer() : m_server(-1) {}
|
|
||||||
|
|
||||||
CUnixPacketServer::~CUnixPacketServer()
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CUnixPacketServer::Open(const char *name, CController *host)
|
|
||||||
{
|
|
||||||
m_server = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
|
||||||
m_host = host;
|
|
||||||
if (m_server < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot open '" << name << "' socket: " << strerror(errno) << std::endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr_un addr;
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.sun_family = AF_UNIX;
|
|
||||||
memcpy(addr.sun_path+1, name, strlen(name));
|
|
||||||
if (-1 == bind(m_server, (struct sockaddr *)&addr, sizeof(addr)))
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot bind '" << name << "' socket: " << strerror(errno) << std::endl;
|
|
||||||
Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (-1 == listen(m_server, 1))
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot listen on '" << name << "' socket: " << strerror(errno) << std::endl;
|
|
||||||
Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_fd = accept(m_server, nullptr, 0);
|
|
||||||
if (m_fd < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot accept on '" << name << "' socket: " << strerror(errno) << std::endl;
|
|
||||||
Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
strncpy(m_name, name, 108);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUnixPacketServer::Close()
|
|
||||||
{
|
|
||||||
if (m_server >= 0)
|
|
||||||
{
|
|
||||||
close(m_server);
|
|
||||||
m_server = -1;
|
|
||||||
}
|
|
||||||
if (m_fd >= 0)
|
|
||||||
{
|
|
||||||
close(m_fd);
|
|
||||||
m_fd = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CUnixPacketClient::~CUnixPacketClient()
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CUnixPacketClient::Open(const char *name, CController *host)
|
|
||||||
{
|
|
||||||
m_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
|
|
||||||
if (m_fd < 0)
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot open unix client socket " << name << std::endl;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct sockaddr_un addr;
|
|
||||||
memset(&addr, 0, sizeof(addr));
|
|
||||||
addr.sun_family = AF_UNIX;
|
|
||||||
memcpy(addr.sun_path+1, name, strlen(name));
|
|
||||||
int rval = -1;
|
|
||||||
int tries = 0;
|
|
||||||
while (rval < 0)
|
|
||||||
{
|
|
||||||
rval = connect(m_fd, (struct sockaddr *)&addr, sizeof(addr));
|
|
||||||
if (rval < 0)
|
|
||||||
{
|
|
||||||
if (ECONNREFUSED == errno)
|
|
||||||
{
|
|
||||||
if (0 == tries++ % 20)
|
|
||||||
std::cout << "Waiting for " << name << " server to start..." << std::endl;
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
std::cerr << "Cannot connect '" << name << "' socket: " << strerror(errno) << std::endl;
|
|
||||||
Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (! m_host->IsRunning())
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_host = host;
|
|
||||||
strncpy(m_name, name, 108);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUnixPacketClient::Close()
|
|
||||||
{
|
|
||||||
if (m_fd >= 0)
|
|
||||||
{
|
|
||||||
close(m_fd);
|
|
||||||
m_fd = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,58 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
|
|
||||||
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
|
|
||||||
// Copyright © 2021 Thomas A. Early N7TAE
|
|
||||||
|
|
||||||
// This program is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
|
|
||||||
// This program is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU General Public License for more details.
|
|
||||||
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include "Controller.h"
|
|
||||||
|
|
||||||
class CUnixPacket
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CUnixPacket();
|
|
||||||
virtual bool Open(const char *name, CController *host) = 0;
|
|
||||||
virtual void Close() = 0;
|
|
||||||
bool Write(const void *buffer, const ssize_t size);
|
|
||||||
bool Receive(std::vector<uint8_t> &buf, unsigned timeout);
|
|
||||||
int GetFD();
|
|
||||||
protected:
|
|
||||||
bool Restart();
|
|
||||||
int m_fd;
|
|
||||||
CController *m_host;
|
|
||||||
char m_name[108];
|
|
||||||
};
|
|
||||||
|
|
||||||
class CUnixPacketServer : public CUnixPacket
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CUnixPacketServer();
|
|
||||||
~CUnixPacketServer();
|
|
||||||
bool Open(const char *name, CController *host);
|
|
||||||
void Close();
|
|
||||||
protected:
|
|
||||||
int m_server;
|
|
||||||
};
|
|
||||||
|
|
||||||
class CUnixPacketClient : public CUnixPacket
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
~CUnixPacketClient();
|
|
||||||
bool Open(const char *name, CController *host);
|
|
||||||
void Close();
|
|
||||||
};
|
|
||||||
Loading…
Reference in new issue