-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_wire.go
65 lines (52 loc) · 1.54 KB
/
server_wire.go
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
// Copyright (c) 2022-present, DiceDB contributors
// All rights reserved. Licensed under the BSD 3-Clause License. See LICENSE file in the project root for full license information.
package wire
import (
"fmt"
"net"
"os"
"time"
"wire/internal"
)
type ServerWire struct {
*internal.ProtobufTCPWire
}
func NewServerWire(maxMsgSize int, keepAlive int, clientFD int) (*ServerWire, error) {
file := os.NewFile(uintptr(clientFD), "client-connection")
if file == nil {
return nil, fmt.Errorf("failed to create file from file descriptor")
}
conn, err := net.FileConn(file)
if err != nil {
return nil, fmt.Errorf("failed to create net.Conn from file descriptor: %w", err)
}
if tcpConn, ok := conn.(*net.TCPConn); ok {
if err := tcpConn.SetNoDelay(true); err != nil {
return nil, fmt.Errorf("failed to set TCP_NODELAY: %w", err)
}
if err := tcpConn.SetKeepAlive(true); err != nil {
return nil, fmt.Errorf("failed to set keepalive: %w", err)
}
if err := tcpConn.SetKeepAlivePeriod(time.Duration(keepAlive) * time.Second); err != nil {
return nil, fmt.Errorf("failed to set keepalive period: %w", err)
}
}
wire := &ServerWire{
ProtobufTCPWire: internal.NewProtobufTCPWire(maxMsgSize, conn),
}
return wire, nil
}
func (sw *ServerWire) Send(resp *Response) error {
return sw.ProtobufTCPWire.Send(resp)
}
func (sw *ServerWire) Receive() (*Command, error) {
cmd := &Command{}
err := sw.ProtobufTCPWire.Receive(cmd)
if err != nil {
return nil, err
}
return cmd, nil
}
func (sw *ServerWire) Close() {
sw.ProtobufTCPWire.Close()
}