-
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
1 parent
a0c18b8
commit 2b454c6
Showing
17 changed files
with
389 additions
and
257 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
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,4 @@ | ||
name: "liuLI V0.4 demoServerApp" | ||
Host: "127.0.0.1" | ||
TcpPort: 9527 | ||
MaxConn: 3 |
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,9 @@ | ||
package iface | ||
|
||
type IDataPack interface { | ||
GetHeadLen() uint32 | ||
|
||
Pack(msg IMessage) ([]byte, error) | ||
|
||
Unpack([]byte) (IMessage, 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,11 @@ | ||
package iface | ||
|
||
type IMessage interface { | ||
GetMsgId() uint32 | ||
GetMsgLen() uint32 | ||
GetData() []byte | ||
|
||
SetMsgId(uint32) | ||
SetMsgLen(uint32) | ||
SetData([]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 |
---|---|---|
|
@@ -5,4 +5,6 @@ type IRequest interface { | |
GetConnection() IConnection | ||
|
||
GetData() []byte | ||
|
||
GetMsgId() uint32 | ||
} |
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,58 @@ | ||
package net | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"errors" | ||
"wukong/iface" | ||
"wukong/utils" | ||
) | ||
|
||
type DataPack struct{} | ||
|
||
func NewDataPack() *DataPack { | ||
return &DataPack{} | ||
} | ||
|
||
func (dp *DataPack) GetHeadLen() uint32 { | ||
//DataLen uint32(4字节) + Id uint32(4字节) | ||
return 8 | ||
} | ||
|
||
func (dp *DataPack) Pack(msg iface.IMessage) ([]byte, error) { | ||
dataBuff := bytes.NewBuffer([]byte{}) | ||
|
||
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetMsgLen()); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetMsgId()); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := binary.Write(dataBuff, binary.LittleEndian, msg.GetData()); err != nil { | ||
return nil, err | ||
} | ||
|
||
return dataBuff.Bytes(), nil | ||
} | ||
|
||
func (dp *DataPack) Unpack(binaryData []byte) (iface.IMessage, error) { | ||
dataBuff := bytes.NewReader(binaryData) | ||
|
||
msg := &Message{} | ||
|
||
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.MsgLen); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := binary.Read(dataBuff, binary.LittleEndian, &msg.Id); err != nil { | ||
return nil, err | ||
} | ||
|
||
if utils.GlobalObject.MaxPackageSize > 0 && msg.MsgLen > utils.GlobalObject.MaxPackageSize { | ||
return nil, errors.New("too Large msg data recv") | ||
} | ||
|
||
return msg, nil | ||
} |
Oops, something went wrong.