Commit 09d0a578 authored by AlexStocks's avatar AlexStocks

accumulate all interfaces

parent 78dcd589
......@@ -35,48 +35,6 @@ var (
)
/////////////////////////////////////////
// compress
/////////////////////////////////////////
type CompressType int
const (
CompressNone CompressType = flate.NoCompression // 0
CompressZip = flate.DefaultCompression // -1
CompressBestSpeed = flate.BestSpeed // 1
CompressBestCompression = flate.BestCompression // 9
CompressHuffman = flate.HuffmanOnly // -2
CompressSnappy = 10
)
/////////////////////////////////////////
// connection interfacke
/////////////////////////////////////////
type Connection interface {
ID() uint32
SetCompressType(CompressType)
LocalAddr() string
RemoteAddr() string
incReadPkgCount()
incWritePkgCount()
// update session's active time
UpdateActive()
// get session's active time
GetActive() time.Time
readTimeout() time.Duration
// SetReadTimeout sets deadline for the future read calls.
SetReadTimeout(time.Duration)
writeTimeout() time.Duration
// SetWriteTimeout sets deadline for the future read calls.
SetWriteTimeout(time.Duration)
Write(interface{}) (int, error)
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
close(int)
}
/////////////////////////////////////////
// getty connection
/////////////////////////////////////////
......
/******************************************************
# DESC : codec interface
# DESC : getty interface
# MAINTAINER : Alex Stocks
# LICENCE : Apache License 2.0
# EMAIL : alexstocks@foxmail.com
# MOD : 2016-08-17 11:20
# FILE : codec.go
# FILE : getty.go
******************************************************/
package getty
import (
"compress/flate"
"errors"
"net"
"time"
)
// NewSessionCallback will be invoked when server accepts a new client connection or client connects to server successfully.
......@@ -61,10 +64,100 @@ type EventListener interface {
OnMessage(Session, interface{})
}
/////////////////////////////////////////
// compress
/////////////////////////////////////////
type CompressType int
const (
CompressNone CompressType = flate.NoCompression // 0
CompressZip = flate.DefaultCompression // -1
CompressBestSpeed = flate.BestSpeed // 1
CompressBestCompression = flate.BestCompression // 9
CompressHuffman = flate.HuffmanOnly // -2
CompressSnappy = 10
)
/////////////////////////////////////////
// connection interfacke
/////////////////////////////////////////
type Connection interface {
ID() uint32
SetCompressType(CompressType)
LocalAddr() string
RemoteAddr() string
incReadPkgCount()
incWritePkgCount()
// update session's active time
UpdateActive()
// get session's active time
GetActive() time.Time
readTimeout() time.Duration
// SetReadTimeout sets deadline for the future read calls.
SetReadTimeout(time.Duration)
writeTimeout() time.Duration
// SetWriteTimeout sets deadline for the future read calls.
SetWriteTimeout(time.Duration)
Write(interface{}) (int, error)
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close
close(int)
}
/////////////////////////////////////////
// Session interfacke
/////////////////////////////////////////
var (
ErrSessionClosed = errors.New("session Already Closed")
ErrSessionBlocked = errors.New("session Full Blocked")
ErrMsgTooLong = errors.New("Message Too Long")
)
type Session interface {
Connection
Reset()
Conn() net.Conn
Stat() string
IsClosed() bool
SetMaxMsgLen(int)
SetName(string)
SetEventListener(EventListener)
SetPkgHandler(ReadWriter)
SetReader(Reader)
SetWriter(Writer)
SetCronPeriod(int)
SetRQLen(int)
SetWQLen(int)
SetWaitTime(time.Duration)
GetAttribute(interface{}) interface{}
SetAttribute(interface{}, interface{})
RemoveAttribute(interface{})
// the Writer will invoke this function.
// for udp session, the first parameter should be UDPContext. Otherwise its type is []byte.
WritePkg(interface{}, time.Duration) error
WriteBytes([]byte) error
WriteBytesArray(...[]byte) error
Close()
}
/////////////////////////////////////////
// EndPoint interfacke
/////////////////////////////////////////
type EndPoint interface {
// get endpoint type
Type() EndPointType
// run event loop
RunEventLoop(newSession NewSessionCallback)
// check the endpoint has been closed
IsClosed() bool
// close the endpoint and free its resource
Close()
}
......@@ -74,5 +167,6 @@ type Client interface {
type Server interface {
EndPoint
// get the network listener
Listener() net.Listener
}
......@@ -11,7 +11,6 @@ package getty
import (
"bytes"
"errors"
"fmt"
"net"
"runtime"
......@@ -47,45 +46,9 @@ const (
/////////////////////////////////////////
var (
ErrSessionClosed = errors.New("session Already Closed")
ErrSessionBlocked = errors.New("session Full Blocked")
ErrMsgTooLong = errors.New("Message Too Long")
)
var (
wheel = gxtime.NewWheel(gxtime.TimeMillisecondDuration(100), 1200) // wheel longest span is 2 minute
)
type Session interface {
Connection
Reset()
Conn() net.Conn
Stat() string
IsClosed() bool
SetMaxMsgLen(int)
SetName(string)
SetEventListener(EventListener)
SetPkgHandler(ReadWriter)
SetReader(Reader)
SetWriter(Writer)
SetCronPeriod(int)
SetRQLen(int)
SetWQLen(int)
SetWaitTime(time.Duration)
GetAttribute(interface{}) interface{}
SetAttribute(interface{}, interface{})
RemoveAttribute(interface{})
// the Writer will invoke this function.
// for udp session, the first parameter should be UDPContext. Otherwise its type is []byte.
WritePkg(interface{}, time.Duration) error
WriteBytes([]byte) error
WriteBytesArray(...[]byte) error
Close()
}
// getty base session
type session struct {
name string
......
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