anope

- supernets anope source code & configuration
git clone git://git.acid.vegas/anope.git
Log | Files | Refs | Archive | README

pipeengine.cpp (1656B)

      1 /*
      2  *
      3  * (C) 2003-2022 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 
     43 Pipe::~Pipe()
     44 {
     45 	if (this->write_pipe >= 0)
     46 		anope_close(this->write_pipe);
     47 }
     48 
     49 bool Pipe::ProcessRead()
     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 
     77 void Pipe::Notify()
     78 {
     79 	this->Write("\0", 1);
     80 }