README ¶
scdt
SCDT(single connection data transmission) is a package of tcp data transmission
It use TCP to establish a one-way channel And make both ends free to send and receive data
server:
l, err := NewListener("0.0.0.0:12345")
if err != nil {
panic(err)
}
//send some data to client and wait success
l.RangeConnections(f func(id string, connection Connection){
l.SendTo(id, []byte("hello"), func(id string, message *Message){
//do something
})
l.SendTo(id, []byte("hello"), func(id string, message *Message){
//do something
})
l.SendCustomTo(id, 0x02, []byte("hello"), f func(id string, message *Message){
//do something
})
})
//wait
time.Sleep(30*time.Minute)
//stop
l.Stop()
client
dial, err := net.Dial("tcp", "localhost:12345")
if err != nil {
log.Errorw("dail error", "err", err)
}
connect := Connect(dial)
//send some data to server and wait success
msg, b := connect.SendOnWait([]byte("hello"))
if b {
fmt.Printf("waited send message:%+v,data:%s\n", msg, msg.Data)
}
//send some data to server and wait success callback
queue,ok:=connect.SendWithCallback([]byte("hello"), func(message *Message) {
fmt.Printf("send message:%+v,data:%s\n", message, message.Data)
})
//send some data to server
queue,ok:=connect.Send([]byte("hello"))
if ok {
//get the response message
msg:=queue.Wait()
if msg!=nil{
//data was here
fmt.Println(string(msg.Data))
}
}
//called when recv some message
connect.Recv(func(message *Message) ([]byte, error) {
fmt.Printf("recv message:%+v,data:%s\n", id, message.Data)
return nil, nil
})
client with custom data
//send some data to server with a custom id and wait success callback
queue,ok:=connect.SendCustomDataWithCallback(0x01,[]byte("hello"), func(message *Message) {
fmt.Printf("send message:%+v,data:%s\n", message, message.Data)
})
//called when recv some message
connect.RecvCustomData(func(message *Message) ([]byte, error) {
fmt.Printf("recv message:%+v,data:%s\n", id, message.Data)
return nil, nil
})
Documentation ¶
Index ¶
- Variables
- func ScanExchange(scanner *bufio.Scanner, packer ReadPacker) error
- func UUID() string
- type Config
- type ConfigFunc
- type Connection
- type CustomID
- type DataLength
- type Extension
- type HandleRecvFunc
- type Listener
- type Message
- func (m *Message) Error() error
- func (m Message) Pack(writer io.Writer) (err error)
- func (m *Message) RequestType() Type
- func (m *Message) SetCustomID(id CustomID) *Message
- func (m *Message) SetData(data []byte) *Message
- func (m *Message) SetDataString(data string) *Message
- func (m *Message) SetRequestType(requestType Type) *Message
- func (m *Message) Unpack(reader io.Reader) (err error)
- type MessageCallbackFunc
- type MessageID
- type OnRecvCallbackFunc
- type Queue
- func (q *Queue) NeedCallback() bool
- func (q *Queue) RecvCallback() func(msg *Message)
- func (q *Queue) Session() Session
- func (q *Queue) SetRecvCallback(recvCallback func(msg *Message)) *Queue
- func (q *Queue) SetSendCallback(f func(message *Message)) *Queue
- func (q *Queue) SetTimeout(timeout time.Duration)
- func (q *Queue) Timeout() time.Duration
- func (q *Queue) Wait() *Message
- type ReadPacker
- type ReadWritePacker
- type RecvCallbackFunc
- type RequestStatus
- type SendCallback
- type Session
- type SumLength
- type Type
- type Version
- type WritePacker
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrPing = errors.New("ping remote address failed")
ErrPing ...
Functions ¶
func ScanExchange ¶
func ScanExchange(scanner *bufio.Scanner, packer ReadPacker) error
ScanExchange ...
Types ¶
type Connection ¶
type Connection interface { LocalID() string RemoteID() (string, error) Ping() (string, error) Close() IsClosed() bool NetConn() net.Conn SendQueue(q *Queue) bool Recv(fn RecvCallbackFunc) OnRecv(fn OnRecvCallbackFunc) RecvCustomData(fn RecvCallbackFunc) SendCustomData(id CustomID, data []byte) (*Queue, bool) SendCustomDataOnWait(id CustomID, data []byte) (msg *Message, b bool) SendCustomDataWithCallback(id CustomID, data []byte, cb func(message *Message)) (*Queue, bool) Send(data []byte) (*Queue, bool) SendOnWait(data []byte) (*Message, bool) SendWithCallback(data []byte, cb func(message *Message)) (*Queue, bool) SendClose([]byte) bool }
Connection ...
func NewConnection ¶ added in v0.0.22
func NewConnection(id string, conn net.Conn, cfs ...ConfigFunc) Connection
NewConnection ...
type HandleRecvFunc ¶
HandleRecvFunc ...
type Listener ¶
type Listener interface { Stop() error Conn(id string) (Connection, bool) Listen(network string, lis net.Listener) (b bool) HandleRecv(fn HandleRecvFunc) SendCustomTo(id string, cid CustomID, data []byte, f func(id string, message *Message)) (*Queue, bool) SendTo(id string, data []byte, f func(id string, message *Message)) (*Queue, bool) Range(f func(id string, connection Connection)) }
Listener ...
type Message ¶
type Message struct { MessageID MessageID CustomID CustomID DataLength DataLength Session Session Data []byte // contains filtered or unexported fields }
Message ...
func (*Message) SetDataString ¶
SetDataString ...
func (*Message) SetRequestType ¶
SetRequestType ...
type MessageID ¶
type MessageID uint8
MessageID ...
const ( // MessagePing ... MessagePing MessageID = iota + 1 // MessageHeartBeat ... MessageHeartBeat // MessageIDRequest ... MessageIDRequest // MessageDataTransfer ... MessageDataTransfer // MessageUserCustom ... MessageUserCustom // MessageRecvFailed ... MessageRecvFailed // MessageClose ... MessageClose )
type OnRecvCallbackFunc ¶ added in v0.0.20
OnRecvCallbackFunc ...
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue ...
func (*Queue) RecvCallback ¶ added in v0.0.15
RecvCallback ...
func (*Queue) SetRecvCallback ¶ added in v0.0.15
SetRecvCallback ...
func (*Queue) SetSendCallback ¶
SetSendCallback ...
func (*Queue) SetTimeout ¶
SetTimeout set timeout with time duration
type ReadWritePacker ¶
type ReadWritePacker interface { ReadPacker WritePacker }
ReadWritePacker ...
type RecvCallbackFunc ¶
RecvCallbackFunc ...
type WritePacker ¶
WritePacker ...
Click to show internal directories.
Click to hide internal directories.