- /*
-
* main.cc
-
*
-
* Created on: 2011-12-1
-
* Author: simondu
-
*/
-
-
#include "head.h"
-
using namespace std;
-
-
inline int min( int a, int b)
-
{
-
return a>b?b:a;
-
}
-
class Ping_Pong : public ACE_Event_Handler
-
{
-
public:
-
Ping_Pong (char *b)
-
: len (min(strlen (b) + 1, BUFSIZ))
-
{
-
strncpy (this->buf, b, BUFSIZ);
-
}
-
virtual int handle_input (ACE_HANDLE handle)
-
{
-
return read (handle, this->buf, BUFSIZ);
-
}
-
virtual int handle_output (ACE_HANDLE handle)
-
{
-
return write (handle, this->buf, this->len);
-
}
-
virtual int handle_signal (int signum)
-
{
-
this->finished = 1;
-
return 0;
-
}
-
virtual int handle_timeout(const ACE_Time_Value &time, const void* d)
-
{
-
this->finished = 1;
-
return 0;
-
}
-
-
bool done (void)
-
{
-
return this->finished == 1;
-
}
-
private:
-
sig_atomic_t finished;
-
char buf[BUFSIZ];
-
size_t len;
-
};
-
static int init_handles (ACE_HANDLE handles[])
-
{
-
if (pipe (handles) == -1)
-
printf("error\n");
-
//LM_ERROR ((LOG_ERROR, "%p\n%a", "pipe", 1));
-
-
// Enable message-oriented mode instead of
-
-
// bytestream mode.
-
int arg = RMSGN;
-
if (ioctl (handles[0], I_SRDOPT, arg) == -1 || ioctl (handles[1], I_SRDOPT, arg) == -1)
-
return -1;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
printf("Starting ...\n");
-
-
-
ACE_HANDLE handles[2];
-
ACE_Reactor reactor;
-
init_handles (handles);
-
pid_t pid = fork ();
-
Ping_Pong callback (argv[1]);
-
// Register I/O-based event handler
-
reactor.register_handler (
-
handles[pid == 0],
-
&callback,
-
ACE_Event_Handler::READ_MASK | ACE_Event_Handler::WRITE_MASK);
-
-
// Register signal-based event handler
-
reactor.register_handler(SIGINT, &callback);
-
// Register timer-based event handler
-
// reactor.schedule_timer(&callback, 0, 10);
-
/* Main event loop (run in each process) */
-
while (callback.done () == false)
-
reactor.handle_events ();
-
-
-
printf("Ending ...\n");
-
return 0;
-
}
基本上所有的ACE相关文章都是来自ACE技术论文集等电子书
阅读(1341) | 评论(0) | 转发(0) |