66 lines
1.5 KiB
C++
66 lines
1.5 KiB
C++
/*
|
|
* This file is part of the Owl Positioning System (OwlPS).
|
|
* OwlPS is a project of the University of Franche-Comté
|
|
* (Université de Franche-Comté), France.
|
|
*/
|
|
|
|
|
|
#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 *** */
|
|
|
|
|
|
/**
|
|
* @return \em true if the socket were successfully opened.
|
|
* @return \em false in case of error.
|
|
*/
|
|
bool OutputUDPSocket::init_socket()
|
|
{
|
|
sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port,
|
|
&server_info, &client_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,
|
|
(struct sockaddr *) &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 ;
|
|
}
|