anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
socket_transport.cpp (3712B)
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 BufferedSocket::BufferedSocket() 17 { 18 } 19 20 BufferedSocket::~BufferedSocket() 21 { 22 } 23 24 bool BufferedSocket::ProcessRead() 25 { 26 char tbuffer[NET_BUFSIZE]; 27 28 this->recv_len = 0; 29 30 int len = this->io->Recv(this, tbuffer, sizeof(tbuffer) - 1); 31 if (len == 0) 32 return false; 33 if (len < 0) 34 return SocketEngine::IgnoreErrno(); 35 36 tbuffer[len] = 0; 37 this->read_buffer.append(tbuffer); 38 this->recv_len = len; 39 40 return true; 41 } 42 43 bool BufferedSocket::ProcessWrite() 44 { 45 int count = this->io->Send(this, this->write_buffer); 46 if (count == 0) 47 return false; 48 if (count < 0) 49 return SocketEngine::IgnoreErrno(); 50 51 this->write_buffer = this->write_buffer.substr(count); 52 if (this->write_buffer.empty()) 53 SocketEngine::Change(this, false, SF_WRITABLE); 54 55 return true; 56 } 57 58 const Anope::string BufferedSocket::GetLine() 59 { 60 size_t s = this->read_buffer.find('\n'); 61 if (s == Anope::string::npos) 62 return ""; 63 Anope::string str = this->read_buffer.substr(0, s + 1); 64 this->read_buffer.erase(0, s + 1); 65 this->read_buffer.ltrim("\r\n"); 66 return str.trim("\r\n"); 67 } 68 69 void BufferedSocket::Write(const char *buffer, size_t l) 70 { 71 this->write_buffer += buffer + Anope::string("\r\n"); 72 SocketEngine::Change(this, true, SF_WRITABLE); 73 } 74 75 void BufferedSocket::Write(const char *message, ...) 76 { 77 va_list vi; 78 char tbuffer[BUFSIZE]; 79 80 if (!message) 81 return; 82 83 va_start(vi, message); 84 int len = vsnprintf(tbuffer, sizeof(tbuffer), message, vi); 85 va_end(vi); 86 87 this->Write(tbuffer, std::min(len, static_cast<int>(sizeof(tbuffer)))); 88 } 89 90 void BufferedSocket::Write(const Anope::string &message) 91 { 92 this->Write(message.c_str(), message.length()); 93 } 94 95 int BufferedSocket::ReadBufferLen() const 96 { 97 return recv_len; 98 } 99 100 int BufferedSocket::WriteBufferLen() const 101 { 102 return this->write_buffer.length(); 103 } 104 105 106 BinarySocket::DataBlock::DataBlock(const char *b, size_t l) 107 { 108 this->orig = this->buf = new char[l]; 109 memcpy(this->buf, b, l); 110 this->len = l; 111 } 112 113 BinarySocket::DataBlock::~DataBlock() 114 { 115 delete [] this->orig; 116 } 117 118 BinarySocket::BinarySocket() 119 { 120 } 121 122 BinarySocket::~BinarySocket() 123 { 124 } 125 126 bool BinarySocket::ProcessRead() 127 { 128 char tbuffer[NET_BUFSIZE]; 129 130 int len = this->io->Recv(this, tbuffer, sizeof(tbuffer)); 131 if (len <= 0) 132 return false; 133 134 return this->Read(tbuffer, len); 135 } 136 137 bool BinarySocket::ProcessWrite() 138 { 139 if (this->write_buffer.empty()) 140 { 141 SocketEngine::Change(this, false, SF_WRITABLE); 142 return true; 143 } 144 145 DataBlock *d = this->write_buffer.front(); 146 147 int len = this->io->Send(this, d->buf, d->len); 148 if (len <= -1) 149 return false; 150 else if (static_cast<size_t>(len) == d->len) 151 { 152 delete d; 153 this->write_buffer.pop_front(); 154 } 155 else 156 { 157 d->buf += len; 158 d->len -= len; 159 } 160 161 if (this->write_buffer.empty()) 162 SocketEngine::Change(this, false, SF_WRITABLE); 163 164 return true; 165 } 166 167 void BinarySocket::Write(const char *buffer, size_t l) 168 { 169 if (l == 0) 170 return; 171 this->write_buffer.push_back(new DataBlock(buffer, l)); 172 SocketEngine::Change(this, true, SF_WRITABLE); 173 } 174 175 void BinarySocket::Write(const char *message, ...) 176 { 177 va_list vi; 178 char tbuffer[BUFSIZE]; 179 180 if (!message) 181 return; 182 183 va_start(vi, message); 184 int len = vsnprintf(tbuffer, sizeof(tbuffer), message, vi); 185 va_end(vi); 186 187 this->Write(tbuffer, std::min(len, static_cast<int>(sizeof(tbuffer)))); 188 } 189 190 void BinarySocket::Write(const Anope::string &message) 191 { 192 this->Write(message.c_str(), message.length()); 193 } 194 195 bool BinarySocket::Read(const char *buffer, size_t l) 196 { 197 return true; 198 }