owlps/owlps-positioner/outputudpsocket.cc

72 lines
1.8 KiB
C++
Raw Permalink Normal View History

/*
* 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
2016-11-03 05:10:34 +01:00
* 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 "outputudpsocket.hh"
2011-03-15 15:27:42 +01:00
#include "request.hh"
#include "resultlist.hh"
#include "result.hh"
#include "posexcept.hh"
#include <owlps.h>
2011-03-15 15:27:42 +01:00
#include <cstdio> // For perror()
2011-03-15 15:27:42 +01:00
using namespace std ;
/* *** Constructors *** */
OutputUDPSocket::OutputUDPSocket(const string &_remote_host,
const uint_fast16_t _remote_port):
OutputNetworkSocket(_remote_host, _remote_port)
2011-03-15 15:27:42 +01:00
{
if (! init_socket())
throw error_opening_output_file("UDP socket") ;
2011-03-15 15:27:42 +01:00
}
/* *** 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 ;
}