-
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.
- Loading branch information
Showing
10 changed files
with
183 additions
and
43 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
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,62 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net" | ||
"time" | ||
wkNet "wukong/net" | ||
) | ||
|
||
func main() { | ||
|
||
fmt.Println("client1 start...") | ||
time.Sleep(1 + time.Second) | ||
|
||
conn, err := net.Dial("tcp", "127.0.0.1:9527") | ||
if err != nil { | ||
fmt.Println("client start err exit! ") | ||
return | ||
} | ||
|
||
for { | ||
dp := wkNet.NewDataPack() | ||
binaryMsg, err := dp.Pack(wkNet.NewMessage(1, []byte("LiuLiGamesV0.7 client1 Test Message "))) | ||
if err != nil { | ||
fmt.Println("Pack error", err) | ||
} | ||
|
||
if _, err := conn.Write(binaryMsg); err != nil { | ||
fmt.Println("conn Write error", err) | ||
return | ||
} | ||
|
||
binaryHead := make([]byte, dp.GetHeadLen()) | ||
if _, err := io.ReadFull(conn, binaryHead); err != nil { | ||
fmt.Println("read head error", err) | ||
return | ||
} | ||
|
||
msgHead, err := dp.Unpack(binaryHead) | ||
if err != nil { | ||
fmt.Println("client unpack msgHead error", err) | ||
break | ||
} | ||
|
||
if msgHead.GetMsgLen() > 0 { | ||
msg := msgHead.(*wkNet.Message) | ||
msg.SetData(make([]byte, msg.GetMsgLen())) | ||
|
||
if _, err := io.ReadFull(conn, msg.Data); err != nil { | ||
fmt.Println("read msg data error", err) | ||
return | ||
} | ||
|
||
fmt.Println("-----> Recv Server msgId= ", | ||
msg.Id, " len= ", msg.MsgLen, " data = ", string(msg.Data)) | ||
} | ||
|
||
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
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,7 @@ | ||
package iface | ||
|
||
type IMsgHandler interface { | ||
DoMsgHandler(request IRequest) | ||
|
||
AddRouter(msgId uint32, 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 |
---|---|---|
|
@@ -5,6 +5,5 @@ type IServer interface { | |
Stop() | ||
Serve() | ||
|
||
AddRouter(router IRouter) | ||
|
||
AddRouter(msgId uint32, 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
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
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,37 @@ | ||
package net | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"wukong/iface" | ||
) | ||
|
||
type MsgHandler struct { | ||
Apis map[uint32]iface.IRouter | ||
} | ||
|
||
func NewMsgHandler() *MsgHandler { | ||
return &MsgHandler{ | ||
Apis: make(map[uint32]iface.IRouter), | ||
} | ||
} | ||
|
||
func (mh *MsgHandler) DoMsgHandler(request iface.IRequest) { | ||
handler, ok := mh.Apis[request.GetMsgId()] | ||
if !ok { | ||
fmt.Println(" api msgId = ", request.GetMsgId(), "is not found Need Register") | ||
} | ||
|
||
handler.PreHandle(request) | ||
handler.Handle(request) | ||
handler.PostHandle(request) | ||
} | ||
|
||
func (mh *MsgHandler) AddRouter(msgId uint32, router iface.IRouter) { | ||
if _, ok := mh.Apis[msgId]; ok { | ||
panic("repeat api , msgId = " + strconv.Itoa(int(msgId))) | ||
} | ||
|
||
mh.Apis[msgId] = router | ||
fmt.Println("Add api msgId = ", msgId) | ||
} |
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
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