Skip to content

Commit

Permalink
remove redundant native method/structs
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed Sep 14, 2024
1 parent 1f718c7 commit ed4715a
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 116 deletions.
12 changes: 2 additions & 10 deletions csdl/Native/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@ internal static partial class NativeMethods
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SessionEventCallback(IntPtr alertPtr);

/// <summary>
/// Creates a session with the given configuration.
/// </summary>
/// <param name="config">The configuration to apply</param>
/// <returns>A handle to the session</returns>
[DllImport(LibraryName, EntryPoint = "create_session")]
public static extern IntPtr CreateSession(in NativeStructs.SessionConfig config);

/// <summary>
/// Creates a session, optionally using a provided settings pack.
/// </summary>
/// <param name="settingsPack">A settings pack handle, set to <see cref="IntPtr.Zero"/> to initialise without customisation</param>
/// <param name="settingsPack">A settings pack handle, set to <c>null</c> to initialise without customisation</param>
/// <returns>A handle to the session</returns>
[LibraryImport(LibraryName, EntryPoint = "create_session_from_pack")]
[LibraryImport(LibraryName, EntryPoint = "create_session")]
public static unsafe partial IntPtr CreateSession(void* settingsPack);

/// <summary>
Expand Down
28 changes: 0 additions & 28 deletions csdl/Native/NativeStructs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,6 @@ namespace csdl.Native;

internal static class NativeStructs
{
/// <summary>
/// Holds configuration information relating to a session.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 8)]
public struct SessionConfig
{
[MarshalAs(UnmanagedType.LPStr)]
public string user_agent;

[MarshalAs(UnmanagedType.LPStr)]
public string fingerprint;

[MarshalAs(UnmanagedType.I1)]
public bool private_mode;

[MarshalAs(UnmanagedType.I1)]
public bool block_seeding;

[MarshalAs(UnmanagedType.I1)]
public bool force_encryption;

[MarshalAs(UnmanagedType.I1)]
public bool set_alert_flags;

public int alert_flags;
public int max_connections;
}

/// <summary>
/// Represents a file contained within a torrent.
/// </summary>
Expand Down
4 changes: 1 addition & 3 deletions native/include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ extern "C" {
#endif

// session control
CSDL_EXPORT lt::session* create_session(session_config* config);
CSDL_EXPORT lt::session* create_session_from_pack(lt::settings_pack* pack);

CSDL_EXPORT lt::session* create_session(lt::settings_pack* pack)
CSDL_EXPORT void destroy_session(lt::session* session);

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build osx-x64

expected ';' after top level declarator

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build osx-arm64

expected ';' after top level declarator

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build linux-x64

expected initializer before ‘void’

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build linux-arm64

expected initializer before ‘void’

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build android-arm

expected ';' after top level declarator

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build android-arm64

expected ';' after top level declarator

Check failure on line 21 in native/include/library.h

View workflow job for this annotation

GitHub Actions / Build android-x86_64

expected ';' after top level declarator

CSDL_EXPORT void set_event_callback(lt::session* session, cs_alert_callback callback, bool include_unmapped_events);
Expand Down
15 changes: 0 additions & 15 deletions native/include/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@
extern "C" {
#endif

CSDL_STRUCT typedef struct cs_session_config {
// see https://www.libtorrent.org/reference-Settings.html#settings_pack
char* user_agent;
char* fingerprint;

bool private_mode;
bool block_seeding;
bool encrypted_peers_only;

bool set_alert_flags;

int32_t alert_flags;
int32_t max_connections;
} session_config;

CSDL_STRUCT typedef struct cs_torrent_file_information {
lt::file_index_t index;

Expand Down
61 changes: 1 addition & 60 deletions native/src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,8 @@
#include <libtorrent/torrent_handle.hpp>

extern "C" {
// given a config, create a session
lt::session* create_session(session_config* config)
{
if (config == nullptr)
{
return create_session_from_pack(nullptr);
}

lt::settings_pack pack;

// user-agent
if (config->user_agent != nullptr)
{
auto useragent = std::string(config->user_agent);
if (!useragent.empty())
{
pack.set_str(lt::settings_pack::user_agent, useragent);
}
}

// fingerprint
if (config->fingerprint != nullptr)
{
auto fingerprint = std::string(config->fingerprint);
if (!fingerprint.empty())
{
pack.set_str(lt::settings_pack::peer_fingerprint, fingerprint);
}
}

// events
if (config->set_alert_flags)
{
pack.set_int(lt::settings_pack::alert_mask, config->alert_flags);
}

pack.set_bool(lt::settings_pack::anonymous_mode, config->private_mode);

// disable seeding
if (config->block_seeding)
{
pack.set_bool(lt::settings_pack::seeding_outgoing_connections, false);
}

// max connections
if (config->max_connections > 0)
{
pack.set_int(lt::settings_pack::connections_limit, config->max_connections);
}

// encryption
if (config->encrypted_peers_only)
{
pack.set_int(lt::settings_pack::out_enc_policy, lt::settings_pack::pe_forced);
pack.set_int(lt::settings_pack::in_enc_policy, lt::settings_pack::pe_forced);
}

return create_session_from_pack(&pack);
}

lt::session* create_session_from_pack(lt::settings_pack* pack)
lt::session* create_session(lt::settings_pack* pack)
{
lt::session_params params;

Expand Down

0 comments on commit ed4715a

Please sign in to comment.