orca-sim
Public Member Functions | Private Attributes | List of all members
orcasim::gdbrsp::UdpAsyncServer Class Reference

This class implements an asynchonous udp server. More...

#include <UdpAsyncServer.hpp>

Public Member Functions

 UdpAsyncServer (int port)
 Default ctor. More...
 
 ~UdpAsyncServer ()
 
int Send (char *data, int length)
 Send a reply to the GDB client. More...
 
int Receive (char *data)
 Receives data from the GDB client. More...
 
void Error (UdpAsyncError err)
 

Private Attributes

fd_set original_socket
 
fd_set original_stdin
 
fd_set readfds
 
fd_set writefds
 
struct timeval tv
 
int numfd
 
int socket_fd
 
int bytes_read
 
unsigned int address_length
 
char * recieve_data [MAX_LENGTH]
 
char send_data [MAX_LENGTH]
 
struct sockaddr_in server_address client_address
 

Detailed Description

This class implements an asynchonous udp server.

We use select() to interrupt the simulation only when there is some packet for the gdb server.

Definition at line 58 of file UdpAsyncServer.hpp.

Constructor & Destructor Documentation

§ UdpAsyncServer()

UdpAsyncServer::UdpAsyncServer ( int  port)
explicit

Default ctor.

Parameters
portUDP port number to run the server.

Definition at line 51 of file UdpAsyncServer.cpp.

51  {
52  if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
54  }
55 
56  int flags = fcntl(socket_fd, F_GETFL);
57  flags |= O_NONBLOCK;
58  fcntl(socket_fd, F_SETFL, flags);
59 
60  // file descriptor set to monitoring for reading
61  FD_ZERO(&original_socket);
62  FD_ZERO(&readfds);
63 
64  // file descriptor set to monitoring for writing
65  FD_ZERO(&writefds);
66 
67  // add the receiving socket to the receiving set
68  FD_SET(socket_fd, &original_socket);
69  FD_SET(socket_fd, &readfds);
70 
71  // add the empty set to the writing monitoring list
72  FD_SET(0, &writefds);
73 
74  // since we got s2 second, it's the "greater", so we use that for
75  // // the n param in select()
76  numfd = socket_fd + 1;
77 
78  // wait until either socket has data ready to be recv()d (timeout 10.5 secs)
79  tv.tv_sec = 1;
80  tv.tv_usec = 500000;
81 
82  server_address.sin_family = AF_INET;
83  server_address.sin_port = htons(port);
84  server_address.sin_addr.s_addr = INADDR_ANY;
85  bzero(&(server_address.sin_zero), 8);
86 
87  address_length = sizeof(struct sockaddr);
88 
89  if (bind(socket_fd, (struct sockaddr *)&server_address,
90  sizeof(struct sockaddr)) == -1) {
92  }
93 }
void Error(UdpAsyncError err)

§ ~UdpAsyncServer()

UdpAsyncServer::~UdpAsyncServer ( )

Definition at line 127 of file UdpAsyncServer.cpp.

127  {
128  close(socket_fd);
129 }

Member Function Documentation

§ Error()

void UdpAsyncServer::Error ( UdpAsyncError  err)

Definition at line 32 of file UdpAsyncServer.cpp.

32  {
33  std::string message = "UdpAsyncErro: ";
34 
35  switch (err) {
37  message = message +
38  "Could not create socket" +
39  "(tips: +exceeded_socket_limit +system_permission).";
40  break;
42  message = message +
43  "Could not bind socket (tips: +address_in_use).";
44  break;
45  default: break;
46  }
47 
48  std::cout << message << std::endl;
49 }

§ Receive()

int UdpAsyncServer::Receive ( char *  data)

Receives data from the GDB client.

Parameters
dataLocation to store data in.
Returns
Returns the number of received bytes or -1

Definition at line 101 of file UdpAsyncServer.cpp.

101  {
102  // copy the receiving set to the working variable
104 
105  // chech whether the receving set has data
106  int res = select(numfd, &readfds, &writefds, NULL, &tv);
107 
108  switch (res) {
109  case -1:
111  break;
112  case 0:
114  break;
115  default:
116  if (FD_ISSET(socket_fd, &readfds)) {
117  FD_CLR(socket_fd, &readfds);
118  bytes_read = recvfrom(socket_fd, data, MAX_LENGTH, 0,
119  (struct sockaddr *)&client_address, &address_length);
120  return bytes_read;
121  }
122  }
123 
124  return 0;
125 }
struct sockaddr_in server_address client_address
void Error(UdpAsyncError err)
#define MAX_LENGTH

§ Send()

int UdpAsyncServer::Send ( char *  data,
int  length 
)

Send a reply to the GDB client.

Parameters
dataData to be send
lengthThe number of bytes to send
Returns
Returns 0 when sucefful, otherwise error num.

Definition at line 96 of file UdpAsyncServer.cpp.

96  {
97  return sendto(socket_fd, data, length, 0,
98  (struct sockaddr*)&client_address, sizeof(struct sockaddr));
99 }
struct sockaddr_in server_address client_address

Member Data Documentation

§ address_length

unsigned int orcasim::gdbrsp::UdpAsyncServer::address_length
private

Definition at line 68 of file UdpAsyncServer.hpp.

§ bytes_read

int orcasim::gdbrsp::UdpAsyncServer::bytes_read
private

Definition at line 67 of file UdpAsyncServer.hpp.

§ client_address

struct sockaddr_in server_address orcasim::gdbrsp::UdpAsyncServer::client_address
private

Definition at line 70 of file UdpAsyncServer.hpp.

§ numfd

int orcasim::gdbrsp::UdpAsyncServer::numfd
private

Definition at line 66 of file UdpAsyncServer.hpp.

§ original_socket

fd_set orcasim::gdbrsp::UdpAsyncServer::original_socket
private

Definition at line 60 of file UdpAsyncServer.hpp.

§ original_stdin

fd_set orcasim::gdbrsp::UdpAsyncServer::original_stdin
private

Definition at line 61 of file UdpAsyncServer.hpp.

§ readfds

fd_set orcasim::gdbrsp::UdpAsyncServer::readfds
private

Definition at line 62 of file UdpAsyncServer.hpp.

§ recieve_data

char* orcasim::gdbrsp::UdpAsyncServer::recieve_data[MAX_LENGTH]
private

Definition at line 69 of file UdpAsyncServer.hpp.

§ send_data

char orcasim::gdbrsp::UdpAsyncServer::send_data[MAX_LENGTH]
private

Definition at line 69 of file UdpAsyncServer.hpp.

§ socket_fd

int orcasim::gdbrsp::UdpAsyncServer::socket_fd
private

Definition at line 67 of file UdpAsyncServer.hpp.

§ tv

struct timeval orcasim::gdbrsp::UdpAsyncServer::tv
private

Definition at line 65 of file UdpAsyncServer.hpp.

§ writefds

fd_set orcasim::gdbrsp::UdpAsyncServer::writefds
private

Definition at line 63 of file UdpAsyncServer.hpp.


The documentation for this class was generated from the following files: