Webdar 1.0.0
Web user interface to libdar
proto_connexion.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef PROTO_CONNEXION_HPP
25#define PROTO_CONNEXION_HPP
26
27#include "my_config.h"
28
29 // C++ system header files
30#include <string>
31
32 // webdar headers
33#include "exceptions.hpp"
34
36
39
41{
42public:
43 enum status
44 {
45 connected, //< both read and write are allowed
46 not_connected //< session is closed both directions
47 };
48
50 proto_connexion(const std::string & peerip, unsigned int peerport);
51
53 proto_connexion(const proto_connexion & ref) = delete;
54 proto_connexion(proto_connexion && ref) noexcept = delete;
55 proto_connexion & operator = (const proto_connexion & ref) = delete;
56 proto_connexion & operator = (proto_connexion && ref) noexcept = delete;
57
59 virtual ~proto_connexion();
60
61 status get_status() const { return etat; };
62 const std::string & get_ip() const { return ip; };
63 unsigned int get_port() const { return port; };
64
66 char read_one(bool blocking);
67
70 char read_test_first(bool blocking);
71
74 char read_test_second(bool blocking);
75
77
80 void write(const char *a, unsigned int size);
81
83 void flush_write();
84
85
86protected:
87
89 virtual void write_impl(const char *a, unsigned int size) = 0;
90
92 virtual unsigned int read_impl(char *a, unsigned int size, bool blocking) = 0;
93
95 void set_status(status st) { etat = st; };
96
97private:
98 status etat; //< proto_connexion status
99 std::string ip; //< IP of the peer host
100 unsigned int port; //< port of the peer port
101
102 // buffer management for reading
103 unsigned buffer_size; //< size of the buffer
104 char *buffer; //< temporary area used for parsing, reading data FROM network
105 unsigned int already_read; //< amount of data already read
106 unsigned int data_size; //< total of data in buffer, already read or not
107
108 // output buffer
109 unsigned out_buf_size; //< allocated space for the output buffer (out_buf)
110 char *out_buf; //< temporary areas used to gather bytes for writing
111 unsigned int last_unwrote; //< amount of byte pending for writing
112
114 void fill_buffer(bool blocking);
115};
116
117#endif
buffers data from a TCP connexion, this is a pure virtual class
Definition: proto_connexion.hpp:41
char read_test_second(bool blocking)
Definition: proto_connexion.cpp:116
virtual unsigned int read_impl(char *a, unsigned int size, bool blocking)=0
implementation of the low level (without buffering) reading operation
void flush_write()
flush pending writings if any
Definition: proto_connexion.cpp:144
void set_status(status st)
let inherited class modifying the object status
Definition: proto_connexion.hpp:95
proto_connexion(const proto_connexion &ref)=delete
forbidding copy constuctor and assignment operator
virtual void write_impl(const char *a, unsigned int size)=0
implementation of the low level (without buffering) writing operation
void write(const char *a, unsigned int size)
write data
Definition: proto_connexion.cpp:127
char read_one(bool blocking)
extracts one byte form the buffer / exception thrown if not available
Definition: proto_connexion.cpp:96
char read_test_first(bool blocking)
Definition: proto_connexion.cpp:106
virtual ~proto_connexion()
destructor
Definition: proto_connexion.cpp:88
proto_connexion(const std::string &peerip, unsigned int peerport)
constructor
Definition: proto_connexion.cpp:55