Skip to content

Commit

Permalink
DLL sample
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyKalin committed Jul 5, 2023
1 parent eab431e commit aaa853e
Show file tree
Hide file tree
Showing 10 changed files with 321 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
add [dxfeed-graal-cxx-api](https://github.com/dxFeed/dxfeed-graal-cxx-api) as a **CMake** subproject.
* [vs-project-sample](vs-project-sample/README.md) - demonstrates how to
add [dxfeed-graal-cxx-api](https://github.com/dxFeed/dxfeed-graal-cxx-api) to **Visual Studio** solution.
* [dll-sample](dll-sample/README.md) - demonstrates how to add [dxfeed-graal-cxx-api](https://github.com/dxFeed/dxfeed-graal-cxx-api) to DLL plugin.


## Usage

Expand Down
49 changes: 49 additions & 0 deletions dll-sample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright (c) 2023 Devexperts LLC.
# SPDX-License-Identifier: MPL-2.0

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

build/
cmake-build-*/
.idea/
/doc/html/
/doc/latex/

.PVS-Studio/
*.PVS-Studio.*

x64/
.vs/
third_party/dxfeed-graal-cxx-api/**
out/
15 changes: 15 additions & 0 deletions dll-sample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.25)
project(dllsample)

set(CMAKE_CXX_STANDARD 20)

add_subdirectory(plugin-api)
add_subdirectory(dxfeed-plugin)
add_subdirectory(sample)

add_dependencies(dllsample-dxfeed-plugin dllsample-plugin-api)
add_dependencies(dllsample-sample dllsample-plugin-api dllsample-dxfeed-plugin)




20 changes: 20 additions & 0 deletions dll-sample/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# dxFeed Graal CXX API DLL Sample

## Prerequisites

- Visual Studio 2019 and higher
- CMake

## Installation

- Open Visual Studio
- File -> Open -> CMake
- dll-sample/CMakeLists.txt


## Build

- Select Startup Project `dllsample-sample.exe`
- Build


18 changes: 18 additions & 0 deletions dll-sample/dxfeed-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.25)

add_library(dllsample-dxfeed-plugin SHARED plugin.cpp)

target_link_libraries(dllsample-dxfeed-plugin dllsample-plugin-api)

include(FetchContent)

FetchContent_Declare(dxFeedGraalCxxApi GIT_REPOSITORY "https://github.com/dxFeed/dxfeed-graal-cxx-api.git" GIT_TAG v0.0.1-alpha)
FetchContent_MakeAvailable(dxFeedGraalCxxApi)

target_link_libraries(dllsample-dxfeed-plugin dxfcxx)
target_compile_definitions(dllsample-dxfeed-plugin PRIVATE DXFCPP_USE_DLLS DLLSAMPLE_EXPORTS)

add_custom_command(TARGET dllsample-dxfeed-plugin POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:dxfcxx::graal>
$<TARGET_FILE:dxfcxx>
$<TARGET_FILE_DIR:dllsample-dxfeed-plugin>)
70 changes: 70 additions & 0 deletions dll-sample/dxfeed-plugin/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <plugin-api.h>

#include <dxfeed_graal_cpp_api/api.hpp>

#include <memory>

using namespace dxfcpp;

struct Plugin {
std::shared_ptr<DXEndpoint> endpoint = DXEndpoint::create();
std::shared_ptr<DXFeedSubscription> subscription = endpoint->getFeed()->createSubscription(
{Quote::TYPE, Trade::TYPE});
};

Plugin plugin{};

extern "C" {

DLLSAMPLE_API void dsp_init() {

}

DLLSAMPLE_API void dsp_connect(const char *address) {
plugin.endpoint->connect(address);
}

DLLSAMPLE_API void dsp_subscribe(const char *symbol, dsp_events_listener events_listener, void *user_data) {
plugin.subscription->addEventListener([user_data, events_listener](const auto &events) {
auto size = events.size();

if (size == 0) {
return;
}

auto *eventsToListener = new dsp_event_t *[size];

std::size_t index = 0;

for (const auto &e: events) {
if (const auto &q = e->template sharedAs<Quote>(); q) {
eventsToListener[index++] = dxfcpp::bit_cast<dsp_event_t *>(
new dsp_quote_t{{DSP_ET_QUOTE}, q->getBidPrice(), q->getBidSize(), q->getAskPrice(),
q->getAskSize()});
} else if (const auto &tr = e->template sharedAs<Trade>(); tr) {
eventsToListener[index++] = dxfcpp::bit_cast<dsp_event_t *>(
new dsp_trade_t{{DSP_ET_TRADE}, tr->getPrice(), tr->getSize()});
}
};

events_listener(eventsToListener, index, user_data);

for (std::size_t i = 0; i < index; i++) {
if (eventsToListener[i]->type == DSP_ET_QUOTE) {
delete dxfcpp::bit_cast<dsp_quote_t *>(eventsToListener[i]);
} else if (eventsToListener[i]->type == DSP_ET_TRADE) {
delete dxfcpp::bit_cast<dsp_trade_t *>(eventsToListener[i]);
}
}

delete[] eventsToListener;
});

plugin.subscription->addSymbols(symbol);
}

DLLSAMPLE_API void dsp_deinit() {

}

}
5 changes: 5 additions & 0 deletions dll-sample/plugin-api/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.25)

add_library(dllsample-plugin-api INTERFACE)

target_sources(dllsample-plugin-api PUBLIC FILE_SET HEADERS BASE_DIRS . FILES plugin-api.h)
74 changes: 74 additions & 0 deletions dll-sample/plugin-api/plugin-api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

# ifdef DLLSAMPLE_EXPORTS
# define DLLSAMPLE_API __declspec(dllexport)
# elif DLLSAMPLE_IMPORTS
# define DLLSAMPLE_API __declspec(dllimport)
# elif __cplusplus
# define DLLSAMPLE_API extern "C"
# else
# define DLLSAMPLE_API
# endif

#ifdef __cplusplus
# include <cstddef>
# include <cstdint>

extern "C" {
#else

# include <stddef.h>
# include <stdint.h>

#endif

typedef enum dsp_event_type_t {
DSP_ET_QUOTE,
DSP_ET_TRADE,
} dsp_event_type_t;

#pragma pack(push, 1)

typedef struct dsp_event_t {
dsp_event_type_t type;
} dsp_event_t;

typedef struct dsp_quote_t {
dsp_event_t event;

double bid_price;
double bid_size;
double ask_price;
double ask_size;
} dsp_quote_t;

typedef struct dsp_trade_t {
dsp_event_t event;

double price;
double size;
} dsp_trade_t;

#pragma pack(pop)

typedef void (*dsp_events_listener)(dsp_event_t **events, size_t size, void *user_data);

typedef void (*dsp_init_t)();

DLLSAMPLE_API void dsp_init();

typedef void (*dsp_connect_t)(const char *);

DLLSAMPLE_API void dsp_connect(const char *address);

typedef void (*dsp_subscribe_t)(const char *, dsp_events_listener, void *);

DLLSAMPLE_API void dsp_subscribe(const char *symbol, dsp_events_listener events_listener, void *user_data);

typedef void (*dsp_deinit_t)();

DLLSAMPLE_API void dsp_deinit();

#ifdef __cplusplus
}
#endif
15 changes: 15 additions & 0 deletions dll-sample/sample/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.25)

project(dllsample-sample)

set(CMAKE_C_STANDARD 11)

add_executable(dllsample-sample main.c)

target_link_libraries(dllsample-sample dllsample-plugin-api)

add_custom_command(TARGET dllsample-sample POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:dllsample-dxfeed-plugin>
$<TARGET_FILE:dxfcxx::graal>
$<TARGET_FILE:dxfcxx>
$<TARGET_FILE_DIR:dllsample-sample>)
53 changes: 53 additions & 0 deletions dll-sample/sample/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <plugin-api.h>

#define UNICODE
#define WIN32_LEAN_AND_MEAN

#include "Windows.h"
#include <stdio.h>

void listener(dsp_event_t **events, size_t size, void *user_data) {
if (size == 0 || events == NULL) {
return;
}

for (size_t i = 0; i < size; i++) {
if (events[i]->type == DSP_ET_QUOTE) {
dsp_quote_t *q = (dsp_quote_t *) (events[i]);

printf("Quote{bid_price = %.15g, bid_size = %.15g, ask_price = %.15g, ask_size = %.15g}\n", q->bid_price,
q->bid_size, q->ask_price, q->ask_size);
} else if (events[i]->type == DSP_ET_TRADE) {
dsp_trade_t *tr = (dsp_trade_t *) (events[i]);

printf("Trade{price = %.15g, size = %.15g}\n", tr->price, tr->size);
}
}
}

int main() {
HMODULE handle = LoadLibrary(L"dllsample-dxfeed-plugin.dll");

if (handle == NULL) {
return 1;
}

dsp_init_t dsp_init = (dsp_init_t) (GetProcAddress(handle, "dsp_init"));
dsp_connect_t dsp_connect = (dsp_connect_t) (GetProcAddress(handle, "dsp_connect"));
dsp_subscribe_t dsp_subscribe = (dsp_subscribe_t) (GetProcAddress(handle, "dsp_subscribe"));
dsp_deinit_t dsp_deinit = (dsp_deinit_t) (GetProcAddress(handle, "dsp_deinit"));

if (dsp_init == NULL || dsp_connect == NULL || dsp_subscribe == NULL || dsp_deinit == NULL) {
return 2;
}

dsp_connect("demo.dxfeed.com:7300");
dsp_subscribe("AAPL", listener, NULL);

Sleep(10000);

FreeLibrary(handle);

return 0;

}

0 comments on commit aaa853e

Please sign in to comment.