Skip to content

Commit 147646a

Browse files
Merge pull request #1 from benji-utility/rework
Rework the base API
2 parents 6c9fc3d + c4607a9 commit 147646a

File tree

14 files changed

+725
-263
lines changed

14 files changed

+725
-263
lines changed

.github/workflow/build.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: Build (Linux + Windows)
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
name: Build on ${{ matrix.os }}
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
matrix:
15+
os: [ubuntu-latest, windows-latest]
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
21+
- name: Set up build environment
22+
if: matrix.os == 'ubuntu-latest'
23+
run: |
24+
sudo apt-get update
25+
sudo apt-get install -y make gcc
26+
27+
- name: Set up build environment (Windows)
28+
if: matrix.os == 'windows-latest'
29+
run: |
30+
choco install make mingw
31+
shell: pwsh
32+
33+
- name: Build library and tests
34+
run: make all
35+
shell: bash
36+
37+
- name: Show build output
38+
run: ls -R build
39+
shell: bash

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ dkms.conf
5353

5454
# debug information files
5555
*.dwo
56+
57+
# Build directory
58+
build

Makefile

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,54 @@ GXX := gcc
33
GXX_FLAGS := -g -Wno-discarded-qualifiers
44

55
SRC := src
6-
TESTS := tests
76
BUILD := build
7+
OBJ := $(BUILD)/obj
88

99
SRCS := $(wildcard $(SRC)/*.c)
10-
TESTS_SRCS := $(wildcard $(TESTS)/*.c)
10+
OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS))
1111

12-
OBJS := $(patsubst $(SRC)/%.c, $(BUILD)/%.o, $(SRCS))
12+
TEST_DIR := tests
13+
TEST_OBJ := $(OBJ)/$(TEST_DIR)
14+
TEST_SRCS := $(wildcard $(TEST_DIR)/*.c)
15+
TEST_OBJS := $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ)/%.o, $(TEST_SRCS))
16+
TEST_BIN := $(patsubst $(TEST_DIR)/%.c, $(BUILD)/%, $(TEST_SRCS))
1317

14-
TESTS_LINKED_LIBS := -lWs2_32
18+
ifeq ($(OS), Windows_NT)
19+
LINKED_LIBS := -lWs2_32
20+
else
21+
LINKED_LIBS :=
22+
endif
1523

16-
# STATIC_LIB := $(BUILD)/libcrocket.a
24+
LIB := $(BUILD)/libcrocket.a
1725

18-
# all: clean $(STATIC_LIB)
19-
all: clean
26+
.PHONY: all clean mkbuild
2027

21-
# $(STATIC_LIB): $(OBJS)
22-
# ar rcs $@ $^
28+
all: mkbuild $(LIB) $(TEST_BIN)
2329

2430
$(OBJ)/%.o: $(SRC)/%.c
2531
$(GXX) $(GXX_FLAGS) -c $< -o $@
2632

27-
ifeq ($(OS), Windows_NT)
28-
.SILENT: clean
29-
endif
33+
$(TEST_OBJ)/%.o: $(TEST_DIR)/%.c
34+
$(GXX) $(GXX_FLAGS) -c $< -o $@
3035

31-
.PHONY: clean mkbuild
36+
$(LIB): $(OBJS)
37+
ar rcs $@ $(OBJS)
3238

33-
clean: mkbuild
34-
ifeq ($(OS), Windows_NT)
35-
del /Q /S $(BUILD)\*
36-
else
37-
find $(BUILD) -maxdepth 1 -type f -exec rm {} \;
38-
rm -rf $(OBJ)/*
39-
endif
39+
$(BUILD)/%: $(TEST_OBJ)/%.o $(LIB)
40+
$(GXX) $(GXX_FLAGS) -o $@ $< -L$(BUILD) -lcrocket $(LINKED_LIBS)
4041

4142
mkbuild:
4243
ifeq ($(OS), Windows_NT)
4344
if not exist "$(BUILD)" mkdir "$(BUILD)"
45+
if not exist "$(OBJ)" mkdir "$(OBJ)"
46+
if not exist "$(TEST_OBJ)" mkdir "$(TEST_OBJ)"
4447
else
45-
mkdir -p $(BUILD)
46-
mkdir -p $(OBJ)
48+
mkdir -p $(BUILD) $(OBJ) $(TEST_OBJ)
4749
endif
4850

49-
test: clean
50-
$(GXX) $(GXX_FLAGS) $(SRCS) $(TESTS_SRCS) -o $(BUILD)/tests $(TESTS_LINKED_LIBS)
51+
clean: mkbuild
52+
ifeq ($(OS), Windows_NT)
53+
del /Q /S $(BUILD)\*
54+
else
55+
rm -rf $(BUILD)/*
56+
endif

src/crocket.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef __CROCKET_H
22
#define __CROCKET_H
33

4+
#include "crocket_base.h"
5+
46
#include "socket.h"
57

68
#endif

src/crocket_base.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,33 @@
11
#include "crocket_base.h"
22

3-
#ifdef CROCKET_WINDOWS
4-
CROCKET_API bool crocket_winsock_init() {
3+
#if defined(CROCKET_WINDOWS)
4+
static bool _crocket_is_winsock_initialized = false;
5+
6+
CROCKET_API bool winsock_init() {
7+
if (_crocket_is_winsock_initialized) return true;
8+
59
struct WSAData wsa_data;
610

7-
if (WSAStartup(WINSOCK_VERSION, &wsa_data) != CROCKET_ERROR_NONE) {
8-
_crocket_update_error_context(
9-
WSAGetLastError(),
10-
"WSAStartup() failed"
11-
);
11+
if (WSAStartup(WINSOCK_VERSION, &wsa_data) != CROCKET_SUCCESS) {
12+
// todo: collect error info
1213

1314
return false;
1415
}
1516

17+
_crocket_is_winsock_initialized = true;
18+
1619
return true;
1720
}
1821

19-
CROCKET_API bool crocket_winsock_cleanup() {
20-
if (WSACleanup() != CROCKET_ERROR_NONE) {
21-
int error_code = WSAGetLastError();
22-
23-
if (error_code == WSANOTINITIALISED) {
24-
_crocket_update_error_context(
25-
CROCKET_ERROR_WINSOCK_NOT_INITIALIZED,
26-
"Operation not permitted, Winsock not initialized"
27-
);
28-
}
29-
else {
30-
_crocket_update_error_context(
31-
error_code,
32-
"Failed to cleanup Winsock"
33-
);
34-
}
22+
CROCKET_API bool winsock_cleanup() {
23+
if (!_crocket_is_winsock_initialized) {
24+
// todo: collect error info
25+
26+
return false;
27+
}
28+
29+
if (WSACleanup() != CROCKET_SUCCESS) {
30+
// todo: collect error info
3531

3632
return false;
3733
}

src/crocket_base.h

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#ifndef __CROCKET_BASE_H
22
#define __CROCKET_BASE_H
33

4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <stdint.h>
47
#include <stdbool.h>
58

69
#include "platform.h"
7-
#include "error.h"
810

911
#if defined(CROCKET_WINDOWS)
1012
#pragma comment(lib, "ws2_32.lib")
@@ -15,48 +17,47 @@
1517

1618
#include <winsock2.h>
1719
#include <ws2tcpip.h>
20+
#include <windows.h>
1821

1922
#ifndef CROCKET_API
20-
#define CROCKET_API WINAPI
23+
#define CROCKET_API WSAAPI
2124
#endif
2225

23-
#ifndef CROCKET_SOCKET_ERROR
24-
#define CROCKET_SOCKET_ERROR SOCKET_ERROR
25-
#endif
26-
27-
#ifndef CROCKET_INVALID_SOCKET
28-
#define CROCKET_INVALID_SOCKET INVALID_SOCKET
29-
#endif
30-
31-
#ifndef _CROCKET_SOCKADDR_IN_ADDRESS
32-
#define _CROCKET_SOCKADDR_IN_ADDRESS(_sockaddr_in) (_sockaddr_in.sin_addr.S_un.S_addr)
33-
#endif
26+
typedef SOCKET socket_handle_t;
3427
#elif defined(CROCKET_LINUX)
28+
#include <sys/types.h>
29+
#include <sys/socket.h>
30+
#include <netinet/in.h>
31+
#include <arpa/inet.h>
32+
#include <unistd.h>
3533
#include <errno.h>
3634

3735
#ifndef CROCKET_API
3836
#define CROCKET_API
3937
#endif
4038

41-
// linux does not have a default socket type so we rollin' our own
42-
typedef unsigned long long SOCKET;
39+
typedef int socket_handle_t;
40+
#endif
4341

44-
#ifndef CROCKET_SOCKET_ERROR
45-
#define CROCKET_SOCKET_ERROR (-1)
46-
#endif
42+
#ifndef CROCKET_SUCCESS
43+
#define CROCKET_SUCCESS (0)
44+
#endif
4745

48-
#ifndef CROCKET_INVALID_SOCKET
49-
#define CROCKET_INVALID_SOCKET (CROCKET_SOCKET)(~0)
50-
#endif
46+
#ifndef CROCKET_NO_FLAGS
47+
#define CROCKET_NO_FLAGS (0)
48+
#endif
5149

52-
#ifndef _CROCKET_SOCKADDR_IN_ADDRESS
53-
#define _CROCKET_SOCKADDR_IN_ADDRESS(_sockaddr_in) (_sockaddr_in.sin_addr.s_addr)
54-
#endif
50+
#ifdef __cplusplus
51+
extern "C" {
52+
#endif
53+
54+
#if defined(CROCKET_WINDOWS)
55+
CROCKET_API bool winsock_init();
56+
CROCKET_API bool winsock_cleanup();
5557
#endif
5658

57-
#ifdef CROCKET_WINDOWS
58-
CROCKET_API bool crocket_winsock_init();
59-
CROCKET_API bool crocket_winsock_cleanup();
59+
#ifdef __cplusplus
60+
}
6061
#endif
6162

6263
#endif

src/error.c

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

src/error.h

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

src/platform.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#ifndef __CROCKET_PLATFORM_H
2-
#if defined(_WIN32) || defined(_WIN64)
3-
#ifndef CROCKET_WINDOWS
4-
#define CROCKET_WINDOWS
5-
#endif
6-
#elif defined(__linux__)
7-
#ifndef CROCKET_LINUX
8-
#define CROCKET_LINUX
9-
#endif
2+
#define __CROCKET_PLATFORM_H
3+
4+
#ifdef _WIN32
5+
#ifndef CROCKET_WINDOWS
6+
#define CROCKET_WINDOWS
107
#endif
11-
#endif
8+
#elif defined(__linux__)
9+
#ifndef CROCKET_LINUX
10+
#define CROCKET_LINUX
11+
#endif
12+
#endif
13+
14+
#endif

0 commit comments

Comments
 (0)