owlps/owlps-positioner/outputnetworksocket.cc

76 lines
1.7 KiB
C++

/*
* This file is part of the Owl Positioning System (OwlPS) project.
* It is subject to the copyright notice and license terms in the
* COPYRIGHT.t2t file found in the top-level directory of this
* distribution and at
* https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
* No part of the OwlPS Project, including this file, may be copied,
* modified, propagated, or distributed except according to the terms
* contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
* distributed along with this file, either separately or by replacing
* this notice by the COPYRIGHT.t2t file's contents.
*/
#include "outputnetworksocket.hh"
#include "resultlist.hh"
#include "result.hh"
#include "posexcept.hh"
#include <cstdio> // For perror()
#include <unistd.h> // For close()
using namespace std ;
/* *** Constructors *** */
OutputNetworkSocket::~OutputNetworkSocket()
{
close_socket() ;
}
/* *** Operations *** */
/**
* Normally, the socket is closed automatically by the destructor. Use
* this if you want to close the socket prematurely.
* #sockfd is set to -1, even in case of error.
* @returns `true` if the socket were successfully closed or were not
* opened.
* @returns `false` in case of error.
*/
bool OutputNetworkSocket::close_socket()
{
if (sockfd >= 0)
{
if (close(sockfd))
{
perror("Cannot close network socket") ;
return false ;
}
sockfd = -1 ;
}
return true ;
}
void OutputNetworkSocket::write(const Result &result)
{
send_data(result.to_csv()) ;
}
void OutputNetworkSocket::write(const ResultList &results)
{
if (! results.empty())
send_data(results.to_csv()) ;
}