From 2bea84e0c9a1aef4f9cadc08a9f55e255c85c162 Mon Sep 17 00:00:00 2001 From: Tom Early Date: Thu, 10 Jan 2019 08:40:30 -0700 Subject: [PATCH] rewrote CUnixDgramWriter --- QnetDVAP.cpp | 4 ++-- QnetDVRPTR.cpp | 4 ++-- QnetGateway.cpp | 8 +++---- QnetITAP.cpp | 7 +++--- QnetLink.cpp | 5 ++--- QnetRelay.cpp | 4 ++-- QnetRemote.cpp | 6 +---- UnixDgramSocket.cpp | 54 ++++++++++++++++----------------------------- UnixDgramSocket.h | 7 +++--- 9 files changed, 37 insertions(+), 62 deletions(-) diff --git a/QnetDVAP.cpp b/QnetDVAP.cpp index 8b9b267..a1b9cd2 100644 --- a/QnetDVAP.cpp +++ b/QnetDVAP.cpp @@ -252,7 +252,8 @@ static bool read_config(const char *cfgFile) static int open_sock() { - if (Gate2Modem.Open(gate2modem.c_str()) || Modem2Gate.Open(modem2gate.c_str())) + Modem2Gate.SetUp(modem2gate.c_str()); + if (Gate2Modem.Open(gate2modem.c_str())) return 1; return 0; } @@ -648,7 +649,6 @@ int main(int argc, const char **argv) readthread.get(); Gate2Modem.Close(); - Modem2Gate.Close(); printf("dvap_rptr exiting\n"); return 0; } diff --git a/QnetDVRPTR.cpp b/QnetDVRPTR.cpp index c9fc4cb..68eb1a8 100644 --- a/QnetDVRPTR.cpp +++ b/QnetDVRPTR.cpp @@ -2559,7 +2559,8 @@ int main(int argc, const char **argv) strcpy(DVCALL_and_MOD, DVCALL); DVCALL_and_MOD[7] = DVRPTR_MOD; - if (Gate2Modem.Open(gate2modem.c_str()) || Modem2Gate.Open(modem2gate.c_str())) + Modem2Gate.SetUp(modem2gate.c_str()); + if (Gate2Modem.Open(gate2modem.c_str())) return 1; if (RX_Inverse == true) { @@ -3087,7 +3088,6 @@ int main(int argc, const char **argv) } } - Modem2Gate.Close(); Gate2Modem.Close(); printf("dvrptr exiting...\n"); diff --git a/QnetGateway.cpp b/QnetGateway.cpp index a2767f3..2f3c950 100644 --- a/QnetGateway.cpp +++ b/QnetGateway.cpp @@ -2451,14 +2451,14 @@ int CQnetGateway::Init(char *cfgfile) } // Open unix sockets between qngateway and qnlink - if (Gate2Link.Open(gate2link.c_str())) - return 1; + Gate2Link.SetUp(gate2link.c_str()); if (Link2Gate.Open(link2gate.c_str())) return 1; for (i=0; i<3; i++) { if (rptr.mod[i].defined) { // open unix sockets between qngateway and each defined modem - if (Gate2Modem[i].Open(gate2modem[i].c_str()) || Modem2Gate[i].Open(modem2gate[i].c_str())) + Gate2Modem[i].SetUp(gate2modem[i].c_str()); + if (Modem2Gate[i].Open(modem2gate[i].c_str())) return 1; } // recording for echotest on local repeater modules @@ -2536,10 +2536,8 @@ CQnetGateway::CQnetGateway() CQnetGateway::~CQnetGateway() { - Gate2Link.Close(); Link2Gate.Close(); for (int i=0; i<3; i++) { - Gate2Modem[i].Close(); Modem2Gate[i].Close(); } diff --git a/QnetITAP.cpp b/QnetITAP.cpp index f2db07b..bb61e56 100644 --- a/QnetITAP.cpp +++ b/QnetITAP.cpp @@ -78,7 +78,8 @@ bool CQnetITAP::Initialize(const char *cfgfile) return true; } - if (Gate2Modem.Open(gate2modem.c_str()) || Modem2Gate.Open(modem2gate.c_str())) + Modem2Gate.SetUp(modem2gate.c_str()); + if (Gate2Modem.Open(gate2modem.c_str())) return true; return false; @@ -197,8 +198,7 @@ void CQnetITAP::Run(const char *cfgfile) return; int ug2m = Gate2Modem.GetFD(); - int um2g = Modem2Gate.GetFD(); - printf("gate2modem=%d, modem2gate=%d serial=%d\n", ug2m, um2g, serfd); + printf("gate2modem=%d, serial=%d\n", ug2m, serfd); keep_running = true; unsigned poll_counter = 0; @@ -300,7 +300,6 @@ void CQnetITAP::Run(const char *cfgfile) ::close(serfd); Gate2Modem.Close(); - Modem2Gate.Close(); } int CQnetITAP::SendTo(const unsigned char length, const unsigned char *buf) diff --git a/QnetLink.cpp b/QnetLink.cpp index ae1af44..e67e425 100644 --- a/QnetLink.cpp +++ b/QnetLink.cpp @@ -667,7 +667,8 @@ bool CQnetLink::srv_open() } /* create our gateway unix sockets */ - if (Gate2Link.Open(gate2link.c_str()) || Link2Gate.Open(link2gate.c_str())) { + Link2Gate.SetUp(link2gate.c_str()); + if (Gate2Link.Open(gate2link.c_str())) { close(dcs_g2_sock); dcs_g2_sock = -1; close(xrf_g2_sock); @@ -675,7 +676,6 @@ bool CQnetLink::srv_open() close(ref_g2_sock); ref_g2_sock = -1; Gate2Link.Close(); - Link2Gate.Close(); return false; } @@ -707,7 +707,6 @@ void CQnetLink::srv_close() } Gate2Link.Close(); - Link2Gate.Close(); if (ref_g2_sock != -1) { close(ref_g2_sock); diff --git a/QnetRelay.cpp b/QnetRelay.cpp index 81ca076..9b0b69a 100644 --- a/QnetRelay.cpp +++ b/QnetRelay.cpp @@ -127,7 +127,8 @@ bool CQnetRelay::Run(const char *cfgfile) if (msock < 0) return true; - if (Gate2Modem.Open(gate2modem.c_str()) || Modem2Gate.Open(modem2gate.c_str())) + Modem2Gate.SetUp(modem2gate.c_str()); + if (Gate2Modem.Open(gate2modem.c_str())) return true; int fd = Gate2Modem.GetFD(); @@ -206,7 +207,6 @@ bool CQnetRelay::Run(const char *cfgfile) ::close(msock); Gate2Modem.Close(); - Modem2Gate.Close(); return false; } diff --git a/QnetRemote.cpp b/QnetRemote.cpp index 7d72e14..78c3380 100644 --- a/QnetRemote.cpp +++ b/QnetRemote.cpp @@ -198,8 +198,7 @@ int main(int argc, char *argv[]) CUnixDgramWriter ToGateway; std::string togateway("modem2gate"); togateway.append(1, module-'A'+'0'); - if (ToGateway.Open(togateway.c_str())) - return 1; + ToGateway.SetUp(togateway.c_str()); SDSTR pkt; memcpy(pkt.pkt_id,"DSTR", 4); @@ -241,7 +240,6 @@ int main(int argc, char *argv[]) int sent = ToGateway.Write(pkt.pkt_id, 58); if (sent != 58) { printf("%s: ERROR: Couldn't send header!\n", argv[0]); - ToGateway.Close(); return 1; } @@ -313,10 +311,8 @@ int main(int argc, char *argv[]) sent = ToGateway.Write(pkt.pkt_id, 29); if (sent != 29) { printf("%s: ERROR: could not send voice packet %d\n", argv[0], i); - ToGateway.Close(); return 1; } } - ToGateway.Close(); return 0; } diff --git a/UnixDgramSocket.cpp b/UnixDgramSocket.cpp index ca22754..67f776a 100644 --- a/UnixDgramSocket.cpp +++ b/UnixDgramSocket.cpp @@ -22,7 +22,6 @@ #include #include #include -#include #include "UnixDgramSocket.h" @@ -75,51 +74,36 @@ int CUnixDgramReader::GetFD() return fd; } -CUnixDgramWriter::CUnixDgramWriter() : fd(-1) {} +CUnixDgramWriter::CUnixDgramWriter() {} -CUnixDgramWriter::~CUnixDgramWriter() -{ - Close(); -} +CUnixDgramWriter::~CUnixDgramWriter() {} -bool CUnixDgramWriter::Open(const char *path) // returns true on failure +void CUnixDgramWriter::SetUp(const char *path) // returns true on failure { - fd = socket(AF_UNIX, SOCK_DGRAM, 0); - if (fd < 0) { - fprintf(stderr, "CUnixDgramWriter::Open: socket() failed: %s\n", strerror(errno)); - return true; - } - - struct sockaddr_un addr; + // setup the socket address memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path+1, path, sizeof(addr.sun_path)-2); +} +ssize_t CUnixDgramWriter::Write(void *buf, size_t size) +{ + // open the socket + int fd = socket(AF_UNIX, SOCK_DGRAM, 0); + if (fd < 0) { + fprintf(stderr, "Failed to open socket %s\n", addr.sun_path+1); + return -1; + } + // connect to the receiver int rval = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); if (rval < 0) { - fprintf(stderr, "CUnixDgramWriter::Open: connect() failed: %s\n", strerror(errno)); + fprintf(stderr, "Failed to connect to socket %s\n", addr.sun_path+1); close(fd); - fd = -1; - return true; + return -1; } - return false; -} -ssize_t CUnixDgramWriter::Write(void *buf, size_t size) -{ - if (fd >= 0) - return write(fd, buf, size); - return -1; -} + ssize_t written = write(fd, buf, size); -void CUnixDgramWriter::Close() -{ - if (fd >= 0) - close(fd); - fd = -1; -} - -int CUnixDgramWriter::GetFD() -{ - return fd; + close(fd); + return written; } diff --git a/UnixDgramSocket.h b/UnixDgramSocket.h index 7242955..102d99f 100644 --- a/UnixDgramSocket.h +++ b/UnixDgramSocket.h @@ -18,6 +18,7 @@ */ #include +#include class CUnixDgramReader { @@ -37,10 +38,8 @@ class CUnixDgramWriter public: CUnixDgramWriter(); ~CUnixDgramWriter(); - bool Open(const char *path); + void SetUp(const char *path); ssize_t Write(void *buf, size_t size); - void Close(); - int GetFD(); private: - int fd; + struct sockaddr_un addr; };