Skip to content
This repository was archived by the owner on Apr 28, 2024. It is now read-only.

Commit 73dd340

Browse files
author
Max Nikitenko
committed
feat(transport): Add unix socket transport;
1 parent 3d5463c commit 73dd340

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

transport/unix.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//Package transport provides transports for rpc server
2+
//
3+
//Copyright (C) 2022 Max Nikitenko <[email protected]>
4+
//
5+
//This file is part of go.neonxp.dev/jsonrpc2 project.
6+
//
7+
//This program is free software: you can redistribute it and/or modify
8+
//it under the terms of the GNU General Public License as published by
9+
//the Free Software Foundation, either version 3 of the License, or
10+
//(at your option) any later version.
11+
//
12+
//This program is distributed in the hope that it will be useful,
13+
//but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
//GNU General Public License for more details.
16+
//
17+
//You should have received a copy of the GNU General Public License
18+
//along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
package transport
20+
21+
import (
22+
"context"
23+
"net"
24+
)
25+
26+
type UnixSocket struct {
27+
Path string
28+
Parallel bool
29+
}
30+
31+
func (t *UnixSocket) Run(ctx context.Context, resolver Resolver) error {
32+
tcpAddr, err := net.ResolveUnixAddr("unix", t.Path)
33+
if err != nil {
34+
return err
35+
}
36+
ln, err := net.ListenUnix("unix", tcpAddr)
37+
if err != nil {
38+
return err
39+
}
40+
41+
go func() {
42+
<-ctx.Done()
43+
_ = ln.Close()
44+
}()
45+
for {
46+
conn, err := ln.Accept()
47+
if err != nil {
48+
return err
49+
}
50+
go resolver.Resolve(ctx, conn, conn, t.Parallel)
51+
}
52+
}

0 commit comments

Comments
 (0)