Anope IRC Services  Version 2.0
pipeengine.cpp
Go to the documentation of this file.
1 /*
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "services.h"
13 #include "sockets.h"
14 #include "socketengine.h"
15 
16 #ifndef _WIN32
17 #include <fcntl.h>
18 #endif
19 
20 Pipe::Pipe() : Socket(-1), write_pipe(-1)
21 {
22  int fds[2];
23  if (pipe(fds))
24  throw CoreException("Could not create pipe: " + Anope::LastError());
25  int sflags = fcntl(fds[0], F_GETFL, 0);
26  fcntl(fds[0], F_SETFL, sflags | O_NONBLOCK);
27  sflags = fcntl(fds[1], F_GETFL, 0);
28  fcntl(fds[1], F_SETFL, sflags | O_NONBLOCK);
29 
30  SocketEngine::Change(this, false, SF_READABLE);
31  SocketEngine::Change(this, false, SF_WRITABLE);
32  anope_close(this->sock);
33  this->io->Destroy();
34  SocketEngine::Sockets.erase(this->sock);
35 
36  this->sock = fds[0];
37  this->write_pipe = fds[1];
38 
39  SocketEngine::Sockets[this->sock] = this;
40  SocketEngine::Change(this, true, SF_READABLE);
41 }
42 
44 {
45  if (this->write_pipe >= 0)
46  anope_close(this->write_pipe);
47 }
48 
50 {
51  this->OnNotify();
52 
53  char dummy[512];
54  while (read(this->GetFD(), dummy, 512) == 512);
55  return true;
56 }
57 
58 void Pipe::Write(const char *data, size_t sz)
59 {
60  write(this->write_pipe, data, sz);
61 }
62 
63 int Pipe::Read(char *data, size_t sz)
64 {
65  return read(this->GetFD(), data, sz);
66 }
67 
68 bool Pipe::SetWriteBlocking(bool state)
69 {
70  int f = fcntl(this->write_pipe, F_GETFL, 0);
71  if (state)
72  return !fcntl(this->write_pipe, F_SETFL, f & ~O_NONBLOCK);
73  else
74  return !fcntl(this->write_pipe, F_SETFL, f | O_NONBLOCK);
75 }
76 
78 {
79  this->Write("\0", 1);
80 }
81 
#define F_GETFL
Definition: socket.h:14
SocketIO * io
Definition: sockets.h:209
bool SetWriteBlocking(bool state)
Definition: pipeengine.cpp:68
CoreExport const string LastError()
Definition: misc.cpp:606
~Pipe()
Definition: pipeengine.cpp:43
int fcntl(int fd, int cmd, int arg)
Definition: socket.cpp:131
static std::map< int, Socket * > Sockets
Definition: socketengine.h:24
#define anope_close
Definition: services.h:64
static void Change(Socket *s, bool set, SocketFlag flag)
Pipe()
Definition: pipeengine.cpp:20
int sock
Definition: sockets.h:198
int GetFD() const
Definition: sockets.cpp:474
#define write
Definition: socket.h:9
void Notify()
Definition: pipeengine.cpp:77
#define read
Definition: socket.h:8
#define F_SETFL
Definition: socket.h:15
int pipe(int fds[2])
Definition: pipe.cpp:11
int write_pipe
Definition: sockets.h:460
virtual void Destroy()
Definition: sockets.h:191
bool ProcessRead() anope_override
Definition: pipeengine.cpp:49
int Read(char *data, size_t sz)
Definition: pipeengine.cpp:63
#define O_NONBLOCK
Definition: socket.h:17
virtual void OnNotify()=0
void Write(const char *data, size_t sz)
Definition: pipeengine.cpp:58