peer

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: Unlicense Imports: 9 Imported by: 0

Documentation

Overview

Package peer messages are sent to peers over a P connection (TCP). Only a single active connection to a peer is allowed.

Index

Constants

View Source
const ConnectionType soul.ConnectionType = "P"

ConnectionType represents the type of peer 'P' connection.

Variables

View Source
var ErrBanned = errors.New("Banned")

ErrBanned is returned when the transfer is not allowed because the peer is banned.

View Source
var ErrCancelled = errors.New("Cancelled")

ErrCancelled is returned when the transfer is not allowed because it was cancelled.

View Source
var ErrComplete = errors.New("Complete")

ErrComplete is returned when the transfer is complete.

View Source
var ErrEmptyDirectory = errors.New("directory is empty")

ErrEmptyDirectory is returned when the directory is empty.

View Source
var ErrEmptyDirectoryName = errors.New("directory name is empty")

ErrEmptyDirectoryName is returned when the directory name is empty.

View Source
var ErrEmptyFileExtension = errors.New("file extension is empty")

ErrEmptyFileExtension is returned when the file extension is empty.

View Source
var ErrEmptyFileName = errors.New("file name is empty")

ErrEmptyFileName is returned when the file name is empty.

View Source
var ErrFileNotShared = errors.New("File not shared.")

ErrFileNotShared is returned when the transfer is not allowed because the file is not shared.

View Source
var ErrFileReadError = errors.New("File read error.")

ErrFileReadError is returned when the transfer is not allowed because of a file read error.

View Source
var ErrNoDirectories = errors.New("no directories")

ErrNoDirectories is returned when there are no directories.

View Source
var ErrNotAllowedWithNoReason = errors.New("rejection reason is required when transfer is not allowed")

ErrNotAllowedWithNoReason is returned when a TransferResponse is not allowed and no reason is provided.

View Source
var ErrPendingShutdown = errors.New("Pending shutdown.")

ErrPendingShutdown is returned when the transfer is not allowed because of a pending shutdown.

View Source
var ErrQueued = errors.New("Queued")

ErrQueued is returned when the transfer is queued.

View Source
var ErrSizeZero = errors.New("file size is zero")

ErrSizeZero is returned when the file size is zero.

View Source
var ErrTooManyFiles = errors.New("Too many files")

ErrTooManyFiles is returned when the transfer is not allowed because there are too many files.

View Source
var ErrTooManyMegabytes = errors.New("Too many megabytes")

ErrTooManyMegabytes is returned when the transfer is not allowed because there are too many megabytes.

Functions

func MessageRead added in v1.1.0

func MessageRead[C CodeInit | Code](c C, connection net.Conn) (io.Reader, int, C, error)

func MessageWrite added in v1.1.0

func MessageWrite(connection net.Conn, message []byte) (int, error)

func Reason added in v1.1.0

func Reason(reason string) error

Types

type Attribute

type Attribute struct {
	Code  FileAttributeType
	Value uint32
}

Attribute is a type of file attribute.

type Code added in v1.0.0

type Code int

Code Peer messages are sent to peers over a P connection (TCP). Only a single active connection to a peer is allowed.

const CodeFileSearchResponse Code = 9
const CodeFolderContentsRequest Code = 36
const CodeFolderContentsResponse Code = 37
const CodePlaceInQueueRequest Code = 51
const CodePlaceInQueueResponse Code = 44
const CodeQueueUpload Code = 43
const CodeSharedFileListRequest Code = 4
const CodeSharedFileListResponse Code = 5
const CodeTransferRequest Code = 40
const CodeTransferResponse Code = 41
const CodeUploadDenied Code = 50
const CodeUploadFailed Code = 46
const CodeUserInfoRequest Code = 15
const CodeUserInfoResponse Code = 16

func (Code) String added in v1.0.0

func (i Code) String() string

type CodeInit added in v1.0.0

type CodeInit int

CodeInit Peer init messages are used to initiate a P, F or D connection (TCP) to a peer.

const CodePeerInit CodeInit = 1
const CodePierceFirewall CodeInit = 0

func (CodeInit) String added in v1.0.0

func (i CodeInit) String() string

type Directory

type Directory struct {
	Name  string
	Files []File
}

Directory is a directory in a shared file list.

type File

type File struct {
	Name       string
	Size       uint64
	Extension  string
	Attributes []Attribute
}

File is a file in a directory.

type FileAttributeType added in v0.0.2

type FileAttributeType int

FileAttributeType represents the type of file attribute.

const (
	// Bitrate (kbps).
	Bitrate FileAttributeType = iota
	// Duration (seconds).
	Duration
	// VBR (0 or 1).
	VBR

	// SampleRate (Hz).
	SampleRate
	// BitDepth (bits).
	BitDepth
)

func (FileAttributeType) String added in v1.0.0

func (i FileAttributeType) String() string

type FileSearchResponse added in v1.0.0

type FileSearchResponse struct {
	Username       string
	Token          soul.Token
	Results        []File
	FreeSlot       bool
	AverageSpeed   int
	Queue          int // Queue is the length of the queued transfers.
	PrivateResults []File
}

FileSearchResponse code 9 peer sends this message when it has a file search match. The token is taken from original FileSearch, UserSearch or RoomSearch server message.

func (*FileSearchResponse) Deserialize added in v1.0.0

func (f *FileSearchResponse) Deserialize(reader io.Reader) error

func (FileSearchResponse) Serialize added in v1.0.0

func (f FileSearchResponse) Serialize(fs FileSearchResponse) ([]byte, error)

Serialize accepts a FileSearchResponse and returns a message packed as a byte slice.

type Folder added in v1.0.0

type Folder struct {
	Directory string
	Files     []File
}

Folder represents a folder and its contents.

type FolderContentsRequest added in v1.0.0

type FolderContentsRequest struct {
	Token  soul.Token
	Folder string
}

FolderContentsRequest code 36 we ask the peer to send us the contents of a single folder.

func (*FolderContentsRequest) Deserialize added in v1.0.0

func (f *FolderContentsRequest) Deserialize(reader io.Reader) error

func (FolderContentsRequest) Serialize added in v1.0.0

func (FolderContentsRequest) Serialize(token soul.Token, folder string) ([]byte, error)

type FolderContentsResponse added in v1.0.0

type FolderContentsResponse struct {
	Token   soul.Token
	Folder  string
	Folders []Folder
}

FolderContentsResponse code 37 peer responds with the contents of a particular folder (with all subfolders) after we’ve sent a FolderContentsRequest.

func (*FolderContentsResponse) Deserialize added in v1.0.0

func (f *FolderContentsResponse) Deserialize(reader io.Reader) error

func (FolderContentsResponse) Serialize added in v1.0.0

func (FolderContentsResponse) Serialize(token soul.Token, folder string, folders []Folder) ([]byte, error)

type PeerInit added in v0.0.2

type PeerInit struct {
	// RemoteUsername is the username of the peer that wants to connect to us.
	RemoteUsername string
	ConnectionType soul.ConnectionType
}

PeerInit code 1 message is sent to initiate a direct connection to another peer.

func (*PeerInit) Deserialize added in v0.0.2

func (p *PeerInit) Deserialize(reader io.Reader) error

func (PeerInit) Serialize added in v0.0.2

func (PeerInit) Serialize(ownUsername string, connType soul.ConnectionType) ([]byte, error)

type PierceFirewall added in v0.0.2

type PierceFirewall struct {
	Token soul.Token
}

PierceFirewall code 0 message is sent in response to an indirect connection request from another user. If the message goes through to the user, the connection is ready. The token is taken from the ConnectToPeer server message.

func (*PierceFirewall) Deserialize added in v0.0.2

func (p *PierceFirewall) Deserialize(reader io.Reader) error

func (PierceFirewall) Serialize added in v0.0.2

func (PierceFirewall) Serialize(token soul.Token) ([]byte, error)

type PlaceInQueueRequest added in v1.0.0

type PlaceInQueueRequest struct {
	Filename string
}

PlaceInQueueRequest code 51 message is sent when asking for the upload queue placement of a file.

func (*PlaceInQueueRequest) Deserialize added in v1.0.0

func (p *PlaceInQueueRequest) Deserialize(reader io.Reader) error

func (PlaceInQueueRequest) Serialize added in v1.0.0

func (PlaceInQueueRequest) Serialize(filename string) ([]byte, error)

type PlaceInQueueResponse added in v1.0.0

type PlaceInQueueResponse struct {
	Filename string
	Place    uint32
}

PlaceInQueueResponse code 44 peer replies with the upload queue placement of the requested file.

func (*PlaceInQueueResponse) Deserialize added in v1.0.0

func (p *PlaceInQueueResponse) Deserialize(reader io.Reader) error

func (PlaceInQueueResponse) Serialize added in v1.0.0

func (PlaceInQueueResponse) Serialize(filename string, place uint32) ([]byte, error)

type QueueUpload added in v1.0.0

type QueueUpload struct {
	Filename string
}

QueueUpload code 43 this message is used to tell a peer that an upload should be queued on their end. Once the recipient is ready to transfer the requested file, they will send a TransferRequest to us.

func (*QueueUpload) Deserialize added in v1.0.0

func (q *QueueUpload) Deserialize(reader io.Reader) error

func (QueueUpload) Serialize added in v1.0.0

func (QueueUpload) Serialize(filename string) ([]byte, error)

type SharedFileListRequest

type SharedFileListRequest struct{}

func (*SharedFileListRequest) Deserialize

func (s *SharedFileListRequest) Deserialize(reader io.Reader) error

func (SharedFileListRequest) Serialize

func (SharedFileListRequest) Serialize() ([]byte, error)

type SharedFileListResponse

type SharedFileListResponse struct {
	Directories        []Directory
	PrivateDirectories []Directory
}

SharedFileListResponse code 5 peer responds with a list of shared files after we’ve sent a SharedFileListRequest.

func (*SharedFileListResponse) Deserialize

func (s *SharedFileListResponse) Deserialize(reader io.Reader) error

Deserialize accepts a reader and deserializes the message into the SharedFileListResponse struct.

func (SharedFileListResponse) Serialize

func (s SharedFileListResponse) Serialize(directories, privateDirectories []Directory) ([]byte, error)

Serialize accepts directories and privateDirectories and returns a message packed as a byte slice. It uses custom errors for the following cases: - ErrNoDirectories is returned when there are no directories. - ErrEmptyDirectoryName is returned when the directory name is empty. - ErrEmptyDirectory is returned when the directory is empty. - ErrEmptyFileName is returned when the file name is empty. - ErrSizeZero is returned when the file size is zero. - ErrEmptyFileExtension is returned when the file extension is empty. You can use them in your code to check for specific errors.

type TransferDirection added in v0.0.2

type TransferDirection int

TransferDirection represents the direction of a file transfer.

const (
	// DownloadFromPeer transfer direction.
	DownloadFromPeer TransferDirection = iota
	// UploadToPeer transfer direction.
	UploadToPeer
)

func (TransferDirection) String added in v1.0.0

func (i TransferDirection) String() string

type TransferRequest added in v1.0.0

type TransferRequest struct {
	Direction TransferDirection
	Token     soul.Token
	Filename  string
	FileSize  uint64
}

TransferRequest code 40 this message is sent by a peer once they are ready to start uploading a file to us. A TransferResponse message is expected from the recipient, either allowing or rejecting the upload attempt.

This message was formerly used to send a download request (direction 0) as well, but Nicotine+ >= 3.0.3, Museek+ and the official clients use the QueueUpload peer message for this purpose today.

func (*TransferRequest) Deserialize added in v1.0.0

func (t *TransferRequest) Deserialize(reader io.Reader) error

func (TransferRequest) Serialize added in v1.0.0

func (TransferRequest) Serialize(direction TransferDirection, token soul.Token, filename string, filesize uint64) ([]byte, error)

type TransferResponse added in v1.0.0

type TransferResponse struct {
	Token   soul.Token
	Allowed bool
	Reason  error
}

TransferResponse code 41 response to TransferRequest. We (or the other peer) either agrees, or tells the reason for rejecting the file upload.

func (*TransferResponse) Deserialize added in v1.0.0

func (t *TransferResponse) Deserialize(reader io.Reader) error

func (TransferResponse) Serialize added in v1.0.0

func (TransferResponse) Serialize(token soul.Token, allowed bool, reason ...error) ([]byte, error)

Serialize accepts a TransferResponse and returns a message packed as a byte slice. If the transfer is not allowed, a reason must be provided. The possible errors are: ErrBanned, ErrCancelled, ErrComplete, ErrFileNotShared, ErrFileReadError, ErrPendingShutdown, ErrQueued, ErrTooManyFiles, ErrTooManyMegabytes, and ErrNotAllowedWithNoReason. All errors exist in the peer.TransferResponse package.

type UploadDenied added in v1.0.0

type UploadDenied struct {
	Filename string
	Reason   error
}

UploadDenied code 50 this message is sent to reject QueueUpload attempts and previously queued files. The reason for rejection will appear in the transfer list of the recipient.

func (*UploadDenied) Deserialize added in v1.0.0

func (u *UploadDenied) Deserialize(reader io.Reader) error

func (UploadDenied) Serialize added in v1.0.0

func (UploadDenied) Serialize(filename string, reason error) ([]byte, error)

type UploadFailed added in v1.0.0

type UploadFailed struct {
	Filename string
}

UploadFailed code 46 this message is sent whenever a file connection of an active upload closes. Soulseek NS clients can also send this message when a file cannot be read. The recipient either re-queues the upload (download on their end), or ignores the message if the transfer finished.

func (*UploadFailed) Deserialize added in v1.0.0

func (u *UploadFailed) Deserialize(reader io.Reader) error

func (UploadFailed) Serialize added in v1.0.0

func (UploadFailed) Serialize(filename string) ([]byte, error)

type UploadPermission added in v0.0.2

type UploadPermission int

UploadPermission represents the permission level for uploading files.

const (
	// NoOne permission level.
	NoOne UploadPermission = iota
	// Everyone permission level.
	Everyone
	// UsersInList permission level.
	UsersInList
	// PermittedUsers permission level.
	PermittedUsers
)

func (UploadPermission) String added in v1.0.0

func (i UploadPermission) String() string

type UserInfoRequest added in v1.0.0

type UserInfoRequest struct{}

func (*UserInfoRequest) Deserialize added in v1.0.0

func (u *UserInfoRequest) Deserialize(reader io.Reader) error

func (UserInfoRequest) Serialize added in v1.0.0

func (UserInfoRequest) Serialize() ([]byte, error)

type UserInfoResponse added in v1.0.0

type UserInfoResponse struct {
	Description     string
	Picture         []byte
	TotalUpload     uint32
	QueueSize       uint32
	FreeSlots       bool
	UploadPermitted UploadPermission
}

func (*UserInfoResponse) Deserialize added in v1.0.0

func (u *UserInfoResponse) Deserialize(reader io.Reader) error

func (UserInfoResponse) Serialize added in v1.0.0

func (UserInfoResponse) Serialize(u UserInfoResponse) ([]byte, error)

Jump to

Keyboard shortcuts

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