syncnet

package
v0.0.0-...-82860ec Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const SizeLength = 8

SizeLength is the length that the packet size information occupies in the protocol header

View Source
const TypeLength = 1

TypeLength is the length that the packet type information occupies in the protocol header

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrAuthFailed

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

ErrAuthFailed is returned when the authentication fails

func (ErrAuthFailed) Error

func (e ErrAuthFailed) Error() string

Error returns the error message

type ErrInvalidPacket

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

ErrInvalidPacket is returned when the packet length is invalid

func (ErrInvalidPacket) Error

func (e ErrInvalidPacket) Error() string

Error returns the error message

type ErrInvalidPacketType

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

ErrInvalidPacketType is returned when the packet type is invalid

func (ErrInvalidPacketType) Error

func (e ErrInvalidPacketType) Error() string

Error returns the error message

type ErrUnexpectedPacketType

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

ErrUnexpectedPacketType is returned when the packet type is unexpected

func (ErrUnexpectedPacketType) Error

func (e ErrUnexpectedPacketType) Error() string

Error returns the error message

type IdleTimeoutConn

type IdleTimeoutConn struct {
	// TCPConn is the underlying TCP connection
	*net.TCPConn
	// IdleTimeout is the timeout for idle connection
	IdleTimeout time.Duration
}

IdleTimeoutConn is the connection with idle timeout

func (*IdleTimeoutConn) Log

func (c *IdleTimeoutConn) Log(v ...any)

Log is the logger for the connection

func (*IdleTimeoutConn) Read

func (c *IdleTimeoutConn) Read(b []byte) (int, error)

Read reads data from the connection The timeout is set for each read operation

func (*IdleTimeoutConn) Write

func (c *IdleTimeoutConn) Write(b []byte) (int, error)

Write writes data to the connection The timeout is set for each write operation

type PacketType

type PacketType uint8

PacketType is the type of packet

const (
	// ACK Acknowledgement packet
	ACK PacketType = iota
	// AUTH Authentication packet
	AUTH
	// REPLY packet for authentication
	REPLY
	// PING Ping packet
	PING
	// PONG Pong packet
	PONG
	// FILE packet
	FILE
	// SYNC packet
	SYNC
	META
	BYE
)

type SyncatAckRequest

type SyncatAckRequest struct {
	SyncatRequestHeader
}

SyncatAckRequest is the request for ACK packet

func NewSyncatAckRequest

func NewSyncatAckRequest() *SyncatAckRequest

NewSyncatAckRequest Create a new SyncatAckRequest

func (*SyncatAckRequest) Handle

func (r *SyncatAckRequest) Handle(_ *IdleTimeoutConn) error

Handle ACK request does not need to be handled, the function is empty

type SyncatAuthRequest

type SyncatAuthRequest struct {
	SyncatRequestHeader
	pb.SyncatAuthRequestBody
}

SyncatAuthRequest is the request for AUTH packet

func NewSyncatAuthRequest

func NewSyncatAuthRequest() (*SyncatAuthRequest, error)

NewSyncatAuthRequest Create a new SyncatAuthRequest

func (*SyncatAuthRequest) Handle

func (r *SyncatAuthRequest) Handle(conn *IdleTimeoutConn) error

Handle AUTH request Authenticate the token and check whether the client is registered If the client is not registered, auth will also fail If the client field is empty, the client will be registered REPLY packet will be sent back as response AUTH request will only be sent by the client to the server when the connection is established

func (*SyncatAuthRequest) Send

func (r *SyncatAuthRequest) Send(conn *IdleTimeoutConn) error

Send the AUTH request

type SyncatReplyRequest

type SyncatReplyRequest struct {
	SyncatRequestHeader
	pb.SyncatReplyRequestBody
}

SyncatReplyRequest is the request for REPLY packet

func NewSyncatReplyRequest

func NewSyncatReplyRequest(success bool, clientUUid string, message string) *SyncatReplyRequest

NewSyncatReplyRequest Create a new SyncatReplyRequest

func (*SyncatReplyRequest) Handle

func (r *SyncatReplyRequest) Handle(conn *IdleTimeoutConn) error

Handle REPLY request Check whether the auth is successful, and also update the client's uuid when newly registered REPLY request will only be sent by the server to the client when the connection is established

func (*SyncatReplyRequest) Send

func (r *SyncatReplyRequest) Send(conn *IdleTimeoutConn) error

Send the REPLY request

type SyncatRequest

type SyncatRequest interface {
	// GetType Get the type of the request
	GetType() PacketType
	// GetLength Get the length of the request
	GetLength() uint64
	// Handle the request
	Handle(conn *IdleTimeoutConn) error
	// Send the request
	Send(conn *IdleTimeoutConn) error
}

SyncatRequest is the interface for all syncat request

func RouteConn

func RouteConn(conn *IdleTimeoutConn) (SyncatRequest, error)

RouteConn wait for the next packet, parse the request header and identify which type of request it is

func Wait

func Wait(conn *IdleTimeoutConn, typeList []PacketType) (SyncatRequest, error)

Wait for the certain kinds of packet, provided by typeList

type SyncatRequestHeader

type SyncatRequestHeader struct {
	// PacketType is the type of the packet
	PacketType PacketType
	// Length is the length of the packet
	Length uint64
}

SyncatRequestHeader is the header of all syncat request

func (*SyncatRequestHeader) GetLength

func (r *SyncatRequestHeader) GetLength() uint64

GetLength Get the length of the request from header info, default method for all request

func (*SyncatRequestHeader) GetType

func (r *SyncatRequestHeader) GetType() PacketType

GetType Get the type of the request from header info, default method for all request

func (*SyncatRequestHeader) Send

func (r *SyncatRequestHeader) Send(conn *IdleTimeoutConn) error

Send the request based on configured header info

Jump to

Keyboard shortcuts

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