-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize some code and separate the threads
- Loading branch information
Showing
9 changed files
with
184 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "consolethread.h" | ||
#include <iostream> | ||
|
||
void consoleThread(ISctpSink& sink) | ||
{ | ||
for (;;) | ||
{ | ||
std::string input; | ||
std::getline(std::cin, input); | ||
if (!input.empty()) | ||
{ | ||
sink.send(input.c_str(), input.size()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef CONSOLETHREAD_H | ||
#define CONSOLETHREAD_H | ||
|
||
#include "isctpsink.h" | ||
|
||
void consoleThread(ISctpSink& sink); | ||
|
||
#endif // CONSOLETHREAD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef ISCTPSINK_H | ||
#define ISCTPSINK_H | ||
|
||
#include <memory> | ||
|
||
class ISctpSink | ||
{ | ||
public: | ||
virtual void send(const char* buf, size_t len) = 0; | ||
}; | ||
|
||
#endif // ISCTPSINK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "pingthread.h" | ||
#include <boost/thread.hpp> | ||
|
||
PingThread::PingThread(ISctpSink& sink, int bytes, int interval) | ||
: m_sink(sink), m_bytes(bytes), m_interval(interval) | ||
{ | ||
} | ||
|
||
void PingThread::start() | ||
{ | ||
m_thread = boost::thread(boost::bind(&PingThread::loop, this)); | ||
} | ||
|
||
void PingThread::loop() | ||
{ | ||
std::vector<char> buf(m_bytes); | ||
for (int i = 0; i < m_bytes; ++i) | ||
{ | ||
buf[i] = 'A' + (i % ('Z' - 'A')); | ||
} | ||
for (;;) | ||
{ | ||
boost::this_thread::sleep(boost::posix_time::milliseconds(m_interval)); | ||
m_sink.send(&buf[0], m_bytes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef PINGTHREAD_H | ||
#define PINGTHREAD_H | ||
|
||
#include "isctpsink.h" | ||
#include <boost/thread.hpp> | ||
|
||
class PingThread | ||
{ | ||
public: | ||
PingThread(ISctpSink& sink, int bytes, int interval); | ||
|
||
void start(); | ||
private: | ||
void loop(); | ||
|
||
ISctpSink& m_sink; | ||
int m_bytes; | ||
int m_interval; | ||
boost::thread m_thread; | ||
}; | ||
|
||
#endif // PINGTHREAD_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#ifndef SCTPCAT_H | ||
#define SCTPCAT_H | ||
#include <boost/program_options.hpp> | ||
#include <boost/shared_ptr.hpp> | ||
#include <boost/thread/mutex.hpp> | ||
#include <netdb.h> | ||
#include <netinet/sctp.h> | ||
#include <string> | ||
|
||
#include "isctpsink.h" | ||
|
||
class SctpCat : public ISctpSink | ||
{ | ||
public: | ||
typedef boost::program_options::variables_map varmap; | ||
|
||
SctpCat(const varmap& options); | ||
void setup(std::string host, const std::string& port); | ||
void listenSocket(); | ||
void connectSocket(const std::string &host, const std::string &port); | ||
void receiveLoop(); | ||
void send(const char* buf, size_t len); | ||
void setPathMaxRetrans(sctp_assoc_t assoc_id, int count); | ||
void setAssocMaxRetrans(sctp_assoc_t assoc_id, int count); | ||
void setRto(int rtoMin, int rtoMax, int rtoInitial); | ||
|
||
void registerAssociationCallback(boost::function<void(int, sctp_assoc_t)>); | ||
void registerPeerAddressCallback(boost::function<void(int, sctp_assoc_t, const sockaddr_storage&)>); | ||
private: | ||
void subscribeAllEvents(int fd); | ||
int setupSocket(int ai_family, sockaddr* local_addr, socklen_t local_addr_len); | ||
|
||
void receiveMessages(int fd); | ||
void processMessage(int fd, char* buf, int len, sockaddr* from, socklen_t fromlen, | ||
const sctp_sndrcvinfo& sinfo, int flags); | ||
int m_fd; | ||
sctp_assoc_t m_assoc_id; | ||
static const int s_maxPendingConnections = 10; | ||
bool m_printTicks; | ||
int m_aiFamily; | ||
bool m_listen; | ||
const varmap& m_options; | ||
boost::shared_ptr<addrinfo> m_ai; | ||
mutable boost::mutex m_mutex; | ||
typedef boost::mutex::scoped_lock ScopedLock; | ||
std::vector< boost::function<void(int, sctp_assoc_t)> > m_associationCallbacks; | ||
std::vector< boost::function<void(int, sctp_assoc_t, const sockaddr_storage&)> > m_peerAddresssCallbacks; | ||
}; | ||
|
||
|
||
#endif // SCTPCAT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters