2011-04-04 17:37:19 +02:00
|
|
|
#include "outputudpsocket.hh"
|
2011-03-15 15:27:42 +01:00
|
|
|
#include "request.hh"
|
2011-04-05 12:05:46 +02:00
|
|
|
#include "resultlist.hh"
|
2011-07-30 15:58:48 +02:00
|
|
|
#include "result.hh"
|
2011-04-05 15:06:46 +02:00
|
|
|
#include "posexcept.hh"
|
2011-04-04 16:45:00 +02:00
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
#include <owlps.h>
|
2011-03-15 15:27:42 +01:00
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
#include <cstdio> // For perror()
|
2011-04-05 12:05:46 +02:00
|
|
|
|
2011-03-15 15:27:42 +01:00
|
|
|
using namespace std ;
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
/* *** Constructors *** */
|
|
|
|
|
|
|
|
|
2011-04-05 11:22:28 +02:00
|
|
|
OutputUDPSocket::OutputUDPSocket(const string &_remote_host,
|
|
|
|
const uint_fast16_t _remote_port):
|
2011-07-30 15:58:48 +02:00
|
|
|
OutputNetworkSocket(_remote_host, _remote_port)
|
2011-03-15 15:27:42 +01:00
|
|
|
{
|
2011-04-05 15:06:46 +02:00
|
|
|
if (! init_socket())
|
|
|
|
throw error_opening_output_file("UDP socket") ;
|
2011-03-15 15:27:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 16:45:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* *** Operations *** */
|
|
|
|
|
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
/**
|
2011-06-14 16:44:17 +02:00
|
|
|
* @return \em true if the socket were successfully opened.
|
|
|
|
* @return \em false in case of error.
|
2011-04-05 15:06:46 +02:00
|
|
|
*/
|
|
|
|
bool OutputUDPSocket::init_socket()
|
2011-04-05 12:05:46 +02:00
|
|
|
{
|
2011-04-05 15:06:46 +02:00
|
|
|
sockfd = owl_create_udp_trx_socket(remote_host.c_str(), remote_port,
|
|
|
|
&server_info, &client_info) ;
|
|
|
|
return sockfd >= 0 ;
|
2011-04-05 12:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-05 15:06:46 +02:00
|
|
|
/**
|
|
|
|
* Sends the text buffer 'data'.
|
|
|
|
*/
|
2011-07-30 15:58:48 +02:00
|
|
|
bool OutputUDPSocket::send_data(const string &data) const
|
2011-04-01 16:24:25 +02:00
|
|
|
{
|
2011-04-05 15:06:46 +02:00
|
|
|
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()))
|
2011-07-30 15:58:48 +02:00
|
|
|
{
|
|
|
|
perror("Error sending result data through the UDP socket") ;
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
return true ;
|
2011-04-01 16:24:25 +02:00
|
|
|
}
|