-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module network | ||
|
||
import eventbus | ||
import superernd.cn | ||
|
||
pub const gmanager = create_manager() | ||
|
||
fn create_manager() &NetworkManager { | ||
mut nm := NetworkManager{} | ||
return &nm | ||
} | ||
|
||
@[heap] | ||
pub struct NetworkManager { | ||
pub mut: | ||
current_active_connection cn.Client = cn.Client{} | ||
eb eventbus.EventBus[string] = eventbus.new[string]() | ||
} | ||
|
||
pub fn (mut nm NetworkManager) connect_handler(receiver voidptr, args voidptr, sender voidptr) { | ||
if nm.eb.subscriber.is_subscribed("connect") { | ||
nm.eb.publish("connect", args, sender) | ||
} | ||
} | ||
|
||
pub fn (mut nm NetworkManager) payload_handler(receiver voidptr, args voidptr, sender voidptr) { | ||
if nm.eb.subscriber.is_subscribed("payload") { | ||
nm.eb.publish("payload", args, sender) | ||
} | ||
} | ||
|
||
pub fn (mut nm NetworkManager) disconnect_handler(receiver voidptr, args voidptr, sender voidptr) { | ||
if nm.eb.subscriber.is_subscribed("disconnect") { | ||
nm.eb.publish("disconnect", args, sender) | ||
} | ||
} | ||
|
||
pub fn (mut nm NetworkManager) connect(token string) ! { | ||
nm.current_active_connection = cn.Client{} | ||
|
||
// init handlers! | ||
mut eb := eventbus.new[string]() | ||
eb.subscriber.subscribe("connect", nm.connect_handler) | ||
eb.subscriber.subscribe("disconnect", nm.disconnect_handler) | ||
eb.subscriber.subscribe("payload", nm.payload_handler) | ||
|
||
nm.current_active_connection.eb = eb // maybe that will fix the issue. bruh? | ||
|
||
nm.current_active_connection.init(token)! | ||
} | ||
|
||
pub fn (mut nm NetworkManager) get_sub() &eventbus.Subscriber[string] { | ||
return nm.eb.subscriber | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters