From e8d51fed2fa8c480d2cd9149309291dba1e1bc12 Mon Sep 17 00:00:00 2001 From: onur1 Date: Sat, 9 Nov 2024 06:31:28 +0100 Subject: Update README --- README.md | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 490d26a..7ca24e2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,96 @@ + # binproto -binproto implements low-level support for binary-based two-way communication protocols. You can use it to create your own low-latency binary protocols for things like game networking or file transfer. +`binproto` is a lightweight Go library designed to facilitate low-level binary protocol communication. It enables efficient, multiplexed messaging, making it well-suited for network applications requiring reliable and fast binary data exchange. + +## Features + +- **Multiplexed Channels**: Supports multiple channels over a single connection. +- **Efficient Framing**: Provides an efficient and easy-to-use binary framing mechanism. +- **Customizable Buffers**: Allows control over buffer sizes for optimized performance. + +## Installation + +Install `binproto` by running: + +```sh +go get github.com/onur1/binproto +``` + +## Usage + +Here’s a basic example of how to use `binproto` for a simple client-server communication setup: + +```go +package main + +import ( + "log" + "net" + "github.com/onur1/binproto" +) + +func main() { + // Create a new server instance + server := &Server{} + + // Start the server + go func() { + if err := server.Start(":4242"); err != nil { + log.Fatal(err) + } + }() + + // Connect a client + client, err := binproto.Dial("tcp", ":4242") + if err != nil { + log.Fatal(err) + } + + // Send a message from the client + client.Send(binproto.NewMessage(1, 1, []byte("Hello Server"))) + + // Close the client + client.Close() +} +``` + +### Server Code + +To set up a server using `binproto`, create a handler to process incoming messages. + +```go +type Server struct { + listener net.Listener +} + +func (s *Server) Start(addr string) error { + l, err := net.Listen("tcp", addr) + if err != nil { + return err + } + s.listener = l + for { + conn, err := s.listener.Accept() + if err != nil { + return err + } + go s.handleConnection(conn) + } +} + +func (s *Server) handleConnection(conn net.Conn) { + c := binproto.NewConn(conn) + for { + msg, err := c.ReadMessage() + if err != nil { + return + } + log.Printf("Received message: %s", msg.Data) + } +} +``` + +## License + +MIT License. See [LICENSE](LICENSE) for details. -- cgit v1.3.1