examples/poll.cpp
Go to the documentation of this file.
1 
8 /*****************************************************************************
9 ** Includes
10 *****************************************************************************/
11 
12 #include <iostream>
13 #include <cstdlib>
14 #include <ecl/config/ecl.hpp>
15 #include <ecl/errors/handlers.hpp>
16 #include "../../include/ecl/io/poll.hpp"
17 #include "../../include/ecl/io/socketpair.hpp"
18 #ifndef ECL_IS_WIN32
19  #include <unistd.h>
20 #endif
21 
22 /*****************************************************************************
23 ** Macros
24 *****************************************************************************/
25 
26 #define DATA1 "In Xanadu, did Kublai Khan . . ."
27 #define DATA2 "A stately pleasure dome decree . . ."
28 
29 /*****************************************************************************
30 ** Globals
31 *****************************************************************************/
32 
34 
35 /*****************************************************************************
36 ** Functions
37 *****************************************************************************/
38 
39 int socket_error() {
40 #ifdef ECL_IS_WIN32
41  return WSAGetLastError();
42 #else
43  return errno;
44 #endif
45 }
46 
47 void sleep_one_sec() {
48 #ifdef ECL_IS_WIN32
49  Sleep(1000);
50 #else
51  sleep(1);
52 #endif
53 }
54 
55 /*****************************************************************************
56 ** Main
57 *****************************************************************************/
58 
59 int main(int argc, char **argv) {
60 
62  if ( error.flag() != ecl::NoError ) {
63  std::cout << error.what() << std::endl;
64  abort();
65  }
66 
67  // non blocking mode - need it so the read's don't block below.
68  error = ecl::socketpair(socket_pair,true);
69 
70  if ( error.flag() != ecl::NoError) {
71  std::cout << error.what() << std::endl;
72  std::cout << "SocketPair Error: " << socket_error() << std::endl;
73  abort();
74  }
75  ecl::socket_pollfd pfd[2];
76  pfd[0].fd = socket_pair[0];
77  pfd[0].events = POLLIN;
78  pfd[0].revents = 0;
79  pfd[1].fd = socket_pair[1];
80  pfd[1].events = POLLIN;
81  pfd[1].revents = 0;
82 
83  int poll_timeout = 500; // ms
84  int result = 0;
85  unsigned int count = 0;
86  while ( count < 10 ) {
87  if((result = ecl::poll_sockets(pfd, 2, poll_timeout)) < 0) {
88  std::cout << socket_error();
89  } else if ( result > 0 ) {
90  char receiving_buffer[256];
91  int n;
92  for ( unsigned int i = 0; i < 2; ++i ) {
93  if ( pfd[i].revents == POLLIN ) {
94 #ifdef ECL_IS_WIN32
95  while ( ( n = ::recv(socket_pair[i], reinterpret_cast<char*>(receiving_buffer), 256, 0) ) > 0 ) {
96 #else
97  while( (n = read(socket_pair[i], receiving_buffer, 256)) > 0) {
98 #endif
99  std::cout << receiving_buffer << std::endl;
100  if ( i == 0 ) {
101 #ifdef ECL_IS_WIN32
102  if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
103 #else
104  if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
105 #endif
106  std::cerr << "Failed to write to the socket." << std::endl;
107  }
108  } else {
109 #ifdef ECL_IS_WIN32
110  if( ::send(socket_pair[1], reinterpret_cast<const char*>(DATA2), sizeof(DATA2), 0) < 0 ) {
111 #else
112  if (write(socket_pair[1], DATA2, sizeof(DATA2)) < 0) {
113 #endif
114  std::cerr << "Failed to write to the socket." << std::endl;
115  }
116  }
117  };
118  pfd[i].revents = 0;
119  }
120  }
121  sleep_one_sec();
122  } else {
123  // it will pass through here once to kickstart things
124 #ifdef ECL_IS_WIN32
125  if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
126 #else
127  if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
128 #endif
129  std::cerr << "Failed to write to the socket." << std::endl;
130  }
131  sleep_one_sec();
132  }
133  ++count;
134  }
135 
136 
139 
140  return 0;
141 }
142 
143 
ecl::socket_descriptor socket_pair[2]
virtual ErrorFlag flag() const
int main(int argc, char **argv)
virtual const char * what() const
#define DATA2
ecl_io_PUBLIC SocketError socketpair(socket_descriptor socket_fd_pair[2], const bool non_blocking=false)
Creates a socket pair internal to the current process.
#define DATA1
int socket_descriptor
Cross-platform typedef for a socket file descriptor.
Definition: sockets.hpp:58
ecl_io_PUBLIC SocketError init_sockets()
Initialise the socket subsystem.
Definition: lib/sockets.cpp:27
struct pollfd socket_pollfd
Definition: poll.hpp:87
ecl_io_PUBLIC int poll_sockets(socket_pollfd *fds, nfds_t nfds, int timeout)
Definition: lib/poll.cpp:25
int socket_error()
ecl_io_PUBLIC SocketError close_socket(const socket_descriptor &sock)
Close a socket.
Definition: lib/sockets.cpp:49
Extends the generic error handler with socket specific error strings.
Definition: sockets.hpp:68
void sleep_one_sec()


ecl_io
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:09:05