Skip to content

Commit

Permalink
Merge pull request #59 from Cogmasters/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
lcsmuller authored Apr 18, 2022
2 parents 1e1f693 + 98ac7bb commit 071dbb4
Show file tree
Hide file tree
Showing 37 changed files with 2,455 additions and 714 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ concord-docs
*.swp
src/specs-code
include/discord-specs.h
*.dSYM
examples/*.dSYM
test/*.dSYM
75 changes: 39 additions & 36 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,40 @@ CCORDDOCS_DIR = concord-docs
GENCODECS_HDR = $(GENCODECS_DIR)/discord_codecs.h
GENCODECS_OBJ = $(GENCODECS_DIR)/discord_codecs.o

COGUTILS_OBJS = $(OBJDIR)/$(COGUTILS_DIR)/cog-utils.o \
$(OBJDIR)/$(COGUTILS_DIR)/log.o \
$(OBJDIR)/$(COGUTILS_DIR)/logconf.o \
$(OBJDIR)/$(COGUTILS_DIR)/json-build.o \
$(OBJDIR)/$(COGUTILS_DIR)/jsmn-find.o
CORE_OBJS = $(OBJDIR)/$(CORE_DIR)/work.o \
$(OBJDIR)/$(CORE_DIR)/user-agent.o \
$(OBJDIR)/$(CORE_DIR)/websockets.o \
$(OBJDIR)/$(CORE_DIR)/io_poller.o
THIRDP_OBJS = $(OBJDIR)/$(THIRDP_DIR)/sha1.o \
$(OBJDIR)/$(THIRDP_DIR)/curl-websocket.o \
$(OBJDIR)/$(THIRDP_DIR)/threadpool.o
DISCORD_OBJS = $(OBJDIR)/$(SRC_DIR)/concord-once.o \
$(OBJDIR)/$(SRC_DIR)/discord-adapter.o \
$(OBJDIR)/$(SRC_DIR)/discord-ratelimit.o \
$(OBJDIR)/$(SRC_DIR)/discord-client.o \
$(OBJDIR)/$(SRC_DIR)/discord-gateway.o \
$(OBJDIR)/$(SRC_DIR)/discord-misc.o \
$(OBJDIR)/$(SRC_DIR)/application_command.o \
$(OBJDIR)/$(SRC_DIR)/interaction.o \
$(OBJDIR)/$(SRC_DIR)/audit_log.o \
$(OBJDIR)/$(SRC_DIR)/channel.o \
$(OBJDIR)/$(SRC_DIR)/emoji.o \
$(OBJDIR)/$(SRC_DIR)/gateway.o \
$(OBJDIR)/$(SRC_DIR)/guild.o \
$(OBJDIR)/$(SRC_DIR)/guild_template.o \
$(OBJDIR)/$(SRC_DIR)/invite.o \
$(OBJDIR)/$(SRC_DIR)/user.o \
$(OBJDIR)/$(SRC_DIR)/voice.o \
$(OBJDIR)/$(SRC_DIR)/webhook.o \
COGUTILS_OBJS = $(COGUTILS_DIR)/cog-utils.o \
$(COGUTILS_DIR)/log.o \
$(COGUTILS_DIR)/logconf.o \
$(COGUTILS_DIR)/json-build.o \
$(COGUTILS_DIR)/jsmn-find.o
CORE_OBJS = $(CORE_DIR)/work.o \
$(CORE_DIR)/user-agent.o \
$(CORE_DIR)/websockets.o \
$(CORE_DIR)/io_poller.o
THIRDP_OBJS = $(THIRDP_DIR)/sha1.o \
$(THIRDP_DIR)/curl-websocket.o \
$(THIRDP_DIR)/threadpool.o \
$(THIRDP_DIR)/priority_queue.o
DISCORD_OBJS = $(SRC_DIR)/concord-once.o \
$(SRC_DIR)/discord-adapter.o \
$(SRC_DIR)/discord-adapter_ratelimit.o \
$(SRC_DIR)/discord-adapter_refcount.o \
$(SRC_DIR)/discord-client.o \
$(SRC_DIR)/discord-loop.o \
$(SRC_DIR)/discord-gateway.o \
$(SRC_DIR)/discord-timer.o \
$(SRC_DIR)/discord-misc.o \
$(SRC_DIR)/application_command.o \
$(SRC_DIR)/interaction.o \
$(SRC_DIR)/audit_log.o \
$(SRC_DIR)/channel.o \
$(SRC_DIR)/emoji.o \
$(SRC_DIR)/gateway.o \
$(SRC_DIR)/guild.o \
$(SRC_DIR)/guild_template.o \
$(SRC_DIR)/invite.o \
$(SRC_DIR)/user.o \
$(SRC_DIR)/voice.o \
$(SRC_DIR)/webhook.o \
$(XOBJ)

OBJS := $(COGUTILS_OBJS) $(CORE_OBJS) $(THIRDP_OBJS) $(DISCORD_OBJS) \
Expand All @@ -59,18 +63,18 @@ CFLAGS += -std=c99 -O0 -g -pthread -D_XOPEN_SOURCE=600 \
-I$(GENCODECS_DIR) -I$(PREFIX)/include -DLOG_USE_COLOR
WFLAGS += -Wall -Wextra -Wshadow -Wdouble-promotion -Wconversion -Wpedantic

$(OBJDIR)/$(SRC_DIR)/%.o: $(SRC_DIR)/%.c
$(SRC_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) $(WFLAGS) $(XFLAGS) -c -o $@ $<
$(OBJDIR)/%.o: %.c
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<

all: $(LIB)

voice:
@ $(MAKE) XFLAGS=-DHAS_DISCORD_VOICE XOBJ=$(OBJDIR)/$(SRC_DIR)/discord-voice.o all
@ $(MAKE) XFLAGS=-DCCORD_VOICE XOBJ=$(SRC_DIR)/discord-voice.o all

debug:
@ $(MAKE) XFLAGS="-D_CCORD_DEBUG_WEBSOCKETS -D_CCORD_DEBUG_ADAPTER" all
@ $(MAKE) XFLAGS="-DCCORD_DEBUG_WEBSOCKETS -DCCORD_DEBUG_ADAPTER" all

test: all
@ $(MAKE) -C $(TEST_DIR)
Expand Down Expand Up @@ -119,8 +123,7 @@ echo:
@ echo -e 'OBJS: $(OBJS)\n'

clean:
rm -rf $(OBJDIR)
rm -rf $(LIBDIR)
rm -rf $(GENCODECS_OBJS) $(COGUTILS_OBJS) $(CORE_OBJS) $(THIRDP_OBJS) $(DISCORD_OBJS)
@ $(MAKE) -C $(TEST_DIR) clean
@ $(MAKE) -C $(EXAMPLES_DIR) clean

Expand Down
59 changes: 51 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,24 @@ int main(void) {
```
*This is a minimalistic example, refer to [`examples/`](examples/) for a better overview.*
## Supported operating systems (minimum requirements)
* GNU/Linux 4.x
* FreeBSD 12
* NetBSD 8.1
* Windows 7 (Cygwin)
* GNU/Hurd 0.9
* Mac OS X 10.9
## Build Instructions
### On Windows
* Install **Cygwin**
* **Make sure that you installed libcurl, gcc, make, and git when you ran the Cygwin installer!**
* You will want to check the Windows tutorial [here](docs/WINDOWS.md)!
* Mingw64 and Msys2 are currently NOT supported. Please see [this](docs/MSYS2_MINGW64.md) for more information.
* Once installed, compile it normally like you would on UNIX/Linux/OS X/BSD.
* Note: you will likely need to include `-L/usr/local/lib -I/usr/local/include` on your `gcc` command, or in your `CFLAGS` variable in your Makefile for your bot.
### On Linux, BSD, and Mac OS X
Expand Down Expand Up @@ -80,10 +91,12 @@ $ pkg install curl
```

#### OS X
* Note: you will need to install Xcode, or at a minimum, the command-line tools with `xcode-select --install`.
```console
$ brew install curl (Homebrew)
$ port install curl (MacPorts)
```

### Setting up your environment

#### Clone Concord into your workspace
Expand All @@ -99,19 +112,44 @@ $ make
```

#### Special notes for non-Linux systems
You might run into trouble with the compiler and linker not finding your Curl headers. You can do something like this:

You might run into trouble with the compiler and linker not finding your Libcurl headers. You can do something like this:
```console
$ CFLAGS=-I<some_path> LDFLAGS=-L<some_path> make
```
For instance, on a FreeBSD system:
```console
$ CFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/lib make
```

On OS X using MacPorts:
```console
$ CFLAGS=-I/opt/local/include LDFLAGS=-L/opt/local/lib make
```
On OS X using a self-compiled libcurl:
```console
$ CFLAGS=-I/usr/local/include LDFLAGS=-L/usr/local/include make
```
On Windows with Cygwin, you might need to pass both arguments to use POSIX threading:
```console
$ CFLAGS="-pthread -lpthread" make
```

#### Special compilation flags

The following section outlines flags that can be attached to the Makefile if you wish to override the default compilation behavior with additional functionalities. Example:

```console
$ CFLAGS="-DCCORD_SIGINTCATCH -DCCORD_VOICE" make
```

* `-DCCORD_SIGINTCATCH`
* By default Concord will not shutdown gracefully when a SIGINT is received (i.e. <kbd>Ctrl</kbd>+<kbd>c</kbd>), enable this flag if you wish it to be handled for you.
* `-DCCORD_VOICE`
* Enable experimental Voice Connection handling.
* `-DCCORD_DEBUG_WEBSOCKETS`
* Enable verbose debugging for WebSockets communication.
* `-DCCORD_DEBUG_ADAPTER`
* Enable verbose debugging for REST communication.

### Configuring Concord

Expand Down Expand Up @@ -172,16 +210,16 @@ With <kbd>Ctrl</kbd>+<kbd>c</kbd> or with <kbd>Ctrl</kbd>+<kbd>|</kbd>
# make install
```

Included headers must be `concord/` prefixed:
```c
#include <concord/discord.h>
```

This will install the headers and libary files into $PREFIX. You can override this as such:
```console
# PREFIX=/opt/concord make install
```

Note that included headers must be `concord/` prefixed:
```c
#include <concord/discord.h>
```

### Standalone executable

#### GCC
Expand All @@ -196,7 +234,12 @@ $ gcc myBot.c -o myBot -pthread -ldiscord -lcurl
$ clang myBot.c -o myBot -pthread -ldiscord -lcurl
```

#### UNIX C compiler
#### UNIX C compilers
##### This includes the following compilers:
* IBM XL C/C++ (AIX, z/OS, possibly IBM i)
* Sun/Oracle Studio (Solaris)
* IRIX MIPSpro C++ (IRIX) -- NOTE: currently not supported
* Possibly others!
```console
$ cc myBot.c -o myBot -ldiscord -lcurl -lpthread
```
Expand Down
45 changes: 45 additions & 0 deletions cog-utils/cog-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,40 @@ cog_sleep_ms(const long tms)
return ret;
}

int
cog_sleep_us(const long tms)
{
int ret;

#if _POSIX_C_SOURCE >= 199309L
struct timespec ts;

if (tms < 0) {
errno = EINVAL;
return -1;
}

ts.tv_sec = tms / 1000000;
ts.tv_nsec = (tms % 1000000) * 1000;

do {
ret = nanosleep(&ts, &ts);
} while (ret && errno == EINTR);
#else
struct timeval timeout;
long _tms = tms;

timeout.tv_sec = _tms / 1000000L;
_tms = tms % 1000000L;
timeout.tv_usec = (int)_tms;
select(0, NULL, NULL, NULL, &timeout);

ret = 0;
#endif

return ret;
}

/* returns current timestamp in milliseconds */
uint64_t
cog_timestamp_ms(void)
Expand All @@ -226,6 +260,17 @@ cog_timestamp_ms(void)
return 0;
}

/* returns current timestamp in microseconds */
uint64_t
cog_timestamp_us(void)
{
struct PsnipClockTimespec t;
if (0 == psnip_clock_get_time(PSNIP_CLOCK_TYPE_WALL, &t)) {
return (uint64_t)t.seconds * 1000000 + (uint64_t)t.nanoseconds / 1000;
}
return 0;
}

/* this can be used for checking if a user-given string does not
* exceeds a arbitrary threshold length */
size_t
Expand Down
20 changes: 18 additions & 2 deletions cog-utils/cog-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,36 @@ size_t cog_strndup(const char src[], size_t len, char **p_dest);
size_t cog_asprintf(char **strp, const char fmt[], ...);

/**
* @brief Sleep for milliseconds amount
* @brief Sleep for amount of milliseconds
*
* @param tms milliseconds amount to sleep for
* @param tms amount of milliseconds to sleep for
* @return 0 on success, -1 on error with an `errno` set to indicate the error
*/
int cog_sleep_ms(const long tms);


/**
* @brief Sleep for amount of microseconds
*
* @param tms amount of microseconds to sleep for
* @return 0 on success, -1 on error with an `errno` set to indicate the error
*/
int cog_sleep_us(const long tms);

/**
* @brief Get the current timestamp in milliseconds
*
* @return the timestamp on success, 0 on failure
*/
uint64_t cog_timestamp_ms(void);

/**
* @brief Get the current timestamp in microseconds
*
* @return the timestamp on success, 0 on failure
*/
uint64_t cog_timestamp_us(void);

/**
* @brief Check if arbitrary string length is exceeded
*
Expand Down
Loading

0 comments on commit 071dbb4

Please sign in to comment.