gpip

package
Version: v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: Apache-2.0 Imports: 7 Imported by: 2

README

package gpip

Description

gpip is a simple tool for frame message transfer based on tcp. It can transfer text message and byte content such as file with very easy way.

Usage

Server side:

func main() {
	listener, e1 := net.Listen("tcp", ":8080")
	if e1 != nil {
		panic(e1)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			panic(err)
		}
		go serverHandler(conn)
	}
}


func serverHandler(conn net.Conn) {
	pip := &gpip.Pip{
		Conn: conn,
	}
	defer pip.Close()
	for {
		err := pip.Receive(&common.Header{}, func(_header interface{}, bodyReader io.Reader, bodyLength int64) error {
			header := _header.(*common.Header)
			bs, _ := json.Marshal(header)
			log.Info("server got message:", string(bs))
			return pip.Send(&common.Header{
				Code: 200,
				Attribute: map[string]string{"Result":"success"},
			}, nil, 0)
		})
		if err != nil {
			log.Error("error receive data:", err)
			break
		}
	}
}

Client side:

func main() {
	conn, err := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(common.ServerPort))
	if err != nil {
		log.Fatal("error start client:", err)
	}
	pip := &gpip.Pip{
		Conn: conn,
	}
	defer pip.Close()
	err := pip.Send(&common.Meta{
		Code: 1,
		Attribute: nil,
	}, nil, 0)
	if err != nil {
		log.Fatal("error send data:", err)
		break
	}
	err = pip.Receive(&common.Header{}, func(_header interface{}, bodyReader io.Reader, bodyLength int64) error {
		/*header := _header.(*common.Header)
		bs, _ := json.Marshal(header)
		log.Info("client got message:", string(bs))*/
		return nil
	})
	if err != nil {
		log.Error("error:", err)
	}
}

Documentation

Overview

Copyright (C) 2019 tisnyo <tisnyo@gmail.com>.

A Pip can used for frame data transfer. It can transfer text header content and byte content such as file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeserializeFromObject

func DeserializeFromObject(data []byte, obj interface{}) error

DeserializeFromObject deserialize an byte array to an interface by type.

func DeserializeFromType

func DeserializeFromType(data []byte, p reflect.Type) (interface{}, error)

DeserializeFromType deserialize an byte array to an interface by type.

func Serialize

func Serialize(obj interface{}) ([]byte, error)

Serialize serialize an interface to json.

Types

type Pip

type Pip struct {
	Conn net.Conn
}

Pip is a tcp pipe manager which controls the reception and sending of frames.

func (*Pip) Close

func (pip *Pip) Close()

Close close the Pip.

func (*Pip) Receive

func (pip *Pip) Receive(
	headerObject interface{},
	handler func(filledHeaderObject interface{}, bodyReader io.Reader, bodyLength int64) error,
) error

Receive receives a frame by it's pip. headerObject is an interface which will be used to load header data, handler is a callback function with which you can handle a data frame, parameter filledMetaObject of function handler and headerObject are the same object. filledHeaderObject is data-filled header Object.

func (*Pip) Send

func (pip *Pip) Send(header interface{}, bodyReader io.Reader, bodyLength int64) error

Send sends a frame by it's pip.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL