Anope IRC Services  Version 2.0
socket_transport.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 
13 #include "services.h"
14 #include "sockets.h"
15 #include "socketengine.h"
16 
18 {
19 }
20 
22 {
23 }
24 
26 {
27  char tbuffer[NET_BUFSIZE];
28 
29  this->recv_len = 0;
30 
31  int len = this->io->Recv(this, tbuffer, sizeof(tbuffer) - 1);
32  if (len == 0)
33  return false;
34  if (len < 0)
36 
37  tbuffer[len] = 0;
38  this->read_buffer.append(tbuffer);
39  this->recv_len = len;
40 
41  return true;
42 }
43 
45 {
46  int count = this->io->Send(this, this->write_buffer);
47  if (count == 0)
48  return false;
49  if (count < 0)
51 
52  this->write_buffer = this->write_buffer.substr(count);
53  if (this->write_buffer.empty())
54  SocketEngine::Change(this, false, SF_WRITABLE);
55 
56  return true;
57 }
58 
60 {
61  size_t s = this->read_buffer.find('\n');
62  if (s == Anope::string::npos)
63  return "";
64  Anope::string str = this->read_buffer.substr(0, s + 1);
65  this->read_buffer.erase(0, s + 1);
66  this->read_buffer.ltrim("\r\n");
67  return str.trim("\r\n");
68 }
69 
70 void BufferedSocket::Write(const char *buffer, size_t l)
71 {
72  this->write_buffer += buffer + Anope::string("\r\n");
73  SocketEngine::Change(this, true, SF_WRITABLE);
74 }
75 
76 void BufferedSocket::Write(const char *message, ...)
77 {
78  va_list vi;
79  char tbuffer[BUFSIZE];
80 
81  if (!message)
82  return;
83 
84  va_start(vi, message);
85  int len = vsnprintf(tbuffer, sizeof(tbuffer), message, vi);
86  va_end(vi);
87 
88  this->Write(tbuffer, std::min(len, static_cast<int>(sizeof(tbuffer))));
89 }
90 
92 {
93  this->Write(message.c_str(), message.length());
94 }
95 
97 {
98  return recv_len;
99 }
100 
102 {
103  return this->write_buffer.length();
104 }
105 
106 
107 BinarySocket::DataBlock::DataBlock(const char *b, size_t l)
108 {
109  this->orig = this->buf = new char[l];
110  memcpy(this->buf, b, l);
111  this->len = l;
112 }
113 
115 {
116  delete [] this->orig;
117 }
118 
120 {
121 }
122 
124 {
125 }
126 
128 {
129  char tbuffer[NET_BUFSIZE];
130 
131  int len = this->io->Recv(this, tbuffer, sizeof(tbuffer));
132  if (len <= 0)
133  return false;
134 
135  return this->Read(tbuffer, len);
136 }
137 
139 {
140  if (this->write_buffer.empty())
141  {
142  SocketEngine::Change(this, false, SF_WRITABLE);
143  return true;
144  }
145 
146  DataBlock *d = this->write_buffer.front();
147 
148  int len = this->io->Send(this, d->buf, d->len);
149  if (len <= -1)
150  return false;
151  else if (static_cast<size_t>(len) == d->len)
152  {
153  delete d;
154  this->write_buffer.pop_front();
155  }
156  else
157  {
158  d->buf += len;
159  d->len -= len;
160  }
161 
162  if (this->write_buffer.empty())
163  SocketEngine::Change(this, false, SF_WRITABLE);
164 
165  return true;
166 }
167 
168 void BinarySocket::Write(const char *buffer, size_t l)
169 {
170  if (l == 0)
171  return;
172  this->write_buffer.push_back(new DataBlock(buffer, l));
173  SocketEngine::Change(this, true, SF_WRITABLE);
174 }
175 
176 void BinarySocket::Write(const char *message, ...)
177 {
178  va_list vi;
179  char tbuffer[BUFSIZE];
180 
181  if (!message)
182  return;
183 
184  va_start(vi, message);
185  int len = vsnprintf(tbuffer, sizeof(tbuffer), message, vi);
186  va_end(vi);
187 
188  this->Write(tbuffer, std::min(len, static_cast<int>(sizeof(tbuffer))));
189 }
190 
191 void BinarySocket::Write(const Anope::string &message)
192 {
193  this->Write(message.c_str(), message.length());
194 }
195 
196 bool BinarySocket::Read(const char *buffer, size_t l)
197 {
198  return true;
199 }
200 
virtual ~BufferedSocket()
SocketIO * io
Definition: sockets.h:209
virtual void Write(const char *buffer, size_t l)
string & append(const string &s)
Definition: anope.h:144
const Anope::string GetLine()
virtual int Recv(Socket *s, char *buf, size_t sz)
Definition: sockets.cpp:350
#define BUFSIZE
Definition: services.h:18
Anope::string write_buffer
Definition: sockets.h:275
string & ltrim(const Anope::string &what=" \t\r\n")
Definition: anope.h:163
static void Change(Socket *s, bool set, SocketFlag flag)
#define NET_BUFSIZE
Definition: sockets.h:24
iterator erase(const iterator &i)
Definition: anope.h:155
string substr(size_type pos=0, size_type n=npos) const
Definition: anope.h:277
bool ProcessRead() anope_override
bool ProcessWrite() anope_override
size_type length() const
Definition: anope.h:131
bool ProcessRead() anope_override
static const size_type npos
Definition: anope.h:44
std::deque< DataBlock * > write_buffer
Definition: sockets.h:331
virtual void Write(const char *buffer, size_t l)
string & trim(const Anope::string &what=" \t\r\n")
Definition: anope.h:177
bool empty() const
Definition: anope.h:126
bool ProcessWrite() anope_override
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133
int WriteBufferLen() const
DataBlock(const char *b, size_t l)
const char * c_str() const
Definition: anope.h:117
virtual int Send(Socket *s, const char *buf, size_t sz)
Definition: sockets.cpp:358
virtual bool Read(const char *buffer, size_t l)
virtual ~BinarySocket()
size_type find(const string &_str, size_type pos=0) const
Definition: anope.h:192
Anope::string read_buffer
Definition: sockets.h:273
static bool IgnoreErrno()
Definition: sockets.cpp:566
int ReadBufferLen() const