| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache & Catalin Popescu |
|---|
| 31 |
// |
|---|
| 32 |
#ifndef __NET_BASE_SELECTABLE_H__ |
|---|
| 33 |
#define __NET_BASE_SELECTABLE_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <whisperlib/common/io/buffer/memory_stream.h> |
|---|
| 36 |
#include <whisperlib/net/base/selector.h> |
|---|
| 37 |
|
|---|
| 38 |
namespace net { |
|---|
| 39 |
|
|---|
| 40 |
class Selectable { |
|---|
| 41 |
public: |
|---|
| 42 |
Selectable() |
|---|
| 43 |
: selector_(NULL), |
|---|
| 44 |
desire_(Selector::kWantRead | Selector::kWantError) { |
|---|
| 45 |
} |
|---|
| 46 |
explicit Selectable(Selector* selector) |
|---|
| 47 |
: selector_(selector), |
|---|
| 48 |
desire_(Selector::kWantRead | Selector::kWantError) { |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
virtual ~Selectable() { |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
Selector* selector() const { |
|---|
| 55 |
return selector_; |
|---|
| 56 |
} |
|---|
| 57 |
void set_selector(Selector* selector) { |
|---|
| 58 |
CHECK(selector_ == NULL); |
|---|
| 59 |
selector_ = selector; |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
// In any of the following events the obj is safe to close itself. |
|---|
| 63 |
// The selector will notice the closure (by checking IsOpen()), |
|---|
| 64 |
// and will remove this obj from it's queue. |
|---|
| 65 |
|
|---|
| 66 |
// Signal that informs the selectable object that it should read from |
|---|
| 67 |
// its registered fd. |
|---|
| 68 |
// Return true if the events should be contiued to be processesd w/ respect |
|---|
| 69 |
// to this selectable object. |
|---|
| 70 |
virtual bool HandleReadEvent(int events) { |
|---|
| 71 |
return true; |
|---|
| 72 |
} |
|---|
| 73 |
|
|---|
| 74 |
// Signal that informs the selectable object that it can write data out |
|---|
| 75 |
// Return true if the events should be contiued to be processesd w/ respect |
|---|
| 76 |
// to this selectable object. |
|---|
| 77 |
virtual bool HandleWriteEvent(int events) { |
|---|
| 78 |
return true; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
// Signal an error(exception) occured on the fd. |
|---|
| 82 |
// Return true if the events should be contiued to be processesd w/ respect |
|---|
| 83 |
// to this selectable object. |
|---|
| 84 |
virtual bool HandleErrorEvent(int events) { |
|---|
| 85 |
return true; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
// Returns the file descriptor associated w/ this Selectable object (if any) |
|---|
| 89 |
virtual int GetFd() const = 0; |
|---|
| 90 |
|
|---|
| 91 |
virtual void Close() = 0; |
|---|
| 92 |
|
|---|
| 93 |
protected: |
|---|
| 94 |
// Read/Write basics |
|---|
| 95 |
int32 Write(const char* buf, int32 size); |
|---|
| 96 |
int32 Read(char* buf, int32 size); |
|---|
| 97 |
|
|---|
| 98 |
// Read/Write interface for MemoryStream-s for copy-less stuff |
|---|
| 99 |
int32 Write(io::MemoryStream* ms); |
|---|
| 100 |
int32 Read(io::MemoryStream* ms); |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
// the selector that controls this object |
|---|
| 104 |
Selector* selector_; |
|---|
| 105 |
|
|---|
| 106 |
private: |
|---|
| 107 |
// the desire for read or write **DO NOT TOUCH** updated by the selector only |
|---|
| 108 |
int32 desire_; |
|---|
| 109 |
|
|---|
| 110 |
int32 GetDesiredEvents() const { |
|---|
| 111 |
int32 events = 0; |
|---|
| 112 |
if ( desire_ & Selector::kWantRead ) |
|---|
| 113 |
events |= EPOLLIN | EPOLLRDHUP; |
|---|
| 114 |
if ( desire_ & Selector::kWantWrite ) |
|---|
| 115 |
events |= EPOLLOUT; |
|---|
| 116 |
if ( desire_ & Selector::kWantError ) |
|---|
| 117 |
events |= EPOLLERR | EPOLLHUP; |
|---|
| 118 |
return events; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
friend class Selector; |
|---|
| 122 |
}; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
#endif // __NET_BASE_SELECTABLE_H__ |
|---|