防止并发写网络

This commit is contained in:
dexter
2023-02-03 10:16:55 +08:00
parent 4615ca2472
commit f42849b149
3 changed files with 18 additions and 2 deletions

View File

@@ -6,6 +6,8 @@ import (
"errors"
"io"
"net"
"runtime"
"sync/atomic"
"m7s.live/engine/v4/util"
)
@@ -58,6 +60,7 @@ type NetConnection struct {
tmpBuf util.Buffer //用来接收/发送小数据,复用内存
chunkHeader util.Buffer
bytePool util.BytesPool
writing atomic.Bool // false 可写true 不可写
}
func NewNetConnection(conn net.Conn) *NetConnection {
@@ -291,6 +294,10 @@ func (conn *NetConnection) SendMessage(t byte, msg RtmpMessage) (err error) {
err = conn.SendMessage(RTMP_MSG_ACK, Uint32Message(conn.totalWrite))
err = conn.SendStreamID(RTMP_USER_PING_REQUEST, 0)
}
for !conn.writing.CompareAndSwap(false, true) {
runtime.Gosched()
}
defer conn.writing.Store(false)
conn.tmpBuf.Reset()
msg.Encode(&conn.tmpBuf)
head := newChunkHeader(t)