summaryrefslogtreecommitdiffstats
path: root/src/client/user.cpp
blob: 91b2450d61488445c92b4ce333964cf13824529e (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
#include <string.h>
#include <unistd.h> // for close
#include <netinet/in.h> // for iten_aton, iten_ntoa, sockaddr_in
#include <arpa/inet.h> // for iten_aton, iten_ntoa
#include <sys/types.h> // for bind, connect
#include <sys/socket.h> // for bind, connect
#include <fcntl.h> // for fcntl
#include <cerrno> // for errno
#include <stdio.h>

#include "user.hpp"

const int key_esc = 4;
const int key_enter = 10;
const int key_escape = 27;
const int key_backspace = 127;

Client *Client::Start(const char* ip, int port)
{
	int client;
	client = socket(AF_INET, SOCK_STREAM, 0);
	if(client == -1) return 0;

	// remove "port sticking" aka socket in TIME_WAIT
	int opt = 1;
	setsockopt(client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

	/* call bind optional, the system chooses
		the address automatically */

	struct sockaddr_in server_adress;
	server_adress.sin_family = AF_INET;
	server_adress.sin_port = htons(port);
	if(!inet_aton(ip, &(server_adress.sin_addr))) return 0;
	
	int res = connect(client, (struct sockaddr*) &server_adress,
		sizeof(server_adress));
	if(res == -1) return 0;

	return new Client(client);
}

void Client::Run(ChatRoom *room)
{
	unsigned int usecs = 0125000;
	int flags = fcntl(fd, F_GETFL, 0);
	fcntl(fd, F_SETFL, flags | O_NONBLOCK);

	nodelay(room->GetInputWin(), true);
	curs_set(true);
	do {
		this->HandleButton(room);

		int recive = read(fd, out_buffer, sizeof(out_buffer));

		if(recive < 0) {
			if(errno != EINTR && errno != EAGAIN)
				break;
		}
		else if(recive > 0) {
			/* warning: if we get a message >= maxlen_outbuf then
			this code will not work */
			out_buffer[recive-1] = '\0'; // change '\n' to '\0'
			room->AddMessage(out_buffer);
			memset(out_buffer, 0, recive);
		}

		usleep(usecs);
	} while (!exit_flag);
}

void Client::HandleButton(ChatRoom *room)
{
	int key = wgetch(room->GetInputWin());
	switch(key)
	{
		case key_esc: {
			// #todo: exit
			break;
		}
		// ascii table 32...126
		case ' '...'~': {
			AddCharToBuffer(key);
			room->AddCharToSendMsg(key);
			break;
		}
		case '\n': { // send message
			SendMessage();
			room->InputClear();
			room->SetInputCursor(1, 1);
			break;
		}
		case key_backspace: {
			RemoveCharFromBuffer();
			room->RemoveCharFromMsg();
			break;
		}
		default: break;
	}
}

void Client::AddCharToBuffer(char ch)
{
	if(in_buf_used >= maxlen_inbuf-1)
		return;

	in_buffer[in_buf_used] = ch;
	in_buf_used++;
}

void Client::RemoveCharFromBuffer()
{
	if(in_buf_used <= 0)
		return;

	in_buffer[in_buf_used] = '\0';
	in_buf_used--;
}

void Client::SendMessage()
{
	if(in_buf_used <= 0)
		return;

	in_buffer[in_buf_used] = '\n';

	write(fd, in_buffer, (in_buf_used+1)*sizeof(char));
	memset(in_buffer, 0, (in_buf_used+1)*sizeof(char)); 
	in_buf_used = 0;
}