Skip to content

Instantly share code, notes, and snippets.

@Catizard
Last active May 7, 2025 04:54
Show Gist options
  • Select an option

  • Save Catizard/dfbb5a6235943152efb8d5474d40082f to your computer and use it in GitHub Desktop.

Select an option

Save Catizard/dfbb5a6235943152efb8d5474d40082f to your computer and use it in GitHub Desktop.
Try integrating beatoraja
package main
import (
"fmt"
"math/rand/v2"
"time"
"github.com/Microsoft/go-winio"
"github.com/charmbracelet/log"
)
type CloseWriter interface {
CloseWrite() error
}
// Tried:
// 1. `syscall`:
// 3.1: Only `CreateNamedPipe`: all pipe instances are busy
// 3.2: `CreateNamedPipe` and try `ConnectNamedPipe`: all pipe instances are busy
// 3.3: Add `CreateFile` base on 3.2: this program panic with all pipe instances are busy
//
// Success:
// 1. `npipe`/`winio`:
// 1.1: Write through server
// 2.1: Create another client by calling `DialPipe`
//
// I wrote another java program to simpily mimic beatoraja:
//
// ```java
// BufferedReader reader = new BufferedReader(new FileReader("\\\\.\\pipe\\x"));
// reader.readline()
// ```
func main() {
pipeName := `\\.\pipe\x`
l, err := winio.ListenPipe(pipeName, &winio.PipeConfig{
MessageMode: true, // Use message mode so that CloseWrite() is supported
InputBufferSize: 65536, // Use 64KB buffers to improve performance
OutputBufferSize: 65536,
})
if err != nil {
panic(err)
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
log.Errorf("accept: %s\n", err)
continue
}
// Write through connection works
r := rand.IntN(2)
if r == 0 {
fmt.Fprintln(conn, "hello x")
}
// It seems like we must close the connection to make
// the other side get notified
if err := conn.Close(); err != nil {
log.Errorf("close: %s\n", err)
continue
}
}
}()
// Receive nothing from java side
// conn, err := winio.DialPipe(pipeName, nil)
// if err != nil {
// panic(err)
// }
// for {
// r := rand.IntN(2)
// if r == 0 {
// fmt.Fprintln(conn, "hello x")
// }
// time.Sleep(1 * time.Second)
// }
time.Sleep(100 * time.Hour)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment