poll.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <iostream>
00013 #include <cstdlib>
00014 #include <ecl/config/ecl.hpp>
00015 #include <ecl/errors/handlers.hpp>
00016 #include "../../include/ecl/io/poll.hpp"
00017 #include "../../include/ecl/io/socketpair.hpp"
00018 
00019 /*****************************************************************************
00020 ** Macros
00021 *****************************************************************************/
00022 
00023 #define DATA1 "In Xanadu, did Kublai Khan . . ."
00024 #define DATA2 "A stately pleasure dome decree . . ."
00025 
00026 /*****************************************************************************
00027 ** Globals
00028 *****************************************************************************/
00029 
00030 ecl::socket_descriptor socket_pair[2];
00031 
00032 /*****************************************************************************
00033 ** Functions
00034 *****************************************************************************/
00035 
00036 int socket_error() {
00037 #ifdef ECL_IS_WIN32
00038         return WSAGetLastError();
00039 #else
00040         return errno;
00041 #endif
00042 }
00043 
00044 void sleep_one_sec() {
00045 #ifdef ECL_IS_WIN32
00046         Sleep(1000);
00047 #else
00048         sleep(1);
00049 #endif
00050 }
00051 
00052 /*****************************************************************************
00053 ** Main
00054 *****************************************************************************/
00055 
00056 int main(int argc, char **argv) {
00057 
00058         ecl::SocketError error = ecl::init_sockets();
00059         if ( error.flag() != ecl::NoError ) {
00060                 std::cout << error.what() << std::endl;
00061                 abort();
00062         }
00063 
00064         // non blocking mode - need it so the read's don't block below.
00065         error = ecl::socketpair(socket_pair,true);
00066 
00067         if ( error.flag() != ecl::NoError) {
00068                 std::cout << error.what() << std::endl;
00069                 std::cout << "SocketPair Error: " << socket_error() << std::endl;
00070                 abort();
00071         }
00072         ecl::socket_pollfd pfd[2];
00073         pfd[0].fd = socket_pair[0];
00074         pfd[0].events = POLLIN;
00075         pfd[0].revents = 0;
00076         pfd[1].fd = socket_pair[1];
00077         pfd[1].events = POLLIN;
00078         pfd[1].revents = 0;
00079 
00080         int poll_timeout = 500; // ms
00081         int result = 0;
00082         unsigned int count = 0;
00083         while ( count < 10 ) {
00084                 if((result = ecl::poll_sockets(pfd, 2, poll_timeout)) < 0) {
00085                         std::cout << socket_error();
00086                 } else if ( result > 0 ) {
00087                         char receiving_buffer[256];
00088                         int n;
00089                         for ( unsigned int i = 0; i < 2; ++i ) {
00090                                 if ( pfd[i].revents == POLLIN ) {
00091 #ifdef ECL_IS_WIN32
00092                                         while ( ( n = ::recv(socket_pair[i], reinterpret_cast<char*>(receiving_buffer), 256, 0) ) > 0 ) {
00093 #else
00094                                         while( (n = read(socket_pair[i], receiving_buffer, 256)) > 0) {
00095 #endif
00096                                                 std::cout << receiving_buffer << std::endl;
00097                                                 if ( i == 0 ) {
00098 #ifdef ECL_IS_WIN32
00099                                                         if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
00100 #else
00101                                                         if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
00102 #endif
00103                                                                 std::cerr << "Failed to write to the socket." << std::endl;
00104                                                         }
00105                                                 } else {
00106 #ifdef ECL_IS_WIN32
00107                                                         if( ::send(socket_pair[1], reinterpret_cast<const char*>(DATA2), sizeof(DATA2), 0) < 0 ) {
00108 #else
00109                                                         if (write(socket_pair[1], DATA2, sizeof(DATA2)) < 0) {
00110 #endif
00111                                                                 std::cerr << "Failed to write to the socket." << std::endl;
00112                                                         }
00113                                                 }
00114                                         };
00115                                         pfd[i].revents = 0;
00116                                 }
00117                         }
00118                         sleep_one_sec();
00119                 } else {
00120                         // it will pass through here once to kickstart things
00121 #ifdef ECL_IS_WIN32
00122                         if( ::send(socket_pair[0], reinterpret_cast<const char*>(DATA1), sizeof(DATA1), 0) < 0 ) {
00123 #else
00124                         if (write(socket_pair[0], DATA1, sizeof(DATA1)) < 0) {
00125 #endif
00126                                 std::cerr << "Failed to write to the socket." << std::endl;
00127                         }
00128                         sleep_one_sec();
00129                 }
00130                 ++count;
00131         }
00132 
00133 
00134         ecl::close_socket(socket_pair[0]);
00135         ecl::close_socket(socket_pair[1]);
00136 
00137         return 0;
00138 }
00139 
00140 


ecl_io
Author(s): Daniel Stonier (d.stonier@gmail.com)
autogenerated on Thu Jan 2 2014 11:11:37