forked from teamgram/marmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingpong.go
146 lines (119 loc) · 3.33 KB
/
pingpong.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022 Teamgram Authors
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: teamgramio (teamgram.io@gmail.com)
//
package main
import (
"io"
"net"
"github.com/yyblackberry/marmota/pkg/net2"
"github.com/zeromicro/go-zero/core/logx"
)
func init() {
net2.RegisterProtocol("pingpong", NewPingpong(4096))
}
type Pingpong struct {
readBuf int
}
func NewPingpong(readBuf int) net2.Protocol {
if readBuf <= 0 {
readBuf = 4096
}
return &Pingpong{
readBuf: readBuf,
}
}
func (b *Pingpong) NewCodec(rw io.ReadWriter) (cc net2.Codec, err error) {
codec := new(PingpongCodec)
codec.readBuf = b.readBuf
codec.rw = rw
codec.c = rw.(io.Closer)
codec.buf = make([]byte, b.readBuf)
return codec, nil
}
type PingpongCodec struct {
readBuf int
rw io.ReadWriter
c io.Closer
buf []byte
}
func (c *PingpongCodec) Send(msg interface{}) error {
if _, err := c.rw.Write(msg.([]byte)); err != nil {
return err
}
return nil
}
func (c *PingpongCodec) Receive() (interface{}, error) {
buf := make([]byte, c.readBuf)
n, err := c.rw.Read(buf)
if err == nil {
return buf[:n], nil
}
return nil, err
}
func (c *PingpongCodec) Close() error {
return c.c.Close()
}
func (c *PingpongCodec) Context() interface{} {
return nil
}
// PingpongServer
/////////////////////////////////////////////////////////////////////////////////////////
type PingpongServer struct {
server *net2.TcpServer
}
func NewPingpongServer(listener net.Listener, protoName string) *PingpongServer {
s := &PingpongServer{}
s.server = net2.NewTcpServer(net2.TcpServerArgs{Listener: listener, ServerName: "pingpong", ProtoName: protoName, SendChanSize: 0, ConnectionCallback: s, MaxConcurrentConnection: 2})
return s
}
func (s *PingpongServer) Serve() {
s.server.Serve()
}
func (s *PingpongServer) OnNewConnection(conn *net2.TcpConnection) {
logx.Info("OnNewConnection %v", conn.RemoteAddr())
}
func (s *PingpongServer) OnConnectionDataArrived(conn *net2.TcpConnection, msg interface{}) error {
// log.Infof("echo_server recv peer(%v) data: %v", conn.RemoteAddr(), msg)
conn.Send(msg)
return nil
}
func (s *PingpongServer) OnConnectionClosed(conn *net2.TcpConnection) {
logx.Info("OnConnectionClosed - %v", conn.RemoteAddr())
}
/////////////////////////////////////////////////////////////////////////////////////////
type PingpongInsance struct {
server *PingpongServer
}
func (this *PingpongInsance) Initialize() error {
listener, err := net.Listen("tcp", "0.0.0.0:33333")
if err != nil {
logx.Error("listen error: %v", err)
return err
}
this.server = NewPingpongServer(listener, "pingpong")
return nil
}
func (this *PingpongInsance) RunLoop() {
this.server.Serve()
}
func (this *PingpongInsance) Destroy() {
this.server.server.Stop()
}
func main() {
// instance := &PingpongInsance{}
// util.DoMainAppInstance(instance)
}