网络层

This commit is contained in:
2025-06-28 17:38:22 +08:00
parent 54dc7ba173
commit 605197345b
20 changed files with 482 additions and 376 deletions

View File

@@ -15,12 +15,13 @@ import (
// WSConn 实现ISocketConn接口
type WSConn struct {
gnet.Conn
buf bytes.Buffer
logger logging.Logger
isUpgrade bool
isClose bool
param map[string]string
openTime int64
buf bytes.Buffer
logger logging.Logger
isUpgrade bool
isClose bool
param map[string]interface{}
openTime int64
remoteAddr string
wsMessageBuf
}
@@ -169,19 +170,24 @@ func (w *WSConn) OnRequest(u []byte) error {
return nil
}
func (w *WSConn) GetParam(key string) string {
func (w *WSConn) GetParam(key string) interface{} {
return w.param[key]
}
func (w *WSConn) SetParam(key string, values string) {
func (w *WSConn) SetParam(key string, values interface{}) {
w.param[key] = values
}
func (w *WSConn) RemoteAddr() string {
return w.remoteAddr
}
func (w *WSConn) Write(data []byte) error {
if w.isClose {
return errors.New("connection has close")
}
return w.write(data, ws.OpBinary)
return w.write(data, ws.OpText)
}
func (w *WSConn) Ping() (err error) {
return w.write(make([]byte, 0), ws.OpPing)
}
func (w *WSConn) Close() (err error) {
@@ -192,74 +198,12 @@ func (w *WSConn) Close() (err error) {
}
func (w *WSConn) write(data []byte, opCode ws.OpCode) error {
if w.isClose {
return errors.New("connection has close")
}
buf := bytes.Buffer{}
err := wsutil.WriteServerMessage(&buf, opCode, data)
if err != nil {
if err := wsutil.WriteServerMessage(&buf, opCode, data); err != nil {
return err
}
return w.Conn.AsyncWrite(buf.Bytes(), nil)
}
//func (w *WSConn) Pong(data []byte) (err error) {
// buf := bytes.Buffer{}
// if data == nil {
// err = wsutil.WriteServerMessage(&buf, ws.OpPong, emptyPayload)
// } else {
// err = wsutil.WriteServerMessage(&buf, ws.OpPong, data)
// }
// if w.isClose {
// return errors.New("connection has close")
// }
// if err != nil {
// return
// }
// return w.Conn.AsyncWrite(buf.Bytes(), nil)
//}
//func (w *WSConn) Ping(data []byte) (err error) {
// buf := bytes.Buffer{}
// if data == nil {
// err = wsutil.WriteServerMessage(&buf, ws.OpPing, emptyPayload)
// } else {
// err = wsutil.WriteServerMessage(&buf, ws.OpPing, data)
// }
// if w.isClose {
// return errors.New("connection has close")
// }
// if err != nil {
// return err
// }
//
// return w.Conn.AsyncWrite(buf.Bytes(), nil)
//}
//func (w *WSConn) GetProperty(key string) (interface{}, error) {
// if w.context[key] == nil {
// return nil, errors.New("not found this key")
// }
// return w.context[key], nil
//}
//
//func (w *WSConn) SetProperty(key string, ctx interface{}) {
// w.context[key] = ctx
//}
//
//func (w *WSConn) RemoveProperty(key string) {
// delete(w.context, key)
//}
//
//func (w *WSConn) GetConnID() uint64 {
// return w.connID
//}
//
//func (w *WSConn) Clear() {
// w.context = make(map[string]interface{})
//}
//
//func (w *WSConn) GetUrlParam() map[string][]string {
// return w.urlParma
//}
//
//func (w *WSConn) SetUrlParam(key string, values []string) {
// w.urlParma[key] = values
//}