diff options
| author | Mishio595 <[email protected]> | 2018-11-25 18:51:44 -0700 |
|---|---|---|
| committer | Mishio595 <[email protected]> | 2018-11-25 18:51:44 -0700 |
| commit | ad8a13b186683cb1e6c5ef405df503f58b751ffa (patch) | |
| tree | 26b87993cdabb16bf0582423e0629633e8feb6c0 /lib | |
| parent | Naming consistency (diff) | |
| download | disml-ad8a13b186683cb1e6c5ef405df503f58b751ffa.tar.xz disml-ad8a13b186683cb1e6c5ef405df503f58b751ffa.zip | |
convert to client from client_ez
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/_sharder.mli | 138 | ||||
| -rw-r--r-- | lib/sharder.ml | 41 |
2 files changed, 25 insertions, 154 deletions
diff --git a/lib/_sharder.mli b/lib/_sharder.mli deleted file mode 100644 index 0fd16d6..0000000 --- a/lib/_sharder.mli +++ /dev/null @@ -1,138 +0,0 @@ -open Async - -(** -Record type for registering event handlers -*) -type handler = { - ready: (Yojson.Basic.json -> unit) option; - resumed: (Yojson.Basic.json -> unit) option; - channel_create: (Yojson.Basic.json -> unit) option; - channel_delete: (Yojson.Basic.json -> unit) option; - channel_update: (Yojson.Basic.json -> unit) option; - channel_pins_update: (Yojson.Basic.json -> unit) option; - guild_create: (Yojson.Basic.json -> unit) option; - guild_delete: (Yojson.Basic.json -> unit) option; - guild_update: (Yojson.Basic.json -> unit) option; - guild_ban_add: (Yojson.Basic.json -> unit) option; - guild_ban_remove: (Yojson.Basic.json -> unit) option; - guild_emojis_update: (Yojson.Basic.json -> unit) option; - guild_integrations_update: (Yojson.Basic.json -> unit) option; - guild_member_add: (Yojson.Basic.json -> unit) option; - guild_member_remove: (Yojson.Basic.json -> unit) option; - guild_member_update: (Yojson.Basic.json -> unit) option; - guild_members_chunk: (Yojson.Basic.json -> unit) option; - guild_role_create: (Yojson.Basic.json -> unit) option; - guild_role_delete: (Yojson.Basic.json -> unit) option; - guild_role_update: (Yojson.Basic.json -> unit) option; - message_create: (Yojson.Basic.json -> unit) option; - message_delete: (Yojson.Basic.json -> unit) option; - message_update: (Yojson.Basic.json -> unit) option; - message_delete_bulk: (Yojson.Basic.json -> unit) option; - message_reaction_add: (Yojson.Basic.json -> unit) option; - message_reaction_remove: (Yojson.Basic.json -> unit) option; - message_reaction_remove_all: (Yojson.Basic.json -> unit) option; - presence_update: (Yojson.Basic.json -> unit) option; - typing_start: (Yojson.Basic.json -> unit) option; - user_update: (Yojson.Basic.json -> unit) option; - voice_state_update: (Yojson.Basic.json -> unit) option; - voice_server_update: (Yojson.Basic.json -> unit) option; - webhooks_update: (Yojson.Basic.json -> unit) option; -} - -(** -Represents a single Shard. Manual creation is discouraged; use Sharder.start instead -*) -module Shard : sig - type t = { - mutable hb: unit Ivar.t option; - mutable seq: int; - mutable session: string option; - mutable handler: handler; - token: string; - shard: int * int; - write: string Pipe.Writer.t; - read: string Pipe.Reader.t; - ready: unit Ivar.t; - } - - val parse : - [< `Ok of string | `Eof] -> - Yojson.Basic.json - - val push_frame : - ?payload:Yojson.Basic.json -> - t -> - Opcode.t -> - t Deferred.t - - val heartbeat : - t -> - t Deferred.t - - val dispatch : - t -> - Yojson.Basic.json -> - t Deferred.t - - val set_status : - t -> - Yojson.Basic.json -> - t Deferred.t - - val request_guild_members : - guild:int -> - ?query:string -> - ?limit:int -> - t -> - t Deferred.t - - val initialize : - t -> - Yojson.Basic.json -> - t Deferred.t - - val handle_frame : - t -> - Yojson.Basic.json -> - t Deferred.t - - val create : - url:string -> - shards:int * int -> - token:string -> - handler: handler -> - unit -> - t Deferred.t -end - -type t = { - shards: Shard.t list; -} - -val start : - ?count:int -> - handler:handler -> - string -> - t Deferred.t - -val set_status : - t -> - Yojson.Basic.json -> - Shard.t list Deferred.t - -val set_status_with : - t -> - (Shard.t -> Yojson.Basic.json) -> - Shard.t list Deferred.t - -val request_guild_members : - guild:int -> - ?query:string -> - ?limit:int -> - t -> - Shard.t list Deferred.t - -val update_handler : - t -> - handler -> - unit
\ No newline at end of file diff --git a/lib/sharder.ml b/lib/sharder.ml index ccfb047..defcfe1 100644 --- a/lib/sharder.ml +++ b/lib/sharder.ml @@ -11,17 +11,17 @@ module Shard = struct mutable session: string option; token: string; shard: int * int; - write: string Pipe.Writer.t; - read: string Pipe.Reader.t; + write: Frame.t Pipe.Writer.t; + read: Frame.t Pipe.Reader.t; ready: unit Ivar.t; } let identify_lock = Mutex.create () - let parse frame = + let parse (frame:[`Ok of Frame.t | `Eof]) = match frame with - | `Ok s -> Yojson.Basic.from_string s - | `Eof -> raise Invalid_Payload (* This needs to go into reconnect code, or stop using client_ez and handle frames manually *) + | `Ok s -> Yojson.Basic.from_string s.content (* TODO Handler non-text frames *) + | `Eof -> raise Invalid_Payload (* TODO This needs to go into reconnect code, or stop using client_ez and handle frames manually *) let push_frame ?payload ~ev shard = print_endline @@ "Pushing frame. OP: " ^ Opcode.to_string @@ ev; @@ -33,7 +33,7 @@ module Shard = struct ("d", p); ] in - Pipe.write shard.write content + Pipe.write shard.write @@ Frame.create ~content () >>| fun () -> shard @@ -156,7 +156,7 @@ module Shard = struct | HELLO -> initialize ~data:(J.member "d" f) shard | HEARTBEAT_ACK -> return shard | opcode -> - print_endline @@ "Invalid Opcode:" ^ Opcode.to_string opcode; + print_endline @@ "Invalid Opcode: " ^ Opcode.to_string opcode; return shard let create ~url ~shards ~token () = @@ -170,17 +170,26 @@ module Shard = struct | None, Some p -> p | _ -> 443 in let scheme = Option.value_exn ~message:"no scheme in uri" Uri.(scheme uri) in - let tcp_fun (r,w) = - let (read, write) = client_ez + let tcp_fun (net_to_ws, ws_to_net) = + let (app_to_ws, write) = Pipe.create () in + let (read, ws_to_app) = Pipe.create () in + let initialized = Ivar.create () in + client + ~initialized ~extra_headers - uri r w - in - let rec ev_loop shard = - Pipe.read read + ~app_to_ws + ~ws_to_app + ~net_to_ws + ~ws_to_net + uri + >>> ignore; + Ivar.read initialized >>| fun () -> + let rec ev_loop ~reader shard = + Pipe.read reader >>= fun frame -> handle_frame ~f:(parse frame) shard >>= fun shard -> - ev_loop shard + ev_loop ~reader shard in let shard = { read; @@ -193,8 +202,8 @@ module Shard = struct token = token; } in - ev_loop shard |> ignore; - return shard + ev_loop ~reader:read shard |> ignore; + shard in match Unix.getaddrinfo host (string_of_int port) [] with | [] -> failwithf "DNS resolution failed for %s" host () |