cgv
Loading...
Searching...
No Matches
socket.h
1#pragma once
2
3#include <string>
4#include <cgv/data/ref_ptr.h>
5
6#include "lib_begin.h"
7
8namespace cgv {
9 namespace os {
10
12class CGV_API socket : public data::ref_counted
13{
14protected:
15 static bool show_debug_output;
16 static int nr_of_sockets;
17 static bool begin();
18 static void end();
19 friend class socket_server;
20 friend class socket_select;
22 socket();
24 socket(size_t _id);
26 size_t user_data;
28 mutable std::string last_error;
30 bool set_last_error(const char* location, const std::string& text = "") const;
31public:
33 static void enable_debug_output(bool enable = true);
35 virtual ~socket();
37 std::string get_last_error() const;
39 bool is_data_pending() const;
41 int get_nr_of_arrived_bytes() const;
43 std::string receive_line();
45 std::string receive_data(unsigned int nr_of_bytes = 0);
47 bool send_line(const std::string& content);
49 bool send_data(const std::string&);
51 bool close();
52};
53
56
57class CGV_API socket_client;
58
61
63class CGV_API socket_client : public socket
64{
65protected:
66 friend CGV_API socket_client_ptr create_socket_client();
69public:
71 bool connect(const std::string& host, int port);
72};
73
75extern CGV_API socket_client_ptr create_socket_client();
76
77
78class CGV_API socket_server;
79
82
84class CGV_API socket_server : public socket
85{
86protected:
87 friend CGV_API socket_server_ptr create_socket_server();
90public:
92 bool bind_and_listen(int port, int max_nr_pending_connections);
94 socket_ptr wait_for_connection();
96 socket_ptr check_for_connection();
97};
98
100extern CGV_API socket_server_ptr create_socket_server();
101
102#if _MSC_VER >= 1400
103CGV_TEMPLATE template class CGV_API cgv::data::ref_ptr<cgv::os::socket_ptr>;
104CGV_TEMPLATE template class CGV_API cgv::data::ref_ptr<cgv::os::socket_client_ptr>;
105CGV_TEMPLATE template class CGV_API cgv::data::ref_ptr<cgv::os::socket_server_ptr>;
106#endif
107
108 }
109}
110
111#include <cgv/config/lib_end.h>
if you derive your class from this class, a ref_ptr will do reference counting in the inhereted ref_c...
Definition ref_counted.h:11
reference counted pointer, which can work together with types that are derived from ref_counted,...
Definition ref_ptr.h:160
client socket
Definition socket.h:64
socket server allows to be connected to
Definition socket.h:85
base class for all sockets
Definition socket.h:13
size_t user_data
store platform dependent reference to socket
Definition socket.h:26
std::string last_error
store the last error
Definition socket.h:28
the cgv namespace
Definition print.h:11