11use crate :: protocol:: { EventMessage , ProtocolEvent } ;
2+ use crate :: config:: BotConfig ;
3+ use super :: { PacketContext , ServiceContext , SocketContext } ;
24use std:: { any:: TypeId , sync:: Arc } ;
35use tokio:: sync:: broadcast;
46
57pub struct EventContext {
68 sender : broadcast:: Sender < EventMessage > ,
9+ service : Arc < ServiceContext > ,
10+ packet : Arc < PacketContext > ,
11+ socket : Arc < SocketContext > ,
12+ config : Arc < BotConfig > ,
713}
814
915impl EventContext {
10- pub fn new ( ) -> Arc < Self > {
16+ pub fn new (
17+ service : Arc < ServiceContext > ,
18+ packet : Arc < PacketContext > ,
19+ socket : Arc < SocketContext > ,
20+ config : Arc < BotConfig > ,
21+ ) -> Arc < Self > {
1122 let ( sender, _) = broadcast:: channel ( 1024 ) ;
12- Arc :: new ( Self { sender } )
23+ Arc :: new ( Self {
24+ sender,
25+ service,
26+ packet,
27+ socket,
28+ config,
29+ } )
1330 }
1431
1532 pub fn post_event ( & self , event : EventMessage ) {
@@ -31,6 +48,49 @@ impl EventContext {
3148 _phantom : std:: marker:: PhantomData ,
3249 }
3350 }
51+
52+ /// Send a protocol event as a packet through the network and wait for response
53+ pub async fn send_event < T > (
54+ self : & Arc < Self > ,
55+ context : Arc < crate :: context:: BotContext > ,
56+ event : T ,
57+ ) -> Result < ( ) , crate :: Error >
58+ where
59+ T : ProtocolEvent ,
60+ {
61+ use crate :: internal:: context:: packet:: ServiceAttribute ;
62+
63+ let event_msg = EventMessage :: new ( event) ;
64+ let bytes = self . service . resolve_outgoing ( event_msg. clone ( ) , context. clone ( ) ) . await ?;
65+
66+ let event_type = event_msg. type_id ( ) ;
67+ let mappings = crate :: internal:: services:: registry ( )
68+ . get_event_mappings ( event_type)
69+ . ok_or_else ( || crate :: Error :: ServiceNotFound ( format ! ( "event type {:?}" , event_type) ) ) ?;
70+
71+ let service = mappings
72+ . iter ( )
73+ . find ( |m| ( self . config . protocol as u8 ) & m. protocol != 0 )
74+ . ok_or_else ( || crate :: Error :: ServiceNotFound ( format ! ( "No service for protocol {:?}" , self . config. protocol) ) ) ?;
75+
76+ let metadata = service. service . metadata ( ) ;
77+
78+ let attributes = Some ( ServiceAttribute :: new ( )
79+ . with_request_type ( metadata. request_type )
80+ . with_encrypt_type ( metadata. encrypt_type ) ) ;
81+
82+ let response = self . packet . send_packet (
83+ metadata. command . to_string ( ) ,
84+ bytes,
85+ self . socket . clone ( ) ,
86+ attributes,
87+ ) . await ?;
88+
89+ let response_event = self . service . resolve_incoming ( & response, context) . await ?;
90+ self . post_event ( response_event) ;
91+
92+ Ok ( ( ) )
93+ }
3494}
3595
3696pub struct TypedEventReceiver < T > {
@@ -63,12 +123,6 @@ impl<T: 'static> TypedEventReceiver<T> {
63123 }
64124}
65125
66- impl Default for EventContext {
67- fn default ( ) -> Self {
68- let ( sender, _) = broadcast:: channel ( 1024 ) ;
69- Self { sender }
70- }
71- }
72126
73127impl < T > Clone for TypedEventReceiver < T > {
74128 fn clone ( & self ) -> Self {
0 commit comments