root/trunk/whisperlib/net/base/selectable_filereader.cc

Revision 7, 4.4 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
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 // Authors: Cosmin Tudorache & Catalin Popescu
31
32 #include "net/base/selectable_filereader.h"
33
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include "common/base/errno.h"
37
38 namespace net {
39
40 SelectableFilereader::SelectableFilereader(
41     Selector* selector,
42     common::ByteOrder byte_order, int32 block_size)
43     : Selectable(selector),
44       fd_(INVALID_FD_VALUE),
45       inbuf_(byte_order, block_size),
46       data_callback_(NULL),
47       close_callback_(NULL) {
48 }
49 SelectableFilereader::~SelectableFilereader() {
50   CHECK(close_callback_ == NULL);
51 }
52
53 void SelectableFilereader::EnableRead(bool enable) {
54   selector_->EnableReadCallback(this, enable);
55 }
56
57 bool SelectableFilereader::InitializeFd(int fd,
58                                         DataCallback* data_callback,
59                                         Closure* close_callback) {
60   CHECK(fd_ == INVALID_FD_VALUE);
61   CHECK(fd != INVALID_FD_VALUE);
62   CHECK(data_callback->is_permanent());
63
64   const int flags = fcntl(fd, F_GETFL, 0);
65   if ( flags < 0 ) {
66     LOG_ERROR << " Error in fnctl for " << fd << " : "
67               << GetLastSystemErrorDescription();
68     goto Error;
69   }
70   if ( fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ) {
71     LOG_ERROR << " Error in fnctl with noblock " << fd << " : "
72               << GetLastSystemErrorDescription()
73               << " Flags: " << flags;
74     goto Error;
75   }
76   CHECK(selector_ != NULL);
77   fd_ = fd;
78   data_callback_ = data_callback;
79   close_callback_ = close_callback;
80   if ( !selector_->Register(this) ) {
81     ::close(fd_);
82     fd_ = INVALID_FD_VALUE;
83     return false;
84   }
85   selector_->EnableReadCallback(this, true);
86   selector_->EnableWriteCallback(this, false);
87   return true;
88  Error:
89   ::close(fd);
90   fd_ = INVALID_FD_VALUE;
91   return false;
92 }
93
94 bool SelectableFilereader::HandleReadEvent(int events) {
95   // Some HUP - bad - end all -- but after we do what we need to do ..
96   if ( events & (EPOLLHUP | EPOLLRDHUP) ) {
97     LOG_ERROR << " File closed: " << GetFd();
98     selector_->EnableReadCallback(this, false);
99     Close();
100     return false;
101   }
102   // Read from connection in the inbuf_ :)
103   int32 cb = Selectable::Read(&inbuf_);
104   if ( cb < 0 ) {
105     LOG_ERROR << "Error in read for: " << GetFd();
106     selector_->EnableReadCallback(this, false);
107     Close();
108     return false;
109   }
110   if ( data_callback_ ) {
111     data_callback_->Run(&inbuf_);
112   }
113   return true;
114 }
115
116 bool SelectableFilereader::HandleWriteEvent(int events) {
117   LOG_FATAL << " SelectableFilereader should not have write enabled !!";
118   return true;
119 }
120 bool SelectableFilereader::HandleErrorEvent(int events) {
121   selector_->EnableReadCallback(this, false);
122   Close();
123   return false;
124 }
125
126 void SelectableFilereader::Close() {
127   selector_->Unregister(this);
128   if ( fd_ != INVALID_FD_VALUE ) {
129     ::close(fd_);
130     fd_ = INVALID_FD_VALUE;
131   }
132   if ( close_callback_ != NULL ) {
133     close_callback_->Run();
134     close_callback_ = NULL;
135   }
136 }
137 }
Note: See TracBrowser for help on using the browser.