Skip to content

Commit b85c0db

Browse files
authored
Merge pull request eminfedar#1 from TheBeardedQuack/master
Created a project structure to build a shared library.
2 parents 5d4553d + b99c631 commit b85c0db

23 files changed

+739
-425
lines changed

MakeHelper

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!make
2+
3+
#OS Commands
4+
define NoTrailSlash
5+
$(patsubst %\\,%,$(patsubst %/,%,$(1)))
6+
endef
7+
define MakeExe
8+
$(1)$(EXEEXT)
9+
endef
10+
11+
#If Windows
12+
ifeq ($(OS), Windows_NT)
13+
14+
DLLEXT := .dll
15+
EXEEXT := .exe
16+
17+
define ReplaceSlash
18+
$(subst /,\\,$(1))
19+
endef
20+
define MakeLib
21+
$(1)$(DLLEXT)
22+
endef
23+
define RM
24+
-del /F /Q $(call ReplaceSlash,$(call NoTrailSlash,$(1)))
25+
endef
26+
define MD
27+
-mkdir $(call ReplaceSlash,$(call NoTrailSlash,$(1)))
28+
endef
29+
define CP
30+
-robocopy $(call ReplaceSlash,$(1)) $(call ReplaceSlash,$(2)) /E /is
31+
endef
32+
33+
else
34+
#If not Windows, assume Linux
35+
36+
DLLEXT := .so
37+
EXEEXT :=
38+
39+
define ReplaceSlash
40+
$(subst \\,/,$(1))
41+
endef
42+
define MakeLib
43+
lib$(1)$(DLLEXT)
44+
endef
45+
define RM
46+
rm -rf $(1)
47+
endef
48+
define MD
49+
mkdir -p $(1)
50+
endef
51+
define CP
52+
cp -rf $(1) $(2)
53+
endef
54+
55+
endif

easysocket/Makefile

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!make
2+
# This make needs:
3+
# g++
4+
#
5+
6+
include ../MakeHelper
7+
8+
# Project name
9+
NAME := $(patsubst %/,%,$(notdir $(CURDIR)))
10+
11+
#The Target Binary Program
12+
TARGET := $(call MakeLib,$(NAME))
13+
14+
#The Directories, Source, Includes, Objects, Binary and Resources
15+
SRCDIR ?= $(CURDIR)/src
16+
INCDIR ?= $(CURDIR)/include
17+
BUILDDIR ?= $(CURDIR)/obj/$(BUILD)
18+
TARGETDIR ?= $(CURDIR)/bin/$(BUILD)
19+
RESDIR ?= $(CURDIR)/rsc
20+
SRCEXT := .cpp
21+
OBJEXT := .o
22+
23+
#Compiler and Linker
24+
BUILD ?= debug
25+
BITS ?= 64
26+
CC := g++
27+
28+
#Flags, Libraries and Includes
29+
CFLAGS.common := -std=c++17 -fPIC -m$(BITS) -Wall -Wextra -DBUILD_EASYSOCKET
30+
CFLAGS.debug := $(CFLAGS.common) -g
31+
CFLAGS.release := $(CFLAGS.common) -Werror
32+
CFLAGS ?= $(CFLAGS.$(BUILD))
33+
LIB := -L$(TARGETDIR)
34+
INC := -I$(INCDIR)
35+
36+
#---------------------------------------------------------------------------------
37+
#DO NOT EDIT BELOW THIS LINE
38+
#---------------------------------------------------------------------------------
39+
SOURCES := $(wildcard $(SRCDIR)/*$(SRCEXT))
40+
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:$(SRCEXT)=$(OBJEXT)))
41+
42+
#Default Make
43+
all: resources $(TARGET) tests docs
44+
45+
#Remake
46+
remake: cleaner all
47+
48+
#Create template folder structure
49+
template: directories
50+
$(call MD,$(SRCDIR))
51+
$(call MD,$(INCDIR))
52+
$(call MD,$(RESDIR))
53+
54+
#Make the Directories
55+
directories:
56+
$(call MD,$(TARGETDIR))
57+
$(call MD,$(BUILDDIR))
58+
59+
#Copy Resources from Resources Directory to Target Directory
60+
resources: template
61+
# @$(call CP,$(RESDIR)/*,$(TARGETDIR)/)
62+
63+
#Clean only Objecst
64+
clean:
65+
$(call RM,$(BUILDDIR))
66+
67+
#Full Clean, Objects and Binaries
68+
cleaner: clean
69+
$(call RM,$(TARGETDIR))
70+
71+
docs:
72+
#TODO
73+
74+
tests:
75+
#TODO
76+
77+
#Link the final executable/library
78+
$(TARGET): $(OBJECTS)
79+
$(CC) -shared -o $(TARGETDIR)/$(TARGET) $^ $(LIB)
80+
81+
#Compile
82+
$(BUILDDIR)/%$(OBJEXT): $(SRCDIR)/%$(SRCEXT)
83+
$(call MD,$(dir $@))
84+
$(CC) $(CFLAGS) $(INC) -c -o $@ $<
85+
86+
#Non-File Targets
87+
.PHONY: all remake template directories resources clean cleaner

easysocket/basesocket.h

Lines changed: 0 additions & 75 deletions
This file was deleted.

easysocket/include/DllHelper.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifdef BUILD_EASYSOCKET
2+
#define EASYSOCKET_API EXPORT_EASYSOCKET
3+
#else
4+
#define EASYSOCKET_API IMPORT_EASYSOCKET
5+
#endif
6+
7+
#if defined(_MSC_VER)
8+
// Microsoft
9+
#define EXPORT_EASYSOCKET __declspec(dllexport)
10+
#define IMPORT_EASYSOCKET __declspec(dllimport)
11+
#elif defined(__GNUC__)
12+
// GCC
13+
#define EXPORT_EASYSOCKET __attribute__((visibility("default")))
14+
#define IMPORT_EASYSOCKET
15+
#else
16+
// do nothing and hope for the best?
17+
#define EXPORT_EASYSOCKET
18+
#define IMPORT_EASYSOCKET
19+
#pragma warning Unknown dynamic link import/export semantics.
20+
#endif

easysocket/include/basesocket.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef FDR_BASESOCKET_H
2+
#define FDR_BASESOCKET_H
3+
4+
#include <DllHelper.h>
5+
6+
#ifdef __linux__
7+
#include <arpa/inet.h>
8+
#include <sys/socket.h>
9+
#include <sys/types.h>
10+
#include <unistd.h>
11+
#elif _WIN32
12+
#include <winsock32.h>
13+
#endif
14+
15+
#include <string>
16+
#include <functional>
17+
18+
class EASYSOCKET_API BaseSocket
19+
{
20+
protected:
21+
int sock = 0;
22+
23+
static std::string ipToString(sockaddr_in addr);
24+
25+
public:
26+
enum EASYSOCKET_API SocketType
27+
{
28+
TCP = SOCK_STREAM,
29+
UDP = SOCK_DGRAM
30+
};
31+
32+
sockaddr_in address;
33+
bool isClosed = false;
34+
35+
std::function<void(std::string)> onError;
36+
37+
BaseSocket(SocketType sockType = TCP, int socketId = -1);
38+
39+
void Close();
40+
41+
std::string remoteAddress();
42+
int remotePort();
43+
};
44+
45+
#endif

easysocket/include/socket.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef FDR_SOCKET_H
2+
#define FDR_SOCKET_H
3+
4+
#include <DllHelper.h>
5+
6+
#include <basesocket.h>
7+
#include <string>
8+
#include <functional>
9+
#include <thread>
10+
11+
#define BUFFER_SIZE 0xFFFF
12+
13+
class EASYSOCKET_API Socket : public BaseSocket
14+
{
15+
public:
16+
// Event Listeners:
17+
std::function<void(std::string)> onMessageReceived;
18+
std::function<void()> onSocketClosed;
19+
20+
Socket(int socketId = -1);
21+
22+
void Send(std::string message);
23+
void Send(const char *bytes, size_t byteslength);
24+
25+
void Connect(std::string ipv4, uint16_t port, std::function<void()> onConnected);
26+
void Connect(uint32_t IPv4, uint16_t port, std::function<void()> onConnected);
27+
28+
void Listen();
29+
30+
void setAddressStruct(sockaddr_in addr);
31+
32+
private:
33+
static void Receive(Socket *socket);
34+
};
35+
36+
#endif

easysocket/include/tcpserver.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef FDR_TCPSERVER_H
2+
#define FDR_TCPSERVER_H
3+
4+
#include <DllHelper.h>
5+
6+
#include <socket.h>
7+
#include <string>
8+
#include <functional>
9+
#include <thread>
10+
11+
class EASYSOCKET_API TCPServer : public BaseSocket
12+
{
13+
public:
14+
// Event Listeners:
15+
std::function<void(Socket *)> onNewConnection;
16+
17+
TCPServer();
18+
19+
// Binding the server.
20+
void Bind(int port);
21+
void Bind(const char *address, uint16_t port);
22+
23+
// Start listening the server.
24+
void Listen();
25+
26+
private:
27+
static void Accept(TCPServer *server);
28+
};
29+
30+
#endif

easysocket/include/udpserver.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef FDR_UDPSERVER_H
2+
#define FDR_UDPSERVER_H
3+
4+
#include <DllHelper.h>
5+
6+
#include <udpsocket.h>
7+
#include <string>
8+
9+
class EASYSOCKET_API UDPServer : public UDPSocket
10+
{
11+
public:
12+
UDPServer();
13+
14+
void Bind(int port);
15+
void Bind(const char *address, std::uint16_t port);
16+
};
17+
18+
#endif

0 commit comments

Comments
 (0)