safetcpack

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

README

safetcpack

English | 简体中文

safetcpack provides a thread-safe version of tcpack.

Difference from tcpack

Unlike tcpack, with safetcpack, you can construct multiple packers for a TCP connection and use them concurrently in multiple goroutines.

Notice: Using of multiple packagers based on the same TCP connection to send and receive messages concurrently can result in uncertain order of messages sent and received. If you want to ensure that messages are sent and received in order, please use tcpack and avoid concurrency.

Installation Guidelines

  1. To install the safetcpack package, you first need to have Go installed, then you can use the command below to add safetcpack as a dependency in your Go program.
go get -u github.com/lim-yoona/tcpack
  1. Import it in your code:
import safetcpack "github.com/lim-yoona/tcpack/safe"

Usage

The usage of safetcpack is consistent with that of tcpack.

Examples

Here are some Examples.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SafeMsgPack

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

SafeMsgPack implements the interface IMsgPack, carrying HeadLen and conn for Pack() and Unpack(), and mutex for concurrent Pack() and Unpack().

func NewSafeMsgPack

func NewSafeMsgPack(headleng uint32, conn net.Conn) *SafeMsgPack

NewSafeMsgPack returns a thread-safe packager *SafeMsgPack. NewSafeMsgPack returns the same packager for the same TCP connection, so the value of the headLen is consistent with the first time you new a SafeMsgPack.

func (*SafeMsgPack) GetHeadLen

func (smp *SafeMsgPack) GetHeadLen() uint32

GetHeadLen return headLen of the message.

func (*SafeMsgPack) Pack

func (smp *SafeMsgPack) Pack(msg tcpack.Imessage) (uint32, error)

Pack packs a message to bytes stream and sends it.

func (*SafeMsgPack) Unpack

func (smp *SafeMsgPack) Unpack() (tcpack.Imessage, error)

Unpack unpacks a certain length bytes stream to a message.

Jump to

Keyboard shortcuts

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