Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 0aa4fe64c652a16e12d1351ccfe11578e766e596 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
   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
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <assert.h>

#include <iostream>

#include "sys/sys.h"
#include "net/net.h"
#include "net/tcpconnection.h"

namespace net
{

TCPConnection::TCPConnection()
{
	tcpconnection_error = false;
	tcpconnection_fd = -1;
	tcpconnection_port = 0;
}

TCPConnection::~TCPConnection()
{
	disconnect();
}

std::string TCPConnection::host() const
{
	return tcpconnection_host;
}

int TCPConnection::port() const
{
	return tcpconnection_port;
}

bool TCPConnection::error() const
{
	return tcpconnection_error;
}

bool TCPConnection::valid() const
{
	return (tcpconnection_fd != -1);
}

bool TCPConnection::invalid() const
{
	return (tcpconnection_fd == -1);
}

int TCPConnection::fd() const
{
	return (tcpconnection_fd);
}


bool TCPConnection::connected()
{
	return ((tcpconnection_fd != -1) && !tcpconnection_error);
}


void TCPConnection::abort()
{
	tcpconnection_error= true;
}

void TCPConnection::disconnect()
{
	if (tcpconnection_fd != -1)
		close(tcpconnection_fd);
	tcpconnection_fd = -1;
	tcpconnection_error = false;
	tcpconnection_host.clear();
	tcpconnection_port = 0;
	//con_debug << "Network disconnected from server" << std::endl;
}

void TCPConnection::connect(std::string const &to_host, int to_port)
{
	if (valid())
		return;
		
	// resolve serverhostname
	struct hostent *serverhostent;
	serverhostent = gethostbyname(to_host.c_str());
	if (!serverhostent) {
		con_warn << "Could not resolve '" << to_host.c_str() << "'" << std::endl;
		abort();
		return;
	}
	
	con_print << "Connecting to " << inet_ntoa(*((struct in_addr *)serverhostent->h_addr))  << ":" << to_port << "..." << std::endl;
	
	// Get a socket file descriptor
	tcpconnection_fd = socket(PF_INET, SOCK_STREAM, 0);
	if (tcpconnection_fd == -1) {
		//con_error << "Network socket() failed!" << std::endl;
		abort();
		return;
	}
	
	// make the connection
	struct sockaddr_in server_addr;
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(to_port);
	server_addr.sin_addr.s_addr = inet_addr(inet_ntoa(*((struct in_addr *)serverhostent->h_addr)));
	memset(server_addr.sin_zero, '\0', sizeof server_addr.sin_zero);
	
	if (::connect(tcpconnection_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != 0) {
		//con_error << "Network connect() failed!" << std::endl;
		abort();
		return;
	}
	
	tcpconnection_host = to_host;
	tcpconnection_port = to_port;
	tcpconnection_error = false;
	//con_print << "Connected to server." << std::endl;
	
}

void TCPConnection::receive(std::string &msg)
{
	if (error() || invalid())
		return;
		
	char		recvbuf[FRAMESIZE]; // maximum block sizeq
	size_t		msglen = sizeof(recvbuf);
	ssize_t		bytes_received;
	
	memset(recvbuf, '\0', sizeof(recvbuf));
	bytes_received = ::recv(tcpconnection_fd, recvbuf, msglen, 0);
	if (bytes_received == 0) {
		con_print << "Disconnected.";
		abort();
		return;
	} else if (bytes_received < 0) {
		con_error << "Network receive() error!" << std::endl;
		//perror("recv");
		abort();
		return;
	}
	msg = recvbuf;
}

void TCPConnection::send(std::string const &msg)
{
	if (error() || invalid())
		return;
		
	if (msg.size() > FRAMESIZE) {
		con_warn << "Network message exceeds " << FRAMESIZE << " bytes!\n";
		return;
	}
	
	ssize_t 	bytes_sent = 0;
	size_t		total_sent = 0;
	std::string	sendbuf(msg);
	
	while (total_sent <  msg.size()) {
		bytes_sent = ::send(tcpconnection_fd, sendbuf.c_str(), sendbuf.size(), 0);
		if (bytes_sent < 0) {
			con_error << "Network send() error!" << std::endl;
			//perror("send");
			abort();
			return;
		}
		total_sent += bytes_sent;
		
		// assert (bytes_sent <= sendbuf.size());
		sendbuf.erase(sendbuf.size() - bytes_sent, bytes_sent);
	}
	
	return;
}

}