1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# binproto
`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.
|