tcp

package module
v1.2.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2024 License: MIT Imports: 16 Imported by: 0

README

TCP Socket Server

Socket是网络编程的一个抽象概念。通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可。

大多数连接都是可靠的TCP连接。创建TCP连接时,主动发起连接的叫客户端,被动响应连接的叫服务器。

举个例子,当我们在浏览器中访问新浪时,我们自己的计算机就是客户端,浏览器会主动向新浪的服务器发起连接。如果一切顺利,新浪的服务器接受了我们的连接,一个TCP连接就建立起来的,后面的通信就是发送网页内容了。

Documentation

Index

Constants

View Source
const (
	KindTcp kratosTransport.Kind = "tcp"
)

Variables

This section is empty.

Functions

func LogDebug

func LogDebug(args ...interface{})

func LogDebugf

func LogDebugf(format string, args ...interface{})

func LogError

func LogError(args ...interface{})

func LogErrorf

func LogErrorf(format string, args ...interface{})

func LogFatal

func LogFatal(args ...interface{})

func LogFatalf

func LogFatalf(format string, args ...interface{})

func LogInfo

func LogInfo(args ...interface{})

func LogInfof

func LogInfof(format string, args ...interface{})

func LogWarn

func LogWarn(args ...interface{})

func LogWarnf

func LogWarnf(format string, args ...interface{})

func RegisterClientMessageHandler

func RegisterClientMessageHandler[T any](cli *Client, messageType MessageType, handler func(*T) error)

func RegisterServerMessageHandler

func RegisterServerMessageHandler[T any](srv *Server, messageType MessageType, handler func(SessionID, *T) error)

func SetOperation

func SetOperation(ctx context.Context, op string)

SetOperation sets the transport operation.

func WithBigEndian added in v1.2.12

func WithBigEndian()

WithBigEndian set big endian

func WithLittleEndian added in v1.2.12

func WithLittleEndian()

WithLittleEndian set little endian

Types

type Any

type Any interface{}

type Binder

type Binder func() Any

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(opts ...ClientOption) *Client

func (*Client) Connect

func (c *Client) Connect() error

func (*Client) DeregisterMessageHandler

func (c *Client) DeregisterMessageHandler(messageType MessageType)

func (*Client) Disconnect

func (c *Client) Disconnect()

func (*Client) RegisterMessageHandler

func (c *Client) RegisterMessageHandler(messageType MessageType, handler ClientMessageHandler, binder Binder)

func (*Client) SendMessage

func (c *Client) SendMessage(messageType int, message interface{}) error

func (*Client) SendRawData

func (c *Client) SendRawData(message []byte) error

type ClientHandlerData

type ClientHandlerData struct {
	Handler ClientMessageHandler
	Binder  Binder
}

type ClientMessageHandler

type ClientMessageHandler func(MessagePayload) error

type ClientMessageHandlerMap

type ClientMessageHandlerMap map[MessageType]ClientHandlerData

type ClientOption

type ClientOption func(o *Client)

func WithClientCodec

func WithClientCodec(codec string) ClientOption

func WithClientRawDataHandler

func WithClientRawDataHandler(h ClientRawMessageHandler) ClientOption

func WithEndpoint

func WithEndpoint(uri string) ClientOption

type ClientRawMessageHandler

type ClientRawMessageHandler func([]byte) error

type ConnectHandler

type ConnectHandler func(SessionID, bool)

type HandlerData

type HandlerData struct {
	Handler MessageHandler
	Binder  Binder
}

type Message

type Message struct {
	Type MessageType
	Body []byte
}

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

func (*Message) Unmarshal

func (m *Message) Unmarshal(buf []byte) error

type MessageHandler

type MessageHandler func(SessionID, MessagePayload) error

type MessageHandlerMap

type MessageHandlerMap map[MessageType]HandlerData

type MessagePayload

type MessagePayload Any

type MessageType

type MessageType uint32

type RawMessageHandler

type RawMessageHandler func(SessionID, []byte) (err error, msgType MessageType, msgBody []byte)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(opts ...ServerOption) *Server

func (*Server) Broadcast

func (s *Server) Broadcast(messageType MessageType, message MessagePayload)

func (*Server) BroadcastRawData

func (s *Server) BroadcastRawData(message []byte)

func (*Server) DeregisterMessageHandler

func (s *Server) DeregisterMessageHandler(messageType MessageType)

func (*Server) Endpoint

func (s *Server) Endpoint() (*url.URL, error)

func (*Server) GetMessageHandler added in v1.2.12

func (s *Server) GetMessageHandler(msgType MessageType) (error, *HandlerData)

GetMessageHandler find message handler

func (*Server) HandleMessage added in v1.2.12

func (s *Server) HandleMessage(sessionId SessionID, msgType MessageType, msgBody []byte) error

func (*Server) Name

func (s *Server) Name() string

func (*Server) RegisterMessageHandler

func (s *Server) RegisterMessageHandler(messageType MessageType, handler MessageHandler, binder Binder)

func (*Server) SendMessage

func (s *Server) SendMessage(sessionId SessionID, messageType MessageType, message MessagePayload) error

func (*Server) SendRawData

func (s *Server) SendRawData(sessionId SessionID, message []byte) error

SendRawData send raw data to client

func (*Server) SessionCount

func (s *Server) SessionCount() int

func (*Server) Start

func (s *Server) Start(_ context.Context) error

func (*Server) Stop

func (s *Server) Stop(_ context.Context) error

type ServerOption

type ServerOption func(o *Server)

func WithAddress

func WithAddress(addr string) ServerOption

func WithChannelBufferSize

func WithChannelBufferSize(size int) ServerOption

func WithCodec

func WithCodec(c string) ServerOption

func WithConnectHandler

func WithConnectHandler(h ConnectHandler) ServerOption

func WithRawDataHandler

func WithRawDataHandler(h RawMessageHandler) ServerOption

func WithRecvBufferSize

func WithRecvBufferSize(size int) ServerOption

func WithTLSConfig

func WithTLSConfig(c *tls.Config) ServerOption

func WithTimeout

func WithTimeout(timeout time.Duration) ServerOption

type Session

type Session struct {
	// contains filtered or unexported fields
}

func NewSession

func NewSession(conn net.Conn, server *Server) *Session

func (*Session) Close

func (c *Session) Close()

func (*Session) Conn

func (c *Session) Conn() net.Conn

func (*Session) Listen

func (c *Session) Listen()

func (*Session) SendMessage

func (c *Session) SendMessage(message []byte)

func (*Session) SessionID

func (c *Session) SessionID() SessionID

type SessionID

type SessionID string

type SessionMap

type SessionMap map[SessionID]*Session

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is a tcp transport.

func (*Transport) Endpoint

func (tr *Transport) Endpoint() string

Endpoint returns the transport endpoint.

func (*Transport) Kind

func (tr *Transport) Kind() kratosTransport.Kind

Kind returns the transport kind.

func (*Transport) Operation

func (tr *Transport) Operation() string

Operation returns the transport operation.

func (*Transport) PathTemplate

func (tr *Transport) PathTemplate() string

PathTemplate returns the http path template.

func (*Transport) ReplyHeader

func (tr *Transport) ReplyHeader() kratosTransport.Header

ReplyHeader returns the reply header.

func (*Transport) Request

func (tr *Transport) Request() *http.Request

Request returns the HTTP request.

func (*Transport) RequestHeader

func (tr *Transport) RequestHeader() kratosTransport.Header

RequestHeader returns the request header.

type Transporter

type Transporter interface {
	kratosTransport.Transporter
	Request() *http.Request
	PathTemplate() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL