orca-sim
UdpAsyncServer.hpp
Go to the documentation of this file.
1 /******************************************************************************
2  * This file is part of project ORCA. More information on the project
3  * can be found at the following repositories at GitHub's website.
4  *
5  * http://https://github.com/andersondomingues/orca-sim
6  * http://https://github.com/andersondomingues/orca-software
7  * http://https://github.com/andersondomingues/orca-mpsoc
8  * http://https://github.com/andersondomingues/orca-tools
9  *
10  * Copyright (C) 2018-2020 Anderson Domingues, <ti.andersondomingues@gmail.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along
23  * with this program; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 ******************************************************************************/
26 #ifndef ORCASIM_GDBRSP_INCLUDE_UDPASYNCSERVER_HPP_
27 #define ORCASIM_GDBRSP_INCLUDE_UDPASYNCSERVER_HPP_
28 
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <sys/select.h> // use select() for multiplexing
39 #include <sys/fcntl.h> // for non-blocking
40 
41 #define MAX_LENGTH 1024
42 #define SERVER_PORT 5000
43 
44 namespace orcasim::gdbrsp {
45 
46 enum class UdpAsyncError{
47  SOCKET_OPEN, // socket could not be opened, see fd limit or permission
48  SOCKET_BIND, // socket could not bind, see if port is in use
49  SELECT_ERR,
51 };
52 
59  private:
62  fd_set readfds;
63  fd_set writefds;
64 
65  struct timeval tv;
66  int numfd;
67  int socket_fd, bytes_read;
68  unsigned int address_length;
69  char* recieve_data[MAX_LENGTH], send_data[MAX_LENGTH];
70  struct sockaddr_in server_address , client_address;
71 
72  public:
77  explicit UdpAsyncServer(int port);
78 
79  ~UdpAsyncServer();
80 
87  int Send(char* data, int length);
88 
94  int Receive(char* data);
95 
96  void Error(UdpAsyncError err);
97 };
98 
99 } // namespace orcasim::gdbrsp
100 #endif // ORCASIM_GDBRSP_INCLUDE_UDPASYNCSERVER_HPP_
This class implements an asynchonous udp server.
#define MAX_LENGTH