ardupilot/libraries/AP_HAL_Linux/TCPServerDevice.cpp
Gustavo Jose de Sousa 7ac51f60d0 AP_HAL_Linux: standardize inclusion of libaries headers
This commit changes the way libraries headers are included in source files:

 - If the header is in the same directory the source belongs to, so the
 notation '#include ""' is used with the path relative to the directory
 containing the source.

 - If the header is outside the directory containing the source, then we use
 the notation '#include <>' with the path relative to libraries folder.

Some of the advantages of such approach:

 - Only one search path for libraries headers.

 - OSs like Windows may have a better lookup time.
2015-08-19 20:42:40 +09:00

125 lines
2.5 KiB
C++

#include <AP_HAL/AP_HAL.h>
#if CONFIG_HAL_BOARD == HAL_BOARD_LINUX
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include "TCPServerDevice.h"
extern const AP_HAL::HAL& hal;
TCPServerDevice::TCPServerDevice(const char *ip, uint16_t port, bool wait):
_ip(ip),
_port(port),
_wait(wait)
{
}
TCPServerDevice::~TCPServerDevice()
{
if (sock != NULL) {
delete sock;
sock = NULL;
}
}
ssize_t TCPServerDevice::write(const uint8_t *buf, uint16_t n)
{
if (sock == NULL) {
return -1;
}
return sock->send(buf, n);
}
/*
when we try to read we accept new connections if one isn't already
established
*/
ssize_t TCPServerDevice::read(uint8_t *buf, uint16_t n)
{
if (sock == NULL) {
sock = listener.accept(0);
if (sock != NULL) {
sock->set_blocking(_blocking);
}
}
if (sock == NULL) {
return -1;
}
ssize_t ret = sock->recv(buf, n, 1);
if (ret == 0) {
// EOF, go back to waiting for a new connection
delete sock;
sock = NULL;
return -1;
}
return ret;
}
bool TCPServerDevice::open()
{
listener.reuseaddress();
if (!listener.bind(_ip, _port)) {
if (hal.scheduler->millis() - _last_bind_warning > 5000) {
::printf("bind failed on %s port %u - %s\n",
_ip,
_port,
strerror(errno));
_last_bind_warning = hal.scheduler->millis();
}
return false;
}
if (!listener.listen(1)) {
if (hal.scheduler->millis() - _last_bind_warning > 5000) {
::printf("listen failed on %s port %u - %s\n",
_ip,
_port,
strerror(errno));
_last_bind_warning = hal.scheduler->millis();
}
return false;
}
listener.set_blocking(false);
if (_wait) {
::printf("Waiting for connection on %s:%u ....\n",
_ip, (unsigned)_port);
::fflush(stdout);
while (sock == NULL) {
sock = listener.accept(1000);
}
sock->set_blocking(_blocking);
::printf("connected\n");
::fflush(stdout);
}
return true;
}
bool TCPServerDevice::close()
{
if (sock != NULL) {
delete sock;
sock = NULL;
}
return true;
}
void TCPServerDevice::set_blocking(bool blocking)
{
_blocking = blocking;
listener.set_blocking(_blocking);
}
void TCPServerDevice::set_speed(uint32_t speed)
{
}
#endif