summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-01-05 18:07:25 +0300
committerJoursoir <chat@joursoir.net>2021-01-05 18:07:25 +0300
commita76b00c511db1d3529bba51475a24ca93d12913e (patch)
treedc0cb14e52b7d37cb76a58bdc54cb2aac1bc9e11
parenta5d15a716f6e7e21a67529478f77851d15defe09 (diff)
downloadwant-chat-a76b00c511db1d3529bba51475a24ca93d12913e.tar.gz
want-chat-a76b00c511db1d3529bba51475a24ca93d12913e.tar.bz2
want-chat-a76b00c511db1d3529bba51475a24ca93d12913e.zip
client gui: add support windows
-rw-r--r--.gitignore1
-rw-r--r--src/client/ClientBase.cpp103
-rw-r--r--src/client/ClientBase.hpp12
-rw-r--r--src/client/gui/Makefile13
-rw-r--r--src/client/gui/OO_FLTK.cpp3
-rw-r--r--src/client/gui/OO_FLTK.hpp6
-rw-r--r--src/client/gui/WantChat.exebin0 -> 1134821 bytes
7 files changed, 107 insertions, 31 deletions
diff --git a/.gitignore b/.gitignore
index db00a79..47e41da 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@ src/client/clui/client
src/client/gui/client
src/server/server
src/server/hash*
+src/config.hpp
*.o \ No newline at end of file
diff --git a/src/client/ClientBase.cpp b/src/client/ClientBase.cpp
index 8988622..2256532 100644
--- a/src/client/ClientBase.cpp
+++ b/src/client/ClientBase.cpp
@@ -1,51 +1,112 @@
#include <string.h>
#include <unistd.h>
-#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
+#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
+#endif
#include <fcntl.h>
#include <cerrno>
+#include <stdio.h>
#include "ClientBase.hpp"
ClientBase::ClientBase(const char* ip, int port)
: out_buf_used(0), exit_flag(false), connection(true)
{
- fd = CreateSocket(ip, port);
+ if(InitSocket(ip, port) == -1) {
+ exit(5);
+#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::CreateSocket(const char* ip, int port)
+int ClientBase::ConstuctorError() const
{
- int client = socket(AF_INET, SOCK_STREAM, 0);
- if(client == -1) return -1;
+#ifdef _WIN32
+ return fd == INVALID_SOCKET ? 1 : 0;
+#else
+ return fd == -1 ? 1 : 0;
+#endif
+}
- // 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
+ */
+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;
+ }
+#else // linux socket:
+ struct sockaddr_in server_address;
- /* call bind optional, the system chooses
- the address automatically */
+ 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));
- 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 -1;
+ if(!inet_aton(ip, &(server_adress.sin_addr))) {
+ close(fd);
+ return -1;
+ }
- int res = connect(client, (struct sockaddr*) &server_adress,
+ result = connect(client, (struct sockaddr*) &server_adress,
sizeof(server_adress));
- if(res == -1) return -1;
-
- int flags = fcntl(client, F_GETFL, 0);
- fcntl(client, F_SETFL, flags | O_NONBLOCK);
+ if(result == -1) {
+ close(fd);
+ return -1;
+ }
- return client;
+ int flags = fcntl(fd, F_GETFL, 0);
+ fcntl(fd, F_SETFL, flags | O_NONBLOCK);
+#endif
+ return 0;
}
int ClientBase::Run()
diff --git a/src/client/ClientBase.hpp b/src/client/ClientBase.hpp
index a5e76ed..dbe28bd 100644
--- a/src/client/ClientBase.hpp
+++ b/src/client/ClientBase.hpp
@@ -1,11 +1,19 @@
#ifndef WC_CLIENTBASE_H
#define WC_CLIENTBASE_H
+#ifdef _WIN32
+ #include <winsock.h>
+#endif
+
#include "../const_vars.hpp"
class ClientBase {
protected:
+#ifdef _WIN32
+ SOCKET fd;
+#else
int fd;
+#endif
char out_buffer[max_msg_len]; // for message
int out_buf_used;
@@ -15,7 +23,7 @@ protected:
public:
ClientBase(const char* ip, int port);
virtual ~ClientBase();
- int ConstuctorError() const { return fd > -1 ? 0 : 1; }
+ int ConstuctorError() const;
int Run();
void BreakLoop() { exit_flag = true; }
@@ -24,7 +32,7 @@ public:
virtual void AddMessage(const char *msg, const char spec_char) {}
void SendMessage(const char *msg);
private:
- int CreateSocket(const char* ip, int port);
+ int InitSocket(const char* ip, int port);
};
#endif \ No newline at end of file
diff --git a/src/client/gui/Makefile b/src/client/gui/Makefile
index a72e64d..d317bfc 100644
--- a/src/client/gui/Makefile
+++ b/src/client/gui/Makefile
@@ -1,8 +1,15 @@
CPP = g++
-CPPFLAGS = -Wall -g -lfltk
+CPPFLAGS = -Wall -g
+LDFLAGS =
+ifeq ($(OS),Windows_NT)
+ CPPFLAGS += -static-libgcc -static-libstdc++ $(shell fltk-config --cxxflags)
+ LDFLAGS += $(shell fltk-config --ldflags) -lws2_32
+else
+ CPPFLAGS += -lfltk
+endif
SOURCES = main.cpp OO_FLTK.cpp ../ClientBase.cpp Client.cpp
OBJECTS = main.o OO_FLTK.o ClientBase.o Client.o
-EXECUTABLE = client
+EXECUTABLE = WantChat
.PHONY: all clean
@@ -12,7 +19,7 @@ clean:
rm -rf $(OBJECTS) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
- $(CPP) $(CPPFLAGS) -o $(EXECUTABLE) $(OBJECTS)
+ $(CPP) $(CPPFLAGS) $(OBJECTS) $(LDFLAGS) -o $(EXECUTABLE)
$(OBJECTS):
$(CPP) -c $(CPPFLAGS) $(SOURCES) \ No newline at end of file
diff --git a/src/client/gui/OO_FLTK.cpp b/src/client/gui/OO_FLTK.cpp
index 6492481..89e88e3 100644
--- a/src/client/gui/OO_FLTK.cpp
+++ b/src/client/gui/OO_FLTK.cpp
@@ -28,7 +28,7 @@ ChatInput::ChatInput(int x, int y, int w, int h,
maximum_size(max_usermsg_len);
}
-void ChatInput::SendMessage(void *user)
+void ChatInput::SendMsg(void *user)
{
if(strlen(value()) < 1)
return;
@@ -36,7 +36,6 @@ void ChatInput::SendMessage(void *user)
Client *cl = (Client *)user;
if(cl != 0) { // send message to server
const char *message = value();
- printf("U sent: %s\n", message);
cl->SendMessage(message);
}
diff --git a/src/client/gui/OO_FLTK.hpp b/src/client/gui/OO_FLTK.hpp
index a2f7183..334f46a 100644
--- a/src/client/gui/OO_FLTK.hpp
+++ b/src/client/gui/OO_FLTK.hpp
@@ -21,11 +21,11 @@ class ChatInput : public Fl_Input {
public:
ChatInput(int x, int y, int w, int h,
const char *lb = 0, Client *user = 0);
- virtual ~ChatInput() {}
- virtual void SendMessage(void *user);
+ ~ChatInput() {}
+ void SendMsg(void *user);
private:
static void CallbackFunction(Fl_Widget *w, void *user)
- { static_cast<ChatInput*>(w)->SendMessage(user); }
+ { static_cast<ChatInput*>(w)->SendMsg(user); }
};
class ChatBaseOutput : public Fl_Output {
diff --git a/src/client/gui/WantChat.exe b/src/client/gui/WantChat.exe
new file mode 100644
index 0000000..ca27ced
--- /dev/null
+++ b/src/client/gui/WantChat.exe
Binary files differ