libsocket  1.5
socket.hxx
Go to the documentation of this file.
1 /*
2 ** socket.hxx
3 ** Login : Julien Lemoine <speedblue@happycoders.org>
4 ** Started on Tue Jun 1 22:45:16 2004 Julien Lemoine
5 ** $Id: socket.hxx,v 1.2 2004/11/14 19:37:46 speedblue Exp $
6 **
7 ** Copyright (C) 2004 Julien Lemoine
8 ** This program is free software; you can redistribute it and/or modify
9 ** it under the terms of the GNU Lesser General Public License as published by
10 ** the Free Software Foundation; either version 2 of the License, or
11 ** (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 
23 #ifndef SOCKET_HXX_
24 # define SOCKET_HXX_
25 
26 namespace Network
27 {
28  inline bool Socket::_check_answer(int res, std::string &str)
29  {
30  if (res <= 0)
31  {
32  if (!_buffer.size()) // No more data...
33  throw ConnectionClosed("Connection Closed", HERE);
34  else
35  {
36  str += _buffer;
37  _buffer = "";
38  _state_timeout = 0;
39  return true;
40  }
41  }
42  return false;
43  }
44 
45  inline bool Socket::_update_buffer(std::pair<int, int> &delim,
46  int &i, std::string &str)
47  {
48  delim = _find_delim(_buffer, 0);
49  i = delim.first;
50  while (!_empty_lines && !i)
51  {
52  // remove delimiter in front of buffer
53  _buffer = _buffer.substr(delim.second, _buffer.size() - delim.second);
54  delim = _find_delim(_buffer, 0);
55  i = delim.first;
56  }
57  if ((i > 0 || _empty_lines) && ((unsigned int)i < _buffer.size()))
58  {
59  str = _buffer.substr(0, i);
60  _buffer = _buffer.substr(i + delim.second,
61  _buffer.size() - i - delim.second);
62  return true;
63  }
64  else
65  return false;
66  }
67 }
68 
69 #endif /* !SOCKET_HXX_ */
Network::Socket::_buffer
std::string _buffer
Definition: socket.hh:202
Network
Network namespace represent all networks connection.
Definition: localsocket.cc:32
Network::Socket::_empty_lines
bool _empty_lines
Definition: socket.hh:201
HERE
#define HERE
Definition: socketexception.hh:26
Network::Socket::_check_answer
bool _check_answer(int res, std::string &str)
return the content of the buffer is there is
Definition: socket.hxx:28
Network::Socket::_update_buffer
bool _update_buffer(std::pair< int, int > &delim, int &i, std::string &str)
look delimiter and remove delimiter at begining of buffer if needed
Definition: socket.hxx:45
Network::Socket::_state_timeout
unsigned _state_timeout
Definition: socket.hh:192
Network::Socket::_find_delim
std::pair< int, int > _find_delim(const std::string &str, int start) const
Definition: socket.cc:339