-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.h
59 lines (46 loc) · 1.71 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
#ifndef CONNECTION_H
#define CONNECTION_H
#include "csapp.h"
#include <string>
struct Message;
class Connection {
public:
// enumeration type describing reasons why a call to
// send or receive failed
enum Result {
SUCCESS, // send or receive was successful
EOF_OR_ERROR, // EOF or error receiving or sending data
INVALID_MSG, // message format was invalid
};
// Default constructor: Connection starts out as not connected,
// the connect member function must be called to create a connection.
// This is how a client should connect to the server.
Connection();
// Constructor from an open file descriptor, which is assumed
// to be a TCP socket. This is the constructor that the server
// should use when it has accepted a connection from a client.
Connection(int fd);
// Destructor. Should make sure that the file descriptor is closed.
~Connection();
// Connect to a server via specified hostname and port number.
void connect(const std::string &hostname, int port);
bool is_open() const;
void close();
// send and receive should set m_last_result to indicate
// whether the most recent send or receive was successful,
// and if not, whether the reason was an I/O error or reaching EOF,
// or whether the format of the received message was invalid
bool send(const Message &msg);
bool receive(Message &msg);
Result get_last_result() const { return m_last_result; }
private:
// prohibit value semantics
Connection(const Connection &);
Connection &operator=(const Connection &);
// these are the recommended member variables for the
// Connection class
int m_fd;
rio_t m_fdbuf; // used to allow buffered input
Result m_last_result;
};
#endif // CONNECTION_H