code style
This commit is contained in:
@@ -27,15 +27,12 @@
|
||||
/**
|
||||
* class to notify other thread per pipe.
|
||||
*/
|
||||
class Notify
|
||||
{
|
||||
|
||||
public:
|
||||
class Notify {
|
||||
public:
|
||||
/**
|
||||
* constructs a new instance and do notifying.
|
||||
*/
|
||||
Notify()
|
||||
{
|
||||
Notify() {
|
||||
int pipefd[2];
|
||||
int ret = pipe(pipefd);
|
||||
|
||||
@@ -62,15 +59,14 @@ public:
|
||||
* write notify event to file descriptor.
|
||||
* @return result of writing notification.
|
||||
*/
|
||||
int notify() const { return write(m_sendfd,"1",1); }
|
||||
int notify() const { return write(m_sendfd, "1", 1); }
|
||||
|
||||
private:
|
||||
private:
|
||||
/** file descriptor to watch */
|
||||
int m_recvfd;
|
||||
|
||||
/** file descriptor to notify */
|
||||
int m_sendfd;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_NOTIFY_H_
|
||||
|
||||
+12
-20
@@ -33,15 +33,12 @@ using namespace std;
|
||||
* @param T the item type.
|
||||
*/
|
||||
template <typename T>
|
||||
class Queue
|
||||
{
|
||||
|
||||
public:
|
||||
class Queue {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Queue()
|
||||
{
|
||||
Queue() {
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
}
|
||||
@@ -49,28 +46,26 @@ public:
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
~Queue()
|
||||
{
|
||||
~Queue() {
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/**
|
||||
* Hidden copy constructor.
|
||||
* @param src the object to copy from.
|
||||
*/
|
||||
Queue(const Queue& src);
|
||||
|
||||
public:
|
||||
|
||||
public:
|
||||
/**
|
||||
* Add an item to the end of queue.
|
||||
* @param item the item to add.
|
||||
*/
|
||||
void push(T item)
|
||||
{
|
||||
void push(T item) {
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_queue.push_back(item);
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
@@ -82,8 +77,7 @@ public:
|
||||
* @param timeout the maximum time in seconds to wait for the queue being filled, or 0 for no wait.
|
||||
* @return the item, or NULL if no item is available within the specified time.
|
||||
*/
|
||||
T pop(int timeout = 0)
|
||||
{
|
||||
T pop(int timeout = 0) {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (timeout > 0) {
|
||||
@@ -112,8 +106,7 @@ public:
|
||||
* @param wait true to wait for the item to appear in the queue.
|
||||
* @return whether the item was removed.
|
||||
*/
|
||||
bool remove(T item, bool wait = false)
|
||||
{
|
||||
bool remove(T item, bool wait = false) {
|
||||
bool ret = false;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
do {
|
||||
@@ -135,8 +128,7 @@ public:
|
||||
* Return the first item in the queue without removing it.
|
||||
* @return the item, or NULL if no item is available.
|
||||
*/
|
||||
T peek()
|
||||
{
|
||||
T peek() {
|
||||
T item;
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
if (m_queue.empty()) {
|
||||
@@ -148,7 +140,8 @@ public:
|
||||
return item;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/** the queue itself */
|
||||
list<T> m_queue;
|
||||
|
||||
@@ -157,7 +150,6 @@ private:
|
||||
|
||||
/** condition variable for exclusive lock */
|
||||
pthread_cond_t m_cond;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_QUEUE_H_
|
||||
|
||||
@@ -64,8 +64,7 @@ void RotateFile::write(unsigned char* value, unsigned int size, bool received) {
|
||||
fprintf(m_stream, "%04d-%02d-%02d %02d:%02d:%02d.%03ld %c",
|
||||
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
|
||||
tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec/1000000,
|
||||
received ? '<' : '>'
|
||||
);
|
||||
received ? '<' : '>');
|
||||
for (unsigned int pos = 0; pos < size; pos++) {
|
||||
fprintf(m_stream, "%2.2x ", value[pos]);
|
||||
}
|
||||
|
||||
@@ -33,9 +33,8 @@ using namespace std;
|
||||
/**
|
||||
* Helper class for writing to a rotating file with maximum size.
|
||||
*/
|
||||
class RotateFile
|
||||
{
|
||||
public:
|
||||
class RotateFile {
|
||||
public:
|
||||
/**
|
||||
* Construct a new instance.
|
||||
* @param fileName the name of the file write to.
|
||||
@@ -71,7 +70,8 @@ public:
|
||||
*/
|
||||
void write(unsigned char* value, unsigned int size, bool received = true);
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/** whether writing to the file is enabled. */
|
||||
bool m_enabled;
|
||||
|
||||
@@ -89,7 +89,6 @@ private:
|
||||
|
||||
/** the number of bytes already written to the @a m_file. */
|
||||
unsigned long m_fileSize;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_ROTATEFILE_H_
|
||||
|
||||
+10
-17
@@ -37,10 +37,8 @@ using namespace std;
|
||||
/**
|
||||
* class for low level tcp socket operations. (open, close, send, receive).
|
||||
*/
|
||||
class TCPSocket
|
||||
{
|
||||
|
||||
public:
|
||||
class TCPSocket {
|
||||
public:
|
||||
/** grant access for friend class TCPClient */
|
||||
friend class TCPClient;
|
||||
|
||||
@@ -92,7 +90,8 @@ public:
|
||||
*/
|
||||
bool isValid();
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/** file descriptor from tcp socket */
|
||||
int m_sfd;
|
||||
|
||||
@@ -108,16 +107,13 @@ private:
|
||||
* @param address struct which holds the ip address.
|
||||
*/
|
||||
TCPSocket(int sfd, struct sockaddr_in* address);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* class to initiate a tcp socket connection to a listening server.
|
||||
*/
|
||||
class TCPClient
|
||||
{
|
||||
|
||||
public:
|
||||
class TCPClient {
|
||||
public:
|
||||
/**
|
||||
* initiate a tcp socket connection to a listening server.
|
||||
* @param server the server name or ip address to connect.
|
||||
@@ -125,16 +121,13 @@ public:
|
||||
* @return pointer to an opened tcp socket.
|
||||
*/
|
||||
TCPSocket* connect(const string& server, const uint16_t& port);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* class for a tcp based network server.
|
||||
*/
|
||||
class TCPServer
|
||||
{
|
||||
|
||||
public:
|
||||
class TCPServer {
|
||||
public:
|
||||
/**
|
||||
* creates a new instance of a listening tcp server.
|
||||
* @param port the tcp port.
|
||||
@@ -166,7 +159,8 @@ public:
|
||||
*/
|
||||
int getFD() const { return m_lfd; }
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/** file descriptor from listening tcp socket */
|
||||
int m_lfd;
|
||||
|
||||
@@ -178,7 +172,6 @@ private:
|
||||
|
||||
/** true if object is already listening */
|
||||
bool m_listening;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_TCPSOCKET_H_
|
||||
|
||||
@@ -36,37 +36,28 @@ Thread::~Thread() {
|
||||
}
|
||||
|
||||
bool Thread::start(const char* name) {
|
||||
|
||||
int result = pthread_create(&m_threadid, NULL, runThread, this);
|
||||
|
||||
if (result == 0) {
|
||||
|
||||
#ifdef HAVE_PTHREAD_SETNAME_NP
|
||||
#ifndef __MACH__
|
||||
pthread_setname_np(m_threadid, name);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
m_started = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Thread::join() {
|
||||
int result = -1;
|
||||
|
||||
if (m_started) {
|
||||
m_stopped = true;
|
||||
result = pthread_join(m_threadid, NULL);
|
||||
|
||||
if (result == 0) {
|
||||
m_started = false;
|
||||
}
|
||||
}
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
|
||||
+8
-13
@@ -26,10 +26,8 @@
|
||||
/**
|
||||
* wrapper class for pthread.
|
||||
*/
|
||||
class Thread
|
||||
{
|
||||
|
||||
public:
|
||||
class Thread {
|
||||
public:
|
||||
/**
|
||||
* constructor.
|
||||
*/
|
||||
@@ -77,15 +75,15 @@ public:
|
||||
*/
|
||||
pthread_t self() { return m_threadid; }
|
||||
|
||||
protected:
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Thread entry method to be overridden by derived class.
|
||||
*/
|
||||
virtual void run() = 0;
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/**
|
||||
* Enter the Thread loop by calling run().
|
||||
*/
|
||||
@@ -102,17 +100,14 @@ private:
|
||||
|
||||
/** Whether the thread was stopped by @a stop() or @a join(). */
|
||||
bool m_stopped;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* A @a Thread that can be waited on.
|
||||
*/
|
||||
class WaitThread : public Thread
|
||||
{
|
||||
|
||||
public:
|
||||
class WaitThread : public Thread {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@@ -136,13 +131,13 @@ public:
|
||||
*/
|
||||
bool Wait(int seconds);
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
/** the mutex for waiting. */
|
||||
pthread_mutex_t m_mutex;
|
||||
|
||||
/** the condition for waiting. */
|
||||
pthread_cond_t m_cond;
|
||||
|
||||
};
|
||||
|
||||
#endif // LIB_UTILS_THREAD_H_
|
||||
|
||||
Reference in New Issue
Block a user