-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b557af
commit 7f00603
Showing
20 changed files
with
1,240 additions
and
4 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
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,25 @@ | ||
// excerpts from http://code.google.com/p/muduo/ | ||
// | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the License file. | ||
// | ||
// Author: Shuo Chen (chenshuo at chenshuo dot com) | ||
|
||
#ifndef MUDUO_NET_CALLBACKS_H | ||
#define MUDUO_NET_CALLBACKS_H | ||
|
||
#include <boost/function.hpp> | ||
#include <boost/shared_ptr.hpp> | ||
|
||
#include "datetime/Timestamp.h" | ||
|
||
namespace muduo | ||
{ | ||
|
||
// All client visible callbacks go here. | ||
|
||
typedef boost::function<void()> TimerCallback; | ||
|
||
} | ||
|
||
#endif // MUDUO_NET_CALLBACKS_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,58 @@ | ||
// excerpts from http://code.google.com/p/muduo/ | ||
// | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the License file. | ||
// | ||
// Author: Shuo Chen (chenshuo at chenshuo dot com) | ||
|
||
#include "Channel.h" | ||
#include "EventLoop.h" | ||
#include "logging/Logging.h" | ||
|
||
#include <sstream> | ||
|
||
#include <poll.h> | ||
|
||
using namespace muduo; | ||
|
||
const int Channel::kNoneEvent = 0; | ||
const int Channel::kReadEvent = POLLIN | POLLPRI; | ||
const int Channel::kWriteEvent = POLLOUT; | ||
|
||
Channel::Channel(EventLoop *loop, int fdArg) | ||
: loop_(loop), | ||
fd_(fdArg), | ||
events_(0), | ||
revents_(0), | ||
index_(-1) | ||
{ | ||
} | ||
|
||
void Channel::update() | ||
{ | ||
loop_->updateChannel(this); | ||
} | ||
|
||
void Channel::handleEvent() | ||
{ | ||
if (revents_ & POLLNVAL) | ||
{ | ||
LOG_WARN << "Channel::handle_event() POLLNVAL"; | ||
} | ||
|
||
if (revents_ & (POLLERR | POLLNVAL)) | ||
{ | ||
if (errorCallback_) | ||
errorCallback_(); | ||
} | ||
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) | ||
{ | ||
if (readCallback_) | ||
readCallback_(); | ||
} | ||
if (revents_ & POLLOUT) | ||
{ | ||
if (writeCallback_) | ||
writeCallback_(); | ||
} | ||
} |
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,85 @@ | ||
// excerpts from http://code.google.com/p/muduo/ | ||
// | ||
// Use of this source code is governed by a BSD-style license | ||
// that can be found in the License file. | ||
// | ||
// Author: Shuo Chen (chenshuo at chenshuo dot com) | ||
|
||
#ifndef MUDUO_NET_CHANNEL_H | ||
#define MUDUO_NET_CHANNEL_H | ||
|
||
#include <boost/function.hpp> | ||
#include <boost/noncopyable.hpp> | ||
|
||
namespace muduo | ||
{ | ||
|
||
class EventLoop; | ||
|
||
/// | ||
/// A selectable I/O channel. | ||
/// | ||
/// This class doesn't own the file descriptor. | ||
/// The file descriptor could be a socket, | ||
/// an eventfd, a timerfd, or a signalfd | ||
class Channel : boost::noncopyable | ||
{ | ||
public: | ||
typedef boost::function<void()> EventCallback; | ||
|
||
Channel(EventLoop *loop, int fd); | ||
|
||
void handleEvent(); | ||
void setReadCallback(const EventCallback &cb) | ||
{ | ||
readCallback_ = cb; | ||
} | ||
void setWriteCallback(const EventCallback &cb) | ||
{ | ||
writeCallback_ = cb; | ||
} | ||
void setErrorCallback(const EventCallback &cb) | ||
{ | ||
errorCallback_ = cb; | ||
} | ||
|
||
int fd() const { return fd_; } | ||
int events() const { return events_; } | ||
void set_revents(int revt) { revents_ = revt; } | ||
bool isNoneEvent() const { return events_ == kNoneEvent; } | ||
|
||
void enableReading() | ||
{ | ||
events_ |= kReadEvent; | ||
update(); | ||
} | ||
// void enableWriting() { events_ |= kWriteEvent; update(); } | ||
// void disableWriting() { events_ &= ~kWriteEvent; update(); } | ||
// void disableAll() { events_ = kNoneEvent; update(); } | ||
|
||
// for Poller | ||
int index() { return index_; } | ||
void set_index(int idx) { index_ = idx; } | ||
|
||
EventLoop *ownerLoop() { return loop_; } | ||
|
||
private: | ||
void update(); | ||
|
||
static const int kNoneEvent; | ||
static const int kReadEvent; | ||
static const int kWriteEvent; | ||
|
||
EventLoop *loop_; | ||
const int fd_; | ||
int events_; | ||
int revents_; | ||
int index_; // used by Poller. | ||
|
||
EventCallback readCallback_; | ||
EventCallback writeCallback_; | ||
EventCallback errorCallback_; | ||
}; | ||
|
||
} // namespace muduo | ||
#endif // MUDUO_NET_CHANNEL_H |
Oops, something went wrong.