-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from liuligames/develop
增加服务端于客户端通信,封装请求
- Loading branch information
Showing
15 changed files
with
693 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
func main() { | ||
|
||
fmt.Println("client start...") | ||
time.Sleep(1 + time.Second) | ||
|
||
conn, err := net.Dial("tcp", "127.0.0.1:9527") | ||
if err != nil { | ||
fmt.Println("client start err", err) | ||
return | ||
} | ||
|
||
for { | ||
_, err := conn.Write([]byte("Hello LiuLiGames V0.2 ")) | ||
if err != nil { | ||
fmt.Println("write conn err", err) | ||
return | ||
} | ||
buf := make([]byte, 512) | ||
cnt, err := conn.Read(buf) | ||
if err != nil { | ||
fmt.Println("read buf err", err) | ||
return | ||
} | ||
|
||
fmt.Printf("server call back: %s ,cnt = %d \n", buf, cnt) | ||
|
||
time.Sleep(1 * time.Second) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"wukong/iface" | ||
"wukong/net" | ||
) | ||
|
||
type PingRouter struct { | ||
net.BaseRouter | ||
} | ||
|
||
func (pr *PingRouter) PreHandle(request iface.IRequest) { | ||
fmt.Println("Call Router PreHandle ....") | ||
|
||
_, err := request.GetConnection().GetTCPConnection().Write([]byte("before ping.... \n")) | ||
if err != nil { | ||
fmt.Println("call back before ping error ") | ||
} | ||
} | ||
|
||
func (pr *PingRouter) Handle(request iface.IRequest) { | ||
fmt.Println("Call Router Handle ....") | ||
|
||
_, err := request.GetConnection().GetTCPConnection().Write([]byte("ping...ping...ping... \n")) | ||
if err != nil { | ||
fmt.Println("call back ping...ping...ping... error ") | ||
} | ||
} | ||
|
||
func (pr *PingRouter) PostHandle(request iface.IRequest) { | ||
fmt.Println("Call Router PostHandle ....") | ||
|
||
_, err := request.GetConnection().GetTCPConnection().Write([]byte("after ping.... \n")) | ||
if err != nil { | ||
fmt.Println("call back after ping error ") | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
s := net.NewServer("[liu li games V0.3]") | ||
|
||
s.AddRouter(&PingRouter{}) | ||
|
||
s.Serve() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
module wukong | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/gin-gonic/gin v1.7.2 // indirect | ||
github.com/gorilla/websocket v1.4.2 // indirect | ||
github.com/libp2p/go-reuseport v0.0.1 // indirect | ||
github.com/panjf2000/gnet v1.5.3 // indirect | ||
github.com/smallnest/goframe v0.0.0-20191101094441-1fbd8e51db18 // indirect | ||
go.uber.org/atomic v1.9.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package iface | ||
|
||
import "net" | ||
|
||
type IConnection interface { | ||
Start() | ||
|
||
Stop() | ||
|
||
GetTCPConnection() *net.TCPConn | ||
|
||
GetConnID() uint32 | ||
|
||
GetRemoteAddr() net.Addr | ||
|
||
Send(data []byte) error | ||
} | ||
|
||
type HandleFunc func(*net.TCPConn, []byte, int) error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package iface | ||
|
||
type IRequest interface { | ||
|
||
GetConnection() IConnection | ||
|
||
GetData() []byte | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package iface | ||
|
||
type IRouter interface { | ||
PreHandle(request IRequest) | ||
|
||
Handle(request IRequest) | ||
|
||
PostHandle(request IRequest) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package iface | ||
|
||
type IServer interface { | ||
Start() | ||
Stop() | ||
Serve() | ||
|
||
AddRouter(router IRouter) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/panjf2000/gnet" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
//// 初始化引擎 | ||
//engine := gin.Default() | ||
//// 注册一个路由和处理函数 | ||
//engine.GET("/", middleware1, middleware2, WebRoot) | ||
// | ||
//engine.GET("/user/:name", func(c *gin.Context) { | ||
// // 使用 c.Param(key) 获取 url 参数 | ||
// name := c.Param("name") | ||
// c.String(http.StatusOK, "Hello %s", name) | ||
//}) | ||
// | ||
//// 绑定端口,然后启动应用 | ||
//engine.Run(":9205") | ||
|
||
echo := new(echoServer) | ||
log.Fatal(gnet.Serve(echo, "tcp://:9000", gnet.WithMulticore(true))) | ||
} | ||
|
||
/** | ||
* 根请求处理函数 | ||
* 所有本次请求相关的方法都在 context 中,完美 | ||
* 输出响应 hello, world | ||
*/ | ||
func WebRoot(context *gin.Context) { | ||
firstname := context.DefaultQuery("firstname", "Guest") | ||
lastname := context.Query("lastname") | ||
context.String(http.StatusOK, "hello %s %s", firstname, lastname) | ||
} | ||
|
||
func middleware1(context *gin.Context) { | ||
log.Println("exec middleware1") | ||
a := 1 | ||
|
||
log.Println(a) | ||
|
||
} | ||
func middleware2(context *gin.Context) { | ||
log.Println("exec middleware2") | ||
} | ||
|
||
type echoServer struct { | ||
*gnet.EventServer | ||
} | ||
|
||
func (es *echoServer) React(frame []byte, c gnet.Conn) (out []byte, action gnet.Action) { | ||
out = frame | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package net | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"wukong/iface" | ||
) | ||
|
||
type Connection struct { | ||
Conn *net.TCPConn | ||
ConnID uint32 | ||
isClosed bool | ||
ExitChan chan bool | ||
Router iface.IRouter | ||
} | ||
|
||
func NewConnection(conn *net.TCPConn, connID uint32, router iface.IRouter) *Connection { | ||
return &Connection{ | ||
Conn: conn, | ||
ConnID: connID, | ||
Router: router, | ||
isClosed: false, | ||
ExitChan: make(chan bool, 1), | ||
} | ||
} | ||
|
||
func (c *Connection) StartReader() { | ||
fmt.Println("reader goroutine is running.....") | ||
defer fmt.Println("connId = ", c.ConnID, "reader is exit remote addr is ", c.GetRemoteAddr().String()) | ||
defer c.Stop() | ||
|
||
for { | ||
buf := make([]byte, 512) | ||
_, err := c.Conn.Read(buf) | ||
if err != nil { | ||
fmt.Println("recv buf err", err) | ||
continue | ||
} | ||
|
||
req := Request{ | ||
conn: c, | ||
data: buf, | ||
} | ||
|
||
go func(request iface.IRequest) { | ||
c.Router.PreHandle(request) | ||
c.Router.Handle(request) | ||
c.Router.PostHandle(request) | ||
}(&req) | ||
} | ||
} | ||
|
||
func (c *Connection) Start() { | ||
fmt.Println("conn start ... connId = ", c.ConnID) | ||
|
||
go c.StartReader() | ||
//todo 启动从当前连接写数据的业务 | ||
|
||
} | ||
|
||
func (c *Connection) Stop() { | ||
fmt.Println("conn stop ... connId = ", c.ConnID) | ||
|
||
if c.isClosed { | ||
return | ||
} | ||
c.isClosed = true | ||
|
||
_ = c.Conn.Close() | ||
|
||
close(c.ExitChan) | ||
} | ||
|
||
func (c *Connection) GetTCPConnection() *net.TCPConn { | ||
return c.Conn | ||
} | ||
|
||
func (c *Connection) GetConnID() uint32 { | ||
return c.ConnID | ||
|
||
} | ||
|
||
func (c *Connection) GetRemoteAddr() net.Addr { | ||
return c.Conn.RemoteAddr() | ||
} | ||
|
||
func (c *Connection) Send(data []byte) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net | ||
|
||
import "wukong/iface" | ||
|
||
type Request struct { | ||
conn iface.IConnection | ||
data []byte | ||
} | ||
|
||
func (r *Request) GetConnection() iface.IConnection { | ||
return r.conn | ||
} | ||
|
||
func (r *Request) GetData() []byte { | ||
return r.data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net | ||
|
||
import "wukong/iface" | ||
|
||
type BaseRouter struct{} | ||
|
||
func (br *BaseRouter) PreHandle(request iface.IRequest) {} | ||
|
||
func (br *BaseRouter) Handle(request iface.IRequest) {} | ||
|
||
func (br *BaseRouter) PostHandle(request iface.IRequest) {} |
Oops, something went wrong.