Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 7e71ed03dd3d5d18dfbc5adf94c1fd2c37f110e8 (plain)
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
/*
   net/tcpconnection.h
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#ifndef __INCLUDED_NET_TCPCONNECTION_H__
#define __INCLUDED_NET_TCPCONNECTION_H__

#include <string>

namespace net
{

/// A TCP client, connected to a file descriptor
/// Handles the connection from a client to a server
class TCPConnection
{
public:
	/// A new TCP client
	TCPConnection();
	
	/// Delete the TCP connection
	/// If the file descriptor is still open, it will be closed.
	virtual ~TCPConnection();
	
	/// returns true if connected
	bool connected();
	
	/// the remote hostname the client is connected to
	std::string host() const;
	
	/// the remote port the client is connected to
	int port() const;
	
	/// the error state of the TCP client
	bool error() const;
	
	/// returns true if the TCP connection has a valid file descriptor
	bool valid() const;
	
	/// true if the TCP connection has an invalid file descriptor
	bool invalid() const;
	
	/// return the file descriptor of the TCP connection
	int fd() const;
	
	/// connect to a remote host
	virtual void connect(std::string to_host, int to_port);
	
	/// disconnect from a remote host
	virtual void disconnect();
	
	/// set the TCP connection to the error state
	void abort();
	
	/// send outgoing data
	void send(std::string const &msg);
	
protected:
	/// receive incoming data
	void receive(std::string &msg);
	
private:
	int			tcpconnection_fd;
	bool			tcpconnection_error;
	std::string		tcpconnection_host;
	int			tcpconnection_port;
};

}

#endif // __INCLUDED_NET_TCPCONNECTION_H__