owlps/owlps-positioning/src/outputudpsocket.cc

58 lines
1.2 KiB
C++
Raw Normal View History

#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 *** */
/**
* @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
{
ssize_t nsent = sendto(sockfd, data.c_str(), data.size(), 0,
(struct sockaddr *) &server_info,
sizeof(server_info)) ;
if (nsent != static_cast<ssize_t>(data.size()))
{
perror("Error sending result data through the UDP socket") ;
return false ;
}
return true ;
}