Browse Subversion Repository
Contents of /common/StandardInput_lin.cpp
Parent Directory
| Revision Log
Revision 380 -
( show annotations)
( download)
( as text)
Sun May 16 11:35:05 2010 UTC
(13 years, 10 months ago)
by satofumi
File MIME type: text/x-c++src
File size: 890 byte(s)
shellViewer が動作するように調整
| 1 |
/*! |
| 2 |
\file |
| 3 |
\brief 標準入力の受信 |
| 4 |
|
| 5 |
\author Satofumi KAMIMURA |
| 6 |
|
| 7 |
$Id$ |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "StandardInput.h" |
| 11 |
#include <sys/poll.h> |
| 12 |
#include <unistd.h> |
| 13 |
|
| 14 |
using namespace beego; |
| 15 |
|
| 16 |
|
| 17 |
/*! |
| 18 |
\brief StandardInput の内部クラス |
| 19 |
*/ |
| 20 |
struct StandardInput::pImpl { |
| 21 |
struct pollfd nfds; /*!< タイムアウト制御 */ |
| 22 |
|
| 23 |
pImpl(void) { |
| 24 |
nfds.fd = 0; |
| 25 |
nfds.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL; |
| 26 |
nfds.revents = 0; |
| 27 |
} |
| 28 |
}; |
| 29 |
|
| 30 |
|
| 31 |
StandardInput::StandardInput(void) : pimpl(new pImpl) { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
StandardInput::~StandardInput(void) { |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
int StandardInput::recv(char *buffer, size_t maxlen, int timeout) { |
| 40 |
size_t filled = 0; |
| 41 |
|
| 42 |
while (filled < maxlen) { |
| 43 |
if (poll(&pimpl->nfds, 1, timeout) == 0) { |
| 44 |
break; // timeout |
| 45 |
} |
| 46 |
|
| 47 |
int n = read(0, &buffer[filled], maxlen - filled); |
| 48 |
if (n < 0) { |
| 49 |
return filled; |
| 50 |
} |
| 51 |
filled += n; |
| 52 |
} |
| 53 |
return filled; |
| 54 |
} |
|