Loading ccsrc/Protocols/Pcap/pcap_layer.cc +53 −13 Changes for ccsrc/Protocols/Pcap/pcap_layer.cc: 53 added lines, 13 removed lines. Original line number Diff line number Diff line #if (OSTYPE == linux) //#if (OSTYPE == linux) #if defined (__CYGWIN__) #define _GNU_SOURCE #endif #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <chrono> #include "Port.hh" #include <Port.hh> #include "pcap_layer_factory.hh" #if defined (__CYGWIN__) typedef void* HANDLE; typedef unsigned int DWORD; extern "C" { DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds); HANDLE pcap_getevent(pcap_t *p); } #endif #include "loggers.hh" pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) : layer(p_type), PORT(p_type.c_str()), _params(), _device(NULL), _pcap_h(-1), _thread(NULL), _running(FALSE), _resume(), _sent_file(NULL), _time_key("pcap_layer::Handle_Fd_Event_Readable") { Loading Loading @@ -38,11 +50,18 @@ pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) : la if (pcap_setnonblock(_device, 1, error_buffer) != 0) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to set blocking mode: %s", error_buffer); } #if defined (__CYGWIN__) if (pipe2(_fd, O_NONBLOCK) == -1) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to create a pipe: %s", ::strerror(errno)); } _pcap_h = _fd[0]; #else // Retrieve the device file handler _pcap_h = pcap_get_selectable_fd(_device); if (_pcap_h == -1) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to get device handler"); } #endif } else { // Check file name it = _params.find(std::string("file")); Loading Loading @@ -156,14 +175,17 @@ pcap_layer::~pcap_layer() { } // End of dtor void* pcap_layer::run(void* p_this) { pcap_layer& p = *static_cast<pcap_layer *>(p_this); return p.thread(); } void* pcap_layer::thread() { loggers::get_instance().log(">>> pcap_layer::run"); // Pointer the main object pcap_layer& p = *static_cast<pcap_layer *>(p_this); // Wait a little bit before to start sending packet std::this_thread::sleep_for(std::chrono::milliseconds(500)); params::const_iterator it = p._params.find("frame_offset"); if ((it != p._params.cend()) && (it->second.compare("0") != 0)) { params::const_iterator it = _params.find("frame_offset"); if ((it != _params.cend()) && (it->second.compare("0") != 0)) { // TODO Try t use PCAP filter to start directly to the correct frame offset /*try { unsigned int offset = std::stoul(str_dec, &s); Loading @@ -179,11 +201,30 @@ void* pcap_layer::run(void* p_this) { catch (out_of_range& o) { }*/ } #if defined (__CYGWIN__) // check HANDLE emulation for pcap if(_fd[0] == _pcap_h) { HANDLE h = pcap_getevent(_device); _running = TRUE; while (_running) { // Loop while _running flag is up DWORD rc = WaitForSingleObject(h, 100); if(rc == 0){ // event raised if (_resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(_fd[1], "\n", 1); // Any character will do the job } else { // not ready yet std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } } return NULL; } #endif // Let's go p._running = TRUE; while (p._running) { // Loop while _running flag is up if (p._resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(p._fd[1], "\n", 1); // Any character will do the job _running = TRUE; while (_running) { // Loop while _running flag is up if (_resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(_fd[1], "\n", 1); // Any character will do the job } else { // not ready yet std::this_thread::sleep_for(std::chrono::milliseconds(100)); } Loading Loading @@ -251,14 +292,13 @@ void pcap_layer::Handle_Fd_Event_Readable(int fd) { read(_fd[0], &c, 1); if (result == -2) { // End of file, therminate worker thread _running = FALSE; } else { // Get next packet } //loggers::get_instance().log("pcap_layer::Handle_Fd_Event_Readable: pcap_next_ex failed: result=%d", result); _resume.unlock(); } } // else, nothing to do } pcap_layer_factory pcap_layer_factory::_f; #endif //#endif ccsrc/Protocols/Pcap/pcap_layer.hh +4 −2 Changes for ccsrc/Protocols/Pcap/pcap_layer.hh: 4 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ */ #pragma once #if (OSTYPE == linux) //#if (OSTYPE == linux) #include <thread> #include <mutex> Loading @@ -37,6 +37,8 @@ class pcap_layer : public layer, public PORT { int _fd[2]; //! \todo static void* run(void* p_this); public: void* thread(void); public: //! \publicsection /*! * \brief Specialised constructor Loading Loading @@ -70,4 +72,4 @@ public: //! \publicsection void Handle_Fd_Event_Readable(int fd); }; #endif //#endif ccsrc/Protocols/Pcap/pcap_layer_factory.hh +2 −2 Changes for ccsrc/Protocols/Pcap/pcap_layer_factory.hh: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ */ #pragma once #if (OSTYPE == linux) //#if (OSTYPE == linux) #include "layer_stack_builder.hh" Loading Loading @@ -45,5 +45,5 @@ public: //! \publicsection }; }; // End of class pcap_layer_factory #endif //#endif Loading
ccsrc/Protocols/Pcap/pcap_layer.cc +53 −13 Changes for ccsrc/Protocols/Pcap/pcap_layer.cc: 53 added lines, 13 removed lines. Original line number Diff line number Diff line #if (OSTYPE == linux) //#if (OSTYPE == linux) #if defined (__CYGWIN__) #define _GNU_SOURCE #endif #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <chrono> #include "Port.hh" #include <Port.hh> #include "pcap_layer_factory.hh" #if defined (__CYGWIN__) typedef void* HANDLE; typedef unsigned int DWORD; extern "C" { DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds); HANDLE pcap_getevent(pcap_t *p); } #endif #include "loggers.hh" pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) : layer(p_type), PORT(p_type.c_str()), _params(), _device(NULL), _pcap_h(-1), _thread(NULL), _running(FALSE), _resume(), _sent_file(NULL), _time_key("pcap_layer::Handle_Fd_Event_Readable") { Loading Loading @@ -38,11 +50,18 @@ pcap_layer::pcap_layer(const std::string& p_type, const std::string& param) : la if (pcap_setnonblock(_device, 1, error_buffer) != 0) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to set blocking mode: %s", error_buffer); } #if defined (__CYGWIN__) if (pipe2(_fd, O_NONBLOCK) == -1) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to create a pipe: %s", ::strerror(errno)); } _pcap_h = _fd[0]; #else // Retrieve the device file handler _pcap_h = pcap_get_selectable_fd(_device); if (_pcap_h == -1) { loggers::get_instance().error("pcap_layer::pcap_layer: Failed to get device handler"); } #endif } else { // Check file name it = _params.find(std::string("file")); Loading Loading @@ -156,14 +175,17 @@ pcap_layer::~pcap_layer() { } // End of dtor void* pcap_layer::run(void* p_this) { pcap_layer& p = *static_cast<pcap_layer *>(p_this); return p.thread(); } void* pcap_layer::thread() { loggers::get_instance().log(">>> pcap_layer::run"); // Pointer the main object pcap_layer& p = *static_cast<pcap_layer *>(p_this); // Wait a little bit before to start sending packet std::this_thread::sleep_for(std::chrono::milliseconds(500)); params::const_iterator it = p._params.find("frame_offset"); if ((it != p._params.cend()) && (it->second.compare("0") != 0)) { params::const_iterator it = _params.find("frame_offset"); if ((it != _params.cend()) && (it->second.compare("0") != 0)) { // TODO Try t use PCAP filter to start directly to the correct frame offset /*try { unsigned int offset = std::stoul(str_dec, &s); Loading @@ -179,11 +201,30 @@ void* pcap_layer::run(void* p_this) { catch (out_of_range& o) { }*/ } #if defined (__CYGWIN__) // check HANDLE emulation for pcap if(_fd[0] == _pcap_h) { HANDLE h = pcap_getevent(_device); _running = TRUE; while (_running) { // Loop while _running flag is up DWORD rc = WaitForSingleObject(h, 100); if(rc == 0){ // event raised if (_resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(_fd[1], "\n", 1); // Any character will do the job } else { // not ready yet std::this_thread::sleep_for(std::chrono::milliseconds(100)); } } } return NULL; } #endif // Let's go p._running = TRUE; while (p._running) { // Loop while _running flag is up if (p._resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(p._fd[1], "\n", 1); // Any character will do the job _running = TRUE; while (_running) { // Loop while _running flag is up if (_resume.try_lock() == TRUE) { // Previous packet was consumed, lock for the next one write(_fd[1], "\n", 1); // Any character will do the job } else { // not ready yet std::this_thread::sleep_for(std::chrono::milliseconds(100)); } Loading Loading @@ -251,14 +292,13 @@ void pcap_layer::Handle_Fd_Event_Readable(int fd) { read(_fd[0], &c, 1); if (result == -2) { // End of file, therminate worker thread _running = FALSE; } else { // Get next packet } //loggers::get_instance().log("pcap_layer::Handle_Fd_Event_Readable: pcap_next_ex failed: result=%d", result); _resume.unlock(); } } // else, nothing to do } pcap_layer_factory pcap_layer_factory::_f; #endif //#endif
ccsrc/Protocols/Pcap/pcap_layer.hh +4 −2 Changes for ccsrc/Protocols/Pcap/pcap_layer.hh: 4 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ */ #pragma once #if (OSTYPE == linux) //#if (OSTYPE == linux) #include <thread> #include <mutex> Loading @@ -37,6 +37,8 @@ class pcap_layer : public layer, public PORT { int _fd[2]; //! \todo static void* run(void* p_this); public: void* thread(void); public: //! \publicsection /*! * \brief Specialised constructor Loading Loading @@ -70,4 +72,4 @@ public: //! \publicsection void Handle_Fd_Event_Readable(int fd); }; #endif //#endif
ccsrc/Protocols/Pcap/pcap_layer_factory.hh +2 −2 Changes for ccsrc/Protocols/Pcap/pcap_layer_factory.hh: 2 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -10,7 +10,7 @@ */ #pragma once #if (OSTYPE == linux) //#if (OSTYPE == linux) #include "layer_stack_builder.hh" Loading Loading @@ -45,5 +45,5 @@ public: //! \publicsection }; }; // End of class pcap_layer_factory #endif //#endif