tdlib/
tdjson.rs

1// Copyright 2021 - developers of the `tdlib-rs` project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8use std::ffi::{CStr, CString};
9use std::os::raw::{c_char, c_double, c_int};
10
11#[link(name = "tdjson")]
12extern "C" {
13    fn td_create_client_id() -> c_int;
14    fn td_send(client_id: c_int, request: *const c_char);
15    fn td_receive(timeout: c_double) -> *const c_char;
16}
17
18pub(crate) fn create_client() -> i32 {
19    unsafe { td_create_client_id() }
20}
21
22pub(crate) fn send(client_id: i32, request: String) {
23    let cstring = CString::new(request).unwrap();
24    unsafe { td_send(client_id, cstring.as_ptr()) }
25}
26
27pub(crate) fn receive(timeout: f64) -> Option<String> {
28    unsafe {
29        td_receive(timeout)
30            .as_ref()
31            .map(|response| CStr::from_ptr(response).to_string_lossy().into_owned())
32    }
33}