mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-13 23:24:45 +08:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9945c29a5c | ||
|
|
173d84f7e6 |
@@ -21,7 +21,7 @@ type WSServer struct {
|
|||||||
NewAgent func(*WSConn) Agent
|
NewAgent func(*WSConn) Agent
|
||||||
ln net.Listener
|
ln net.Listener
|
||||||
handler *WSHandler
|
handler *WSHandler
|
||||||
messageType int
|
messageType int
|
||||||
}
|
}
|
||||||
|
|
||||||
type WSHandler struct {
|
type WSHandler struct {
|
||||||
@@ -33,10 +33,10 @@ type WSHandler struct {
|
|||||||
conns WebsocketConnSet
|
conns WebsocketConnSet
|
||||||
mutexConns sync.Mutex
|
mutexConns sync.Mutex
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
messageType int
|
messageType int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *WSHandler) SetMessageType(messageType int){
|
func (handler *WSHandler) SetMessageType(messageType int) {
|
||||||
handler.messageType = messageType
|
handler.messageType = messageType
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,9 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
conn.SetReadLimit(int64(handler.maxMsgLen))
|
conn.SetReadLimit(int64(handler.maxMsgLen))
|
||||||
handler.messageType = websocket.TextMessage
|
if handler.messageType == 0 {
|
||||||
|
handler.messageType = websocket.TextMessage
|
||||||
|
}
|
||||||
|
|
||||||
handler.wg.Add(1)
|
handler.wg.Add(1)
|
||||||
defer handler.wg.Done()
|
defer handler.wg.Done()
|
||||||
@@ -71,7 +73,7 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
handler.conns[conn] = struct{}{}
|
handler.conns[conn] = struct{}{}
|
||||||
handler.mutexConns.Unlock()
|
handler.mutexConns.Unlock()
|
||||||
|
|
||||||
wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen,handler.messageType)
|
wsConn := newWSConn(conn, handler.pendingWriteNum, handler.maxMsgLen, handler.messageType)
|
||||||
agent := handler.newAgent(wsConn)
|
agent := handler.newAgent(wsConn)
|
||||||
agent.Run()
|
agent.Run()
|
||||||
|
|
||||||
@@ -83,9 +85,9 @@ func (handler *WSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
agent.OnClose()
|
agent.OnClose()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *WSServer) SetMessageType(messageType int){
|
func (server *WSServer) SetMessageType(messageType int) {
|
||||||
server.messageType = messageType
|
server.messageType = messageType
|
||||||
if server.handler!= nil {
|
if server.handler != nil {
|
||||||
server.handler.SetMessageType(messageType)
|
server.handler.SetMessageType(messageType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ func NewPool(C chan interface{},New func()interface{}) *Pool{
|
|||||||
func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{
|
func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{
|
||||||
var pool PoolEx
|
var pool PoolEx
|
||||||
pool.C = C
|
pool.C = C
|
||||||
//pool.New = New
|
|
||||||
pool.syncPool.New = func() interface{} {
|
pool.syncPool.New = func() interface{} {
|
||||||
return New()
|
return New()
|
||||||
}
|
}
|
||||||
@@ -61,10 +60,18 @@ func NewPoolEx(C chan IPoolData,New func()IPoolData) *PoolEx{
|
|||||||
func (pool *PoolEx) Get() IPoolData{
|
func (pool *PoolEx) Get() IPoolData{
|
||||||
select {
|
select {
|
||||||
case d := <-pool.C:
|
case d := <-pool.C:
|
||||||
|
if d.IsRef() {
|
||||||
|
panic("Pool data is in use.")
|
||||||
|
}
|
||||||
|
|
||||||
d.Ref()
|
d.Ref()
|
||||||
return d
|
return d
|
||||||
default:
|
default:
|
||||||
data := pool.syncPool.Get().(IPoolData)
|
data := pool.syncPool.Get().(IPoolData)
|
||||||
|
if data.IsRef() {
|
||||||
|
panic("Pool data is in use.")
|
||||||
|
}
|
||||||
|
|
||||||
data.Ref()
|
data.Ref()
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@@ -76,7 +83,10 @@ func (pool *PoolEx) Put(data IPoolData){
|
|||||||
if data.IsRef() == false {
|
if data.IsRef() == false {
|
||||||
panic("Repeatedly freeing memory")
|
panic("Repeatedly freeing memory")
|
||||||
}
|
}
|
||||||
|
//提前解引用,防止递归释放
|
||||||
|
data.UnRef()
|
||||||
data.Reset()
|
data.Reset()
|
||||||
|
//再次解引用,防止Rest时错误标记
|
||||||
data.UnRef()
|
data.UnRef()
|
||||||
select {
|
select {
|
||||||
case pool.C <- data:
|
case pool.C <- data:
|
||||||
|
|||||||
Reference in New Issue
Block a user