Commit 98dfd51c authored by AlexStocks's avatar AlexStocks

delete udp_server

parent d1e123b3
...@@ -66,7 +66,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client { ...@@ -66,7 +66,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
c.init(opts...) c.init(opts...)
if t != UNCONNECTED_UDP_CLIENT && c.number <= 0 || c.addr == "" { if c.number <= 0 || c.addr == "" {
panic(fmt.Sprintf("@connNum:%d, @serverAddr:%s", c.number, c.addr)) panic(fmt.Sprintf("@connNum:%d, @serverAddr:%s", c.number, c.addr))
} }
...@@ -80,20 +80,9 @@ func NewTCPClient(opts ...ClientOption) Client { ...@@ -80,20 +80,9 @@ func NewTCPClient(opts ...ClientOption) Client {
return newClient(TCP_CLIENT, opts...) return newClient(TCP_CLIENT, opts...)
} }
// NewUdpClient function builds a udp client // NewUdpClient function builds a connected udp client
func NewUDPClient(opts ...ClientOption) Client { func NewUDPClient(opts ...ClientOption) Client {
c := newClient(UNCONNECTED_UDP_CLIENT, opts...) return newClient(UDP_CLIENT, opts...)
if len(c.addr) != 0 {
if c.number <= 0 {
panic(fmt.Sprintf("getty will build a preconected connection by @serverAddr:%s while @connNum is %d",
c.addr, c.number))
}
c.endPointType = CONNECTED_UDP_CLIENT
}
return c
} }
// NewWsClient function builds a ws client. // NewWsClient function builds a ws client.
...@@ -121,7 +110,7 @@ func NewWSSClient(opts ...ClientOption) Client { ...@@ -121,7 +110,7 @@ func NewWSSClient(opts ...ClientOption) Client {
return c return c
} }
func (c client) Type() EndPointType { func (c client) EndPointType() EndPointType {
return c.endPointType return c.endPointType
} }
...@@ -162,13 +151,9 @@ func (c *client) dialUDP() Session { ...@@ -162,13 +151,9 @@ func (c *client) dialUDP() Session {
if c.IsClosed() { if c.IsClosed() {
return nil return nil
} }
if UNCONNECTED_UDP_CLIENT == c.endPointType { conn, err = net.DialUDP("udp", localAddr, peerAddr)
conn, err = net.ListenUDP("udp", localAddr) if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
} else { err = errSelfConnect
conn, err = net.DialUDP("udp", localAddr, peerAddr)
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
err = errSelfConnect
}
} }
if err == nil { if err == nil {
return newUDPSession(conn, c.endPointType) return newUDPSession(conn, c.endPointType)
...@@ -292,7 +277,7 @@ func (c *client) dial() Session { ...@@ -292,7 +277,7 @@ func (c *client) dial() Session {
switch c.endPointType { switch c.endPointType {
case TCP_CLIENT: case TCP_CLIENT:
return c.dialTCP() return c.dialTCP()
case UNCONNECTED_UDP_CLIENT, CONNECTED_UDP_CLIENT: case UDP_CLIENT:
return c.dialUDP() return c.dialUDP()
case WS_CLIENT: case WS_CLIENT:
return c.dialWS() return c.dialWS()
...@@ -378,9 +363,9 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) { ...@@ -378,9 +363,9 @@ func (c *client) RunEventLoop(newSession NewSessionCallback) {
} }
times = 0 times = 0
c.connect() c.connect()
if c.endPointType == UNCONNECTED_UDP_CLIENT || c.endPointType == CONNECTED_UDP_CLIENT { //if c.endPointType == UDP_CLIENT {
break // break
} //}
// time.Sleep(c.interval) // build c.number connections asap // time.Sleep(c.interval) // build c.number connections asap
} }
}() }()
......
...@@ -387,7 +387,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) { ...@@ -387,7 +387,7 @@ func (u *gettyUDPConn) read(p []byte) (int, *net.UDPAddr, error) {
} }
} }
if u.ss.Type() == CONNECTED_UDP_CLIENT { if u.ss.EndPointType() == UDP_CLIENT {
length, err = u.conn.Read(p) length, err = u.conn.Read(p)
} else { } else {
length, addr, err = u.conn.ReadFromUDP(p) length, addr, err = u.conn.ReadFromUDP(p)
...@@ -418,7 +418,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) { ...@@ -418,7 +418,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
if buf, ok = ctx.Pkg.([]byte); !ok { if buf, ok = ctx.Pkg.([]byte); !ok {
return 0, fmt.Errorf("illegal @udpCtx.Pkg{%#v} type", udpCtx) return 0, fmt.Errorf("illegal @udpCtx.Pkg{%#v} type", udpCtx)
} }
if u.ss.Type() == UDP_SERVER || u.ss.Type() == UNCONNECTED_UDP_CLIENT { if u.ss.EndPointType() == UDP_CLIENT {
peerAddr = ctx.PeerAddr peerAddr = ctx.PeerAddr
if peerAddr == nil { if peerAddr == nil {
return 0, ErrNullPeerAddr return 0, ErrNullPeerAddr
......
...@@ -17,42 +17,36 @@ import ( ...@@ -17,42 +17,36 @@ import (
type EndPointType int32 type EndPointType int32
const ( const (
CONNECTED_UDP_CLIENT EndPointType = 0 UDP_ENDPOINT EndPointType = 0
UNCONNECTED_UDP_CLIENT EndPointType = 1 UDP_CLIENT EndPointType = 1
TCP_CLIENT EndPointType = 2 TCP_CLIENT EndPointType = 2
WS_CLIENT EndPointType = 3 WS_CLIENT EndPointType = 3
WSS_CLIENT EndPointType = 4 WSS_CLIENT EndPointType = 4
TCP_SERVER EndPointType = 7
UDP_SERVER EndPointType = 6 WS_SERVER EndPointType = 8
TCP_SERVER EndPointType = 7 WSS_SERVER EndPointType = 9
WS_SERVER EndPointType = 8
WSS_SERVER EndPointType = 9
) )
var EndPointType_name = map[int32]string{ var EndPointType_name = map[int32]string{
0: "CONNECTED_UDP_CLIENT", 0: "UDP_ENDPOINT",
1: "UNCONNECTED_UDP_CLIENT", 1: "UDP_CLIENT",
2: "TCP_CLIENT", 2: "TCP_CLIENT",
3: "WS_CLIENT", 3: "WS_CLIENT",
4: "WSS_CLIENT", 4: "WSS_CLIENT",
6: "UDP_SERVER",
7: "TCP_SERVER", 7: "TCP_SERVER",
8: "WS_SERVER", 8: "WS_SERVER",
9: "WSS_SERVER", 9: "WSS_SERVER",
} }
var EndPointType_value = map[string]int32{ var EndPointType_value = map[string]int32{
"CONNECTED_UDP_CLIENT": 0, "UDP_ENDPOINT": 0,
"UNCONNECTED_UDP_CLIENT": 1, "UDP_CLIENT": 1,
"TCP_CLIENT": 2, "TCP_CLIENT": 2,
"WS_CLIENT": 3, "WS_CLIENT": 3,
"WSS_CLIENT": 4, "WSS_CLIENT": 4,
"TCP_SERVER": 7,
"UDP_SERVER": 6, "WS_SERVER": 8,
"TCP_SERVER": 7, "WSS_SERVER": 9,
"WS_SERVER": 8,
"WSS_SERVER": 9,
} }
func (x EndPointType) String() string { func (x EndPointType) String() string {
......
...@@ -126,7 +126,7 @@ type Session interface { ...@@ -126,7 +126,7 @@ type Session interface {
Stat() string Stat() string
IsClosed() bool IsClosed() bool
// get endpoint type // get endpoint type
Type() EndPointType EndPointType() EndPointType
SetMaxMsgLen(int) SetMaxMsgLen(int)
SetName(string) SetName(string)
...@@ -157,7 +157,7 @@ type Session interface { ...@@ -157,7 +157,7 @@ type Session interface {
type EndPoint interface { type EndPoint interface {
// get endpoint type // get endpoint type
Type() EndPointType EndPointType() EndPointType
// run event loop and serves client request. // run event loop and serves client request.
RunEventLoop(newSession NewSessionCallback) RunEventLoop(newSession NewSessionCallback)
// check the endpoint has been closed // check the endpoint has been closed
......
...@@ -75,9 +75,9 @@ func NewTCPServer(opts ...ServerOption) Server { ...@@ -75,9 +75,9 @@ func NewTCPServer(opts ...ServerOption) Server {
return newServer(TCP_SERVER, opts...) return newServer(TCP_SERVER, opts...)
} }
// NewUDPServer builds a unconnected udp server. // NewUDPEndPoint builds a unconnected udp server.
func NewUDPPServer(opts ...ServerOption) Server { func NewUDPPEndPoint(opts ...ServerOption) Server {
return newServer(UDP_SERVER, opts...) return newServer(UDP_ENDPOINT, opts...)
} }
// NewWSServer builds a websocket server. // NewWSServer builds a websocket server.
...@@ -97,7 +97,7 @@ func NewWSSServer(opts ...ServerOption) Server { ...@@ -97,7 +97,7 @@ func NewWSSServer(opts ...ServerOption) Server {
return s return s
} }
func (s server) Type() EndPointType { func (s server) EndPointType() EndPointType {
return s.endPointType return s.endPointType
} }
...@@ -195,7 +195,7 @@ func (s *server) listen() error { ...@@ -195,7 +195,7 @@ func (s *server) listen() error {
switch s.endPointType { switch s.endPointType {
case TCP_SERVER, WS_SERVER, WSS_SERVER: case TCP_SERVER, WS_SERVER, WSS_SERVER:
return s.listenTCP() return s.listenTCP()
case UDP_SERVER: case UDP_ENDPOINT:
return s.listenUDP() return s.listenUDP()
} }
...@@ -432,7 +432,7 @@ func (s *server) RunEventLoop(newSession NewSessionCallback) { ...@@ -432,7 +432,7 @@ func (s *server) RunEventLoop(newSession NewSessionCallback) {
switch s.endPointType { switch s.endPointType {
case TCP_SERVER: case TCP_SERVER:
s.runTcpEventLoop(newSession) s.runTcpEventLoop(newSession)
case UDP_SERVER: case UDP_ENDPOINT:
s.runUDPEventLoop(newSession) s.runUDPEventLoop(newSession)
case WS_SERVER: case WS_SERVER:
s.runWSEventLoop(newSession) s.runWSEventLoop(newSession)
......
...@@ -77,7 +77,7 @@ type session struct { ...@@ -77,7 +77,7 @@ type session struct {
} }
func newSession(endPointType EndPointType, conn Connection) *session { func newSession(endPointType EndPointType, conn Connection) *session {
session := &session{ ss := &session{
name: defaultSessionName, name: defaultSessionName,
endPointType: endPointType, endPointType: endPointType,
maxMsgLen: maxReadBufLen, maxMsgLen: maxReadBufLen,
...@@ -88,11 +88,11 @@ func newSession(endPointType EndPointType, conn Connection) *session { ...@@ -88,11 +88,11 @@ func newSession(endPointType EndPointType, conn Connection) *session {
attrs: gxcontext.NewValuesContext(nil), attrs: gxcontext.NewValuesContext(nil),
} }
session.Connection.setSession(session) ss.Connection.setSession(ss)
session.SetWriteTimeout(netIOTimeout) ss.SetWriteTimeout(netIOTimeout)
session.SetReadTimeout(netIOTimeout) ss.SetReadTimeout(netIOTimeout)
return session return ss
} }
func newTCPSession(conn net.Conn, endPointType EndPointType) Session { func newTCPSession(conn net.Conn, endPointType EndPointType) Session {
...@@ -150,7 +150,7 @@ func (s *session) Conn() net.Conn { ...@@ -150,7 +150,7 @@ func (s *session) Conn() net.Conn {
return nil return nil
} }
func (s *session) Type() EndPointType { func (s *session) EndPointType() EndPointType {
return s.endPointType return s.endPointType
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment