Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
84b28d4c12 |
+19
-1
@@ -20,6 +20,11 @@ type RawConn struct {
|
|||||||
W io.Writer
|
W io.Writer
|
||||||
compressionThreshold int32
|
compressionThreshold int32
|
||||||
CompressionThresholdMut sync.RWMutex
|
CompressionThresholdMut sync.RWMutex
|
||||||
|
|
||||||
|
// MaxPacketSize is the maximum size of a packet that can be sent or received.
|
||||||
|
// Applies to both raw and compressed packets.
|
||||||
|
// Should be set while the connection is not in use. Or in sync mode.
|
||||||
|
MaxPacketSize int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *RawConn) SetCompressionThreshold(threshold int32) {
|
func (c *RawConn) SetCompressionThreshold(threshold int32) {
|
||||||
@@ -35,6 +40,10 @@ func (c *RawConn) GetCompressionThreshold() int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *RawConn) SendRaw(rawPacketBytes []byte) (err error) {
|
func (c *RawConn) SendRaw(rawPacketBytes []byte) (err error) {
|
||||||
|
if c.MaxPacketSize > 0 && int32(len(rawPacketBytes)) > c.MaxPacketSize {
|
||||||
|
err = errors.New("packet too large")
|
||||||
|
return
|
||||||
|
}
|
||||||
//fmt.Println("send:", rawPacketBytes)
|
//fmt.Println("send:", rawPacketBytes)
|
||||||
var packetBytes []byte
|
var packetBytes []byte
|
||||||
threshold := c.GetCompressionThreshold()
|
threshold := c.GetCompressionThreshold()
|
||||||
@@ -45,7 +54,7 @@ func (c *RawConn) SendRaw(rawPacketBytes []byte) (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
zW := zlib.NewWriter(packetBuffer) // ignore we don't care about the result if it errors
|
zW := zlib.NewWriter(packetBuffer)
|
||||||
_, err = zW.Write(rawPacketBytes)
|
_, err = zW.Write(rawPacketBytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -86,6 +95,11 @@ func (c *RawConn) ReceiveRaw() (packetBytes []byte, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if c.MaxPacketSize > 0 && rawPacketLen > c.MaxPacketSize {
|
||||||
|
err = errors.New("packet too large")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
rawPacketBytes, err := io.ReadAll(io.LimitReader(c.R, int64(rawPacketLen)))
|
rawPacketBytes, err := io.ReadAll(io.LimitReader(c.R, int64(rawPacketLen)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@@ -101,6 +115,10 @@ func (c *RawConn) ReceiveRaw() (packetBytes []byte, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if c.MaxPacketSize > 0 && packetLen > c.MaxPacketSize {
|
||||||
|
err = errors.New("deflated packet too large")
|
||||||
|
return
|
||||||
|
}
|
||||||
if packetLen == 0 {
|
if packetLen == 0 {
|
||||||
packetBytes, err = io.ReadAll(rawPacketBuffer)
|
packetBytes, err = io.ReadAll(rawPacketBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user