Skip to content

Commit

Permalink
Use Eio >= 0.12 and Cohttp_eio >= 6.0.0~beta1
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Nov 4, 2023
1 parent 43e8a5d commit d28c905
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 222 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ RUN apt-get update && apt-get install -y \
USER opam
WORKDIR /home/opam/yomer
RUN opam-2.1 update && \
opam-2.1 pin cohttp "https://github.com/mirage/ocaml-cohttp.git#v6.0.0_beta1" && \
opam-2.1 pin cohttp-eio "https://github.com/mirage/ocaml-cohttp.git#v6.0.0_beta1" && \
opam-2.1 pin websocket "https://github.com/ushitora-anqou/ocaml-websocket.git#eio" && \
opam-2.1 pin websocket-eio "https://github.com/ushitora-anqou/ocaml-websocket.git#eio" && \
opam-2.1 pin sodium "https://github.com/ushitora-anqou/ocaml-sodium.git#yomer"

COPY --chown=opam yomer.opam .
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

```
opam switch create . --no-install
opam pin cohttp https://github.com/ushitora-anqou/ocaml-cohttp.git#yomer
opam pin cohttp-eio https://github.com/ushitora-anqou/ocaml-cohttp.git#yomer
opam pin websocket https://github.com/ushitora-anqou/ocaml-websocket.git#eio
opam pin websocket-eio https://github.com/ushitora-anqou/ocaml-websocket.git#eio
#opam pin websocket-eio https://github.com/ushitora-anqou/ocaml-websocket.git#eio
opam pin sodium https://github.com/ushitora-anqou/ocaml-sodium.git#yomer
opam install . --deps-only
```
17 changes: 9 additions & 8 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@
alcotest
ctypes
ctypes-foreign
(cohttp
(>= 6.0.0~alpha2))
(cohttp-eio
(>= 6.0.0~alpha2))
(eio_main
(<= 0.11))
(>= 0.12))
jingoo
mirage-clock-unix
mirage-crypto-rng-eio
Expand All @@ -39,11 +35,16 @@
ppx_yojson_conv
tls-eio
uuseg
;websocket
;websocket-eio
x509
yaml
yojson)
yojson
;
; vvv pinned packages vvv
;
;websocket
;cohttp
;cohttp-eio
)
(tags (discord)))

; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project
10 changes: 7 additions & 3 deletions lib_discord/agent.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type leave_channel = { guild_id : string }

type play_voice = {
guild_id : string;
src : [ `FrameSource of Eio.Flow.source ];
src : [ `FrameSource of Eio.Flow.source_ty Eio.Resource.t ];
}

type get_voice_states = { guild_id : string; user_id : string }
Expand Down Expand Up @@ -135,10 +135,14 @@ let play_voice process_mgr ~sw ~guild_id ~src (agent : t) =
let _p = spawn_ffmpeg process_mgr ~sw ~stdin:src ~stdout:sink' in
Eio.Flow.close sink';
Actaa.Gen_server.cast agent
(`PlayVoice { guild_id; src = `FrameSource (src' :> Eio.Flow.source) })
(`PlayVoice
{
guild_id;
src = `FrameSource (src' :> Eio.Flow.source_ty Eio.Resource.t);
})
in
match src with
| `Pipe (src : Eio.Flow.source) -> play src
| `Pipe (src : Eio.Flow.source_ty Eio.Resource.t) -> play src
| `Ytdl url ->
let src, sink = Eio.Process.pipe ~sw process_mgr in
let _p1 = spawn_youtubedl process_mgr ~sw ~stdout:sink url in
Expand Down
65 changes: 19 additions & 46 deletions lib_discord/httpx.ml
Original file line number Diff line number Diff line change
@@ -1,60 +1,33 @@
open Util

let authenticator =
(* FIXME *)
let null_auth ?ip:_ ~host:_ _ = Ok None in
null_auth
let null_auth ?ip:_ ~host:_ _ =
Ok None (* Warning: use a real authenticator in your code! *)

let request_https socket host =
let host =
Result.(host |> Domain_name.of_string >>= Domain_name.host |> to_option)
in
let client =
let open Tls.Config in
client ~version:(`TLS_1_0, `TLS_1_3) ~authenticator
~ciphers:Ciphers.supported ()
in
Tls_eio.client_of_flow client ?host socket
let https ~authenticator =
let tls_config = Tls.Config.client ~authenticator () in
fun uri raw ->
let host =
Uri.host uri
|> Option.map (fun x -> Domain_name.(host_exn (of_string_exn x)))
in
Tls_eio.client_of_flow ?host tls_config raw

let request ?headers ?body ~meth env ~sw url =
let url = Uri.of_string url in
let host = Uri.host url |> Option.get in
let port = Uri.port url in
let service = Uri.scheme url |> Option.get in
let path = Uri.path_and_query url in
let request ?headers ?body ~meth env ~sw (url : string) =
let headers = headers |> Option.map Cohttp.Header.of_list in
let body =
body |> Option.map (function `Fixed src -> Cohttp_eio.Body.Fixed src)
body |> Option.map (function `Fixed src -> Cohttp_eio.Body.of_string src)
in
let addr =
match Eio.Net.getaddrinfo_stream (Eio.Stdenv.net env) host ~service with
| [] -> failwith "getaddrinfo failed"
| addr :: _ -> (
match (addr, port) with
| `Tcp (ip, _), Some port -> `Tcp (ip, port)
| _ -> addr)
in
let net = Eio.Stdenv.net env in
let socket = Eio.Net.connect ~sw net addr in
let conn : Eio.Flow.two_way =
match service with
| "https" -> (request_https socket host :> Eio.Flow.two_way)
| "http" -> (socket :> Eio.Flow.two_way)
| _ -> failwith "unsupported scheme"
let client =
Cohttp_eio.Client.make
~https:(Some (https ~authenticator:null_auth))
(Eio.Stdenv.net env)
in
Cohttp_eio.Client.call ~meth env ~host ?port ?headers ?body ~conn path
Cohttp_eio.Client.call ~sw ?headers ?body client meth (Uri.of_string url)

let get = request ~meth:`GET
let post = request ~meth:`POST
let put = request ~meth:`PUT
let delete = request ~meth:`DELETE

let drain_resp_body resp =
let body = ref [] in
match
Cohttp_eio.Client.read_chunked resp (function
| Chunk { data; _ } -> body := data :: !body
| Last_chunk _ -> ())
with
| None -> Cohttp_eio.Client.read_fixed resp
| Some _ -> String.concat "" (List.rev !body)
let drain_resp_body (_, body) =
Eio.Buf_read.(parse_exn take_all) body ~max_size:max_int
2 changes: 1 addition & 1 deletion lib_discord/voice_gateway.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type voice_server = { token : string; endpoint : string }
type cast_msg =
[ `VoiceState of voice_state
| `VoiceServer of voice_server
| `FrameSource of Eio.Flow.source
| `FrameSource of Eio.Flow.source_ty Eio.Resource.t
| `Speaking of int (* ssrc *) * bool (* speaking *)
| `Stop ]

Expand Down
11 changes: 7 additions & 4 deletions lib_discord/voice_udp_stream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ type connection_param = {
type init_arg = { ip : string; port : int; ssrc : int; vgw : vgw }
type call_msg = [ `DiscoverIP ]
type call_reply = [ `DiscoverIP of string (* ip *) * int (* port *) ]
type cast_msg = [ `SecretKey of int list | `FrameSource of Eio.Flow.source ]

type cast_msg =
[ `SecretKey of int list | `FrameSource of Eio.Flow.source_ty Eio.Resource.t ]

type basic_msg = (call_msg, call_reply, cast_msg) Actaa.Gen_server.basic_msg
type msg = [ basic_msg | `Timeout of string ]

type state = {
socket : Eio.Net.datagram_socket;
socket : Eio_unix.Net.datagram_socket_ty Eio.Resource.t;
dst : Eio.Net.Sockaddr.datagram;
ssrc : int;
secret_key : Sodium.secret Sodium.Secret_box.key option;
vgw : vgw;
seq_num : int;
timestamp : int;
opus_encoder : Opus.Encoder.t;
queued_sources : Eio.Flow.source Fqueue.t;
speaking_source : Eio.Flow.source option;
queued_sources : Eio.Flow.source_ty Eio.Resource.t Fqueue.t;
speaking_source : Eio.Flow.source_ty Eio.Resource.t option;
}
[@@deriving make]

Expand Down
2 changes: 1 addition & 1 deletion lib_discord/voice_udp_stream.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ val create : unit -> t
val connect :
Eio_unix.Stdenv.base -> Eio.Switch.t -> t -> connection_param -> unit

val send_frame_source : t -> Eio.Flow.source -> unit
val send_frame_source : t -> Eio.Flow.source_ty Eio.Resource.t -> unit
val attach_secret_key : t -> int list -> unit
val close : t -> unit
val discover_ip : t -> string * int
Loading

0 comments on commit d28c905

Please sign in to comment.