72 lines
1.8 KiB
C++
72 lines
1.8 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
|
|
* http://code.lm7.fr/p/owlps/source/tree/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 "outputudpsocket.hh"
|
|
#include "request.hh"
|
|
#include "resultlist.hh"
|
|
#include "result.hh"
|
|
#include "posexcept.hh"
|
|
|
|
#include <owlps.h>
|
|
|
|
#include <cstdio> // For perror()
|
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
OutputUDPSocket::OutputUDPSocket(const string &_remote_host,
|
|
const uint_fast16_t _remote_port):
|
|
OutputNetworkSocket(_remote_host, _remote_port)
|
|
{
|
|
if (! init_socket())
|
|
throw error_opening_output_file("UDP socket") ;
|
|
}
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
/**
|
|
* @returns `true` if the socket were successfully opened.
|
|
* @returns `false` in case of error.
|
|
*/
|
|
bool OutputUDPSocket::init_socket()
|
|
{
|
|
sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port,
|
|
&server_info) ;
|
|
return sockfd >= 0 ;
|
|
}
|
|
|
|
|
|
/**
|
|
* Sends the text buffer 'data'.
|
|
*/
|
|
bool OutputUDPSocket::send_data(const string &data) const
|
|
{
|
|
unsigned int data_len = data.size() + 1 ; // +1 for the '\0'
|
|
ssize_t nsent = sendto(sockfd, data.c_str(), data_len, 0,
|
|
&server_info, sizeof(server_info)) ;
|
|
if (nsent != static_cast<ssize_t>(data_len))
|
|
{
|
|
perror("Error sending result data through the UDP socket") ;
|
|
return false ;
|
|
}
|
|
return true ;
|
|
}
|