-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.h
72 lines (58 loc) · 2.07 KB
/
connection.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// ------------------------------------------------------------------
//
// Client/Server communication package
//
// Connection header file
//
// Change log
// 950323 RH Initial version
// 951212 RH Modified to allow subclassing of class Connection
// 970127 RH Changed "private" to "protected"
// 990125 PH Changed function names: Read -> read, etc.
// 000114 PH int -> bool, virtual destructors, other minor changes
// 010129 PH added void type to initConnection
// 011212 PH changed char* arguments to const char*
// changed connection closed handling to exception
// unsigned char instead of char/int in write/read
// 020102 PH split into separate file for each class
// 040421 PH added namespace, new casts, cleaned up a lot
// 050113 PH added deregisterConnection, new registration (vector),
// added check for server shutdown, many more changes
// 130515 PH removed namespace
//
// ------------------------------------------------------------------
#ifndef CONNECTION_H
#define CONNECTION_H
class Server;
/* A Connection object represents a connection (a socket) */
class Connection {
friend class Server;
public:
/* Establishes a connection to the computer 'host' via
the port 'port' */
Connection(const char* host, int port);
/* Creates a Connection object which will be initialized
by the server */
Connection();
/* Closes the connection */
virtual ~Connection();
/* Returns true if the connection has been established */
bool isConnected() const;
/* Writes a character */
void write(unsigned char ch) const;
/* Reads a character */
unsigned char read() const;
protected:
/* The socket number that this connections communicates on */
int my_socket;
/* Set to true when the constructor has called signal()
to ignore broken pipe. See comment in the constructor */
static bool ignoresPipeSignals;
/* Initialization from server, receives socket number s */
void initConnection(int s);
/* Server fetches the socket number */
int getSocket() const;
/* Prints error message and exits */
void error(const char* msg) const;
};
#endif