Connect example tweaks #1

Merged
decentral1se merged 2 commits from cblgh/iroh-go:connect-example-tweaks into main 2026-06-23 18:03:43 +00:00
Collaborator

This PR fixes two things in the connect example:

  1. it allows the listener to receive more than one connection
  2. it handles timeouts without panicing

I'm so excited about working with what you've enabled here!!!

This PR fixes two things in the connect example: 1. it allows the listener to receive more than one connection 2. it handles timeouts without panicing I'm so excited about working with what you've enabled here!!!
cblgh added 2 commits 2026-06-23 18:00:55 +00:00
decentral1se merged commit 322b0a2103 into main 2026-06-23 18:03:43 +00:00
Author
Collaborator

i was also working on a complication that makes it so that the listener can also type BACK. and it works! but only for the first volley of each participant (A: one message -> <-one message :B)

not sure why! it's way messier, here's the current diff:

edit: solved it :) it was select (it blocks until one of its cases is matched) -> https://git.coopcloud.tech/decentral1se/iroh-go/pulls/2/files

diff --git a/examples/connect/connect.go b/examples/connect/connect.go
index 662bbad..4210479 100644
--- a/examples/connect/connect.go
+++ b/examples/connect/connect.go
@@ -47,20 +47,34 @@ func send(e *iroh.Endpoint, id string) {

        stream, err := conn.OpenBi()
        check(err)
-
        fmt.Println("connected")
+       recv := stream.Recv()
+       done := make(chan struct{})
+       go handleReading(recv, done)
+       go handleInput(stream, done)
+}

+func handleInput (stream *iroh.BiStream, done chan struct{}) {
+       fmt.Println("input handling")
        scanner := bufio.NewScanner(os.Stdin)
        for scanner.Scan() {
                if line := scanner.Text(); line != "" {
-                       err = stream.Send().WriteAll([]byte(line))
+                       err := stream.Send().WriteAll([]byte(line))
                        check(err)
                }
+
+               // exit if channel done is closed
+               select {
+               case <-done:
+                       fmt.Println("input exit 1")
+                       return
+               }
        }

        if err := scanner.Err(); err != nil {
                panic(err)
        }
+       fmt.Println("input exit 2")
 }

 func listen(e *iroh.Endpoint) {
@@ -70,19 +84,12 @@ func listen(e *iroh.Endpoint) {
        }
 }

-func handleIncoming (incoming *iroh.Incoming) {
-       accepting, err := incoming.Accept()
-       check(err)
-
-       conn, err := accepting.Connect()
-       check(err)
-
-       stream, err := conn.AcceptBi()
-       check(err)
-
-       recv := stream.Recv()
+func handleReading(recv *iroh.RecvStream, done chan struct{}) {
+       fmt.Println("reading attached")
        for {
+               fmt.Println("prep read new")
                frame, err := recv.Read(frameSize)
+               fmt.Println("read new")
                // handle timeout
                if err != nil {
                        irohErr := err.(*iroh.IrohError)
@@ -96,6 +103,24 @@ func handleIncoming (incoming *iroh.Incoming) {
                }
                fmt.Println(string(frame))
        }
+       close(done)
+       fmt.Println("reading exited")
+}
+
+func handleIncoming (incoming *iroh.Incoming) {
+       accepting, err := incoming.Accept()
+       check(err)
+
+       conn, err := accepting.Connect()
+       check(err)
+
+       stream, err := conn.AcceptBi()
+       check(err)
+
+       recv := stream.Recv()
+       done := make(chan struct{})
+       go handleInput(stream, done)
+       go handleReading(recv, done)
 }

 func main() {
i was also working on a complication that makes it so that the listener can also type BACK. and it works! but only for the first volley of each participant (A: one message -> <-one message :B) ~~not sure why! it's way messier, here's the current diff:~~ edit: solved it :) it was select (it blocks until one of its cases is matched) -> https://git.coopcloud.tech/decentral1se/iroh-go/pulls/2/files ```diff diff --git a/examples/connect/connect.go b/examples/connect/connect.go index 662bbad..4210479 100644 --- a/examples/connect/connect.go +++ b/examples/connect/connect.go @@ -47,20 +47,34 @@ func send(e *iroh.Endpoint, id string) { stream, err := conn.OpenBi() check(err) - fmt.Println("connected") + recv := stream.Recv() + done := make(chan struct{}) + go handleReading(recv, done) + go handleInput(stream, done) +} +func handleInput (stream *iroh.BiStream, done chan struct{}) { + fmt.Println("input handling") scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { if line := scanner.Text(); line != "" { - err = stream.Send().WriteAll([]byte(line)) + err := stream.Send().WriteAll([]byte(line)) check(err) } + + // exit if channel done is closed + select { + case <-done: + fmt.Println("input exit 1") + return + } } if err := scanner.Err(); err != nil { panic(err) } + fmt.Println("input exit 2") } func listen(e *iroh.Endpoint) { @@ -70,19 +84,12 @@ func listen(e *iroh.Endpoint) { } } -func handleIncoming (incoming *iroh.Incoming) { - accepting, err := incoming.Accept() - check(err) - - conn, err := accepting.Connect() - check(err) - - stream, err := conn.AcceptBi() - check(err) - - recv := stream.Recv() +func handleReading(recv *iroh.RecvStream, done chan struct{}) { + fmt.Println("reading attached") for { + fmt.Println("prep read new") frame, err := recv.Read(frameSize) + fmt.Println("read new") // handle timeout if err != nil { irohErr := err.(*iroh.IrohError) @@ -96,6 +103,24 @@ func handleIncoming (incoming *iroh.Incoming) { } fmt.Println(string(frame)) } + close(done) + fmt.Println("reading exited") +} + +func handleIncoming (incoming *iroh.Incoming) { + accepting, err := incoming.Accept() + check(err) + + conn, err := accepting.Connect() + check(err) + + stream, err := conn.AcceptBi() + check(err) + + recv := stream.Recv() + done := make(chan struct{}) + go handleInput(stream, done) + go handleReading(recv, done) } func main() { ```
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: decentral1se/iroh-go#1