-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnetwork.hpp
92 lines (78 loc) · 2.35 KB
/
mnetwork.hpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef _NETWORK_H_
#define _NETWORK_H_
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <vector>
#include <map>
#define MAX_BUFFER 1024
typedef unsigned long int SESSIONID;
enum NetworkManagerMode {
UNSET=1,
CLIENT,
SERVER
};
/*
* A manager for multiple sockets, either as a client or server
* depending on how it was invoked.
*/
class NetworkManager
{
public:
// Construtor
NetworkManager();
// Destructor
~NetworkManager(void);
// Disable the copy constructor and the assignment operator.
NetworkManager(NetworkManager &source) = delete;
NetworkManager& operator=(NetworkManager &source) = delete;
// Write data to an open socket
ssize_t write(const std::string msg, SESSIONID sessionid=1);
// Read data from an open socket
ssize_t read(std::string &buffer, SESSIONID sessionid=1);
protected:
// Note: socket fds for sessions are mapped in m_sessionmap.
// The socket fd
int m_sockfd;
// The bind port.
int m_bind_port;
// Client or server?
NetworkManagerMode m_mode;
// Map of sessionids to network fds.
std::map<SESSIONID,int> m_sessionmap;
// An incrementing sessionid.
SESSIONID m_latest_sessionid;
// Setting the mode.
void set_mode(NetworkManagerMode mode);
};
class TcpNetworkManager: public NetworkManager
{
public:
TcpNetworkManager();
~TcpNetworkManager();
// Disable the copy constructor and the assignment operator.
TcpNetworkManager(TcpNetworkManager &source) = delete;
TcpNetworkManager& operator=(TcpNetworkManager &source) = delete;
// Connect to a remote host as a client.
SESSIONID connect_to(std::string host, std::string port);
int listen(int port);
SESSIONID accept();
// A print method for sending data an iostream
std::string print();
private:
};
std::ostream &operator<<(std::ostream &os, TcpNetworkManager &manager);
class UdpNetworkManager: public NetworkManager
{
public:
UdpNetworkManager();
~UdpNetworkManager();
// Disable the copy constructor and the assignment operator.
UdpNetworkManager(UdpNetworkManager &source) = delete;
UdpNetworkManager& operator=(UdpNetworkManager &source) = delete;
// A print method for sending data an iostream
std::string print();
private:
};
std::ostream &operator<<(std::ostream &os, UdpNetworkManager &manager);
#endif