summaryrefslogtreecommitdiffstats
path: root/src/client/ClientBase.cpp
blob: 8646674e36ce1cb1b0ec99c9db58e97033f8f30d (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
#include <string.h>
#include <unistd.h> 
#ifndef _WIN32
	#include <netinet/in.h> // for sockaddr_in
	#include <arpa/inet.h> // for iten_aton
	#include <sys/types.h> // for bind, connect
	#include <sys/socket.h> // for bind, connect
	#include <fcntl.h>
	#include <cerrno>
#endif

#include "ClientBase.hpp"

ClientBase::ClientBase(const char* ip, int port)
	: out_buf_used(0), exit_flag(false), connection(true)
{
	if(InitSocket(ip, port) == -1) {
#ifdef _WIN32
		fd = INVALID_SOCKET;
#else
		fd = -1;
#endif
	}
}

ClientBase::~ClientBase()
{
#ifdef _WIN32
	if(fd != INVALID_SOCKET) {
		closesocket(fd);
	}
	WSACleanup();
#else
	if(fd != -1)
		close(fd);
#endif
}

int ClientBase::ConstuctorError() const
{
#ifdef _WIN32
	return fd == INVALID_SOCKET ? 1 : 0;
#else
	return fd == -1 ? 1 : 0;
#endif
}

/* 
 * call bind optional, the system chooses
 * the address automatically
 */
int ClientBase::InitSocket(const char* ip, int port)
{
	int result;
	int opt = 1; // for remove "port sticking"
	int ifamily = AF_INET;
	int itype = SOCK_STREAM;
	int iprotocol = 0;

#ifdef _WIN32 // windows socket
	WSADATA wsaData;
	SOCKADDR_IN server_address;

	// initialize winsock:
	result = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if(result != 0) return -1;

	fd = socket(ifamily, itype, iprotocol);
	if(fd == INVALID_SOCKET)  return -1;

	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt));

	server_address.sin_family = ifamily;
	server_address.sin_addr.s_addr = inet_addr(ip);
	server_address.sin_port = htons(port);
	result = connect(fd, (SOCKADDR *) &server_address, sizeof(server_address));
	if(result == SOCKET_ERROR) {
		closesocket(fd);
		return -1;
	}

	// nonblocking socket mode:
	u_long imode = 1;
	ioctlsocket(fd, FIONBIO, &imode);
#else // linux socket:
	struct sockaddr_in server_address;

	fd = socket(ifamily, itype, iprotocol);
	if(fd == -1) return -1;

	// remove "port sticking" aka socket in TIME_WAIT
	setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

	server_address.sin_family = AF_INET;
	server_address.sin_port = htons(port);
	if(!inet_aton(ip, &(server_address.sin_addr))) {
		close(fd);
		return -1;
	}
	
	result = connect(fd, (struct sockaddr*) &server_address,
		sizeof(server_address));
	if(result == -1) {
		close(fd);
		return -1;
	}

	// nonblocking socket mode:
	int flags = fcntl(fd, F_GETFL, 0);
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);
#endif
	return 0;
}

int ClientBase::Run()
{
	if(exit_flag)
		return 0;
	HandleActions();

	int recive = recv(fd, out_buffer+out_buf_used,
		sizeof(out_buffer)-out_buf_used, 0);

	if(recive < 0) {
#ifdef _WIN32
		if(WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK)
			return 0;
#else
		if(errno != EINTR && errno != EAGAIN) 
			return 0;
#endif
	}
	else if(recive > 0)
		out_buf_used += recive;
	else {
		if(connection) {
			strcpy(out_buffer, "Server closed the connection. Use ESC to exit.");
			AddMessage(out_buffer, SYSTEM_CHAR);
			connection = false;
		}
	}

	if(out_buf_used > 0) {
		/* warning: if we get a (message without '\n') > max_msg_len then
		this code will not work */
		for(int i = 0; i < out_buf_used; i++) {
			if(out_buffer[i] == '\n') {
				out_buffer[i] = 0;
	
				// in first char have may spec-symbol, check it:
				char spec_char = USUAL_CHAR;
				char *buf = out_buffer;
				switch(out_buffer[0]) {
					case SYSTEM_CHAR:
					case USERS_CHAR:
					case GONLINE_CHAR:
					case RONLINE_CHAR: {
						spec_char = out_buffer[0];
						buf += 1;
						break;
					}
					default: break;
				}

				AddMessage(buf, spec_char);
				memmove(out_buffer, out_buffer + i + 1, out_buf_used - i - 1);
				out_buf_used -= i + 1;
				break;
			}
		}
	}
	return 1;
}

void ClientBase::SendMessage(const char *msg)
{
	int len = strlen(msg) + 1;
	char *buf = new char[len];
	strcpy(buf, msg);
	buf[len-1] = '\n';

	send(fd, buf, len * sizeof(char), 0);
}