summaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2020-11-30 02:29:18 +0300
committerJoursoir <chat@joursoir.net>2020-11-30 02:29:18 +0300
commit7385c0352c7fe7e9ceca343400a5caa5ddd999d6 (patch)
tree9f6968d4c7b646a64f36baf214fcf5af622f366d /src/client
parent4d4cd095cd3a2f0b65f0ed19af51c667c5941abc (diff)
downloadwant-chat-7385c0352c7fe7e9ceca343400a5caa5ddd999d6.tar.gz
want-chat-7385c0352c7fe7e9ceca343400a5caa5ddd999d6.tar.bz2
want-chat-7385c0352c7fe7e9ceca343400a5caa5ddd999d6.zip
refactoring and some feature
client: type nick in argument server: basis for registration; change style add commands; edit send msg to user; feature: private room
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.cpp9
-rw-r--r--src/client/user.cpp37
-rw-r--r--src/client/user.hpp5
3 files changed, 40 insertions, 11 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp
index 7f092cc..8143b9e 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -10,6 +10,11 @@ static int port = 3030;
int main(int argc, char *argv[])
{
+ if(argc < 2) {
+ printf("Usage: client *name*\n");
+ return 1;
+ }
+
initscr();
//raw();
noecho();
@@ -22,10 +27,10 @@ int main(int argc, char *argv[])
return 1;
}
- Client *user = Client::Start(SERVER_IP, port);
+ Client *user = Client::Start(SERVER_IP, port, argv[1]);
if(!user) {
endwin();
- perror("client");
+ perror("server");
return 1;
}
diff --git a/src/client/user.cpp b/src/client/user.cpp
index 957665e..31cb5e5 100644
--- a/src/client/user.cpp
+++ b/src/client/user.cpp
@@ -10,7 +10,24 @@
#include "user.hpp"
-Client *Client::Start(const char* ip, int port)
+const int key_enter = 10;
+const int key_escape = 27;
+const int key_backspace = 127;
+
+//
+
+Client::Client(int i_fd, char *username) : fd(i_fd), in_buf_used(0),
+ out_buf_used(0)
+{
+ int len = strlen(username);
+ char *name = new char[len+2];
+ sprintf(name, "%s\n", username);
+
+ write(i_fd, name, strlen(name));
+ delete[] name;
+}
+
+Client *Client::Start(const char* ip, int port, char *username)
{
int client;
client = socket(AF_INET, SOCK_STREAM, 0);
@@ -32,7 +49,7 @@ Client *Client::Start(const char* ip, int port)
sizeof(server_adress));
if(res == -1) return 0;
- return new Client(client);
+ return new Client(client, username);
}
void Client::Run(ChatRoom *room)
@@ -41,6 +58,7 @@ void Client::Run(ChatRoom *room)
int flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+ int cls = false;
do {
this->HandleButton(room);
@@ -53,6 +71,13 @@ void Client::Run(ChatRoom *room)
}
else if(recive > 0)
out_buf_used += recive;
+ else {
+ if(!cls) {
+ strcpy(out_buffer, "Server closed the connection. Use ESC to exit.");
+ room->AddMessage(out_buffer, system_msg);
+ cls = true;
+ }
+ }
if(out_buf_used > 0) {
/* warning: if we get a (message without '\n') > max_msg_len then
@@ -62,14 +87,14 @@ void Client::Run(ChatRoom *room)
out_buffer[i] = 0;
// in first char have may spec-symbol, check it:
- int spec_symbol = usually_msg;
+ int spec_msg = usual_msg;
char *buf = out_buffer;
- if(out_buffer[0] == '#') {
- spec_symbol = system_msg;
+ if(out_buffer[0] == system_char) {
+ spec_msg = system_msg;
buf += 1;
}
- room->AddMessage(buf, spec_symbol);
+ room->AddMessage(buf, spec_msg);
memmove(out_buffer, out_buffer + i + 1, out_buf_used - i - 1);
out_buf_used -= i + 1;
break;
diff --git a/src/client/user.hpp b/src/client/user.hpp
index 8e2218a..1ae8427 100644
--- a/src/client/user.hpp
+++ b/src/client/user.hpp
@@ -14,12 +14,11 @@ class Client {
bool exit_flag;
- Client(int i_fd)
- : fd(i_fd), in_buf_used(0), out_buf_used(0) { }
+ Client(int i_fd, char *username);
public:
~Client() { close(fd); }
- static Client *Start(const char* ip, int port);
+ static Client *Start(const char* ip, int port, char *username);
void Run(ChatRoom *room);
void BreakLoop() { exit_flag = true; }
void HandleButton(ChatRoom *room);