server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Jarek Potiuk (low bandwidth trajectory follower)
3  *
4  * Copyright 2017, 2018 Simon Rasmussen (refactor)
5  *
6  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
22 #include <arpa/inet.h>
23 #include <netinet/tcp.h>
24 #include <unistd.h>
25 #include <cstring>
26 #include "ur_modern_driver/log.h"
27 
28 URServer::URServer(int port) : port_(port)
29 {
30 }
31 
33 {
35 }
36 
37 std::string URServer::getIP()
38 {
39  sockaddr_in name;
40  socklen_t len = sizeof(name);
41  int res = ::getsockname(getSocketFD(), (sockaddr*)&name, &len);
42 
43  if (res < 0)
44  {
45  LOG_ERROR("Could not get local IP");
46  return std::string();
47  }
48 
49  char buf[128];
50  inet_ntop(AF_INET, &name.sin_addr, buf, sizeof(buf));
51  return std::string(buf);
52 }
53 
54 bool URServer::open(int socket_fd, struct sockaddr* address, size_t address_len)
55 {
56  int flag = 1;
57  setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
58  return ::bind(socket_fd, address, address_len) == 0;
59 }
60 
62 {
63  std::string empty;
64  bool res = TCPSocket::setup(empty, port_);
65 
66  if (!res)
67  return false;
68 
69  if (::listen(getSocketFD(), 1) < 0)
70  return false;
71 
72  return true;
73 }
74 
76 {
78  return false;
79 
80  struct sockaddr addr;
81  socklen_t addr_len;
82  int client_fd = -1;
83 
84  int retry = 0;
85  while ((client_fd = ::accept(getSocketFD(), &addr, &addr_len)) == -1)
86  {
87  LOG_ERROR("Accepting socket connection failed. (errno: %d)", errno);
88  if (retry++ >= 5)
89  return false;
90  }
91 
92  TCPSocket::setOptions(client_fd);
93 
94  return client_.setSocketFD(client_fd);
95 }
96 
98 {
100  return;
101 
102  client_.close();
103 }
104 
105 bool URServer::write(const uint8_t* buf, size_t buf_len, size_t& written)
106 {
107  return client_.write(buf, buf_len, written);
108 }
109 
110 bool URServer::readLine(char* buffer, size_t buf_len)
111 {
112  char* current_pointer = buffer;
113  char ch;
114  size_t total_read;
115 
116  if (buf_len <= 0 || buffer == NULL)
117  {
118  return false;
119  }
120 
121  total_read = 0;
122  for (;;)
123  {
124  if (client_.read(&ch))
125  {
126  if (total_read < buf_len - 1) // just in case ...
127  {
128  total_read++;
129  *current_pointer++ = ch;
130  }
131  if (ch == '\n')
132  {
133  break;
134  }
135  }
136  else
137  {
138  if (total_read == 0)
139  {
140  return false;
141  }
142  else
143  {
144  break;
145  }
146  }
147  }
148 
149  *current_pointer = '\0';
150  return true;
151 }
#define NULL
bool accept()
Definition: server.cpp:75
SocketState getState()
Definition: tcp_socket.h:54
virtual bool open(int socket_fd, struct sockaddr *address, size_t address_len)
Definition: server.cpp:54
bool read(char *character)
Definition: tcp_socket.cpp:134
bool bind()
Definition: server.cpp:61
bool readLine(char *buffer, size_t buf_len)
Definition: server.cpp:110
std::string getIP()
Definition: server.cpp:37
int port_
Definition: server.h:34
bool write(const uint8_t *buf, size_t buf_len, size_t &written)
Definition: server.cpp:105
virtual void setOptions(int socket_fd)
Definition: tcp_socket.cpp:38
bool setSocketFD(int socket_fd)
Definition: tcp_socket.cpp:99
TCPSocket client_
Definition: server.h:35
void disconnectClient()
Definition: server.cpp:97
bool write(const uint8_t *buf, size_t buf_len, size_t &written)
Definition: tcp_socket.cpp:164
~URServer()
Definition: server.cpp:32
bool setup(std::string &host, int port)
Definition: tcp_socket.cpp:45
#define LOG_ERROR(format,...)
Definition: log.h:36
void close()
Definition: tcp_socket.cpp:108
URServer(int port)
Definition: server.cpp:28
int getSocketFD()
Definition: tcp_socket.h:59


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00