p2p

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: GPL-3.0 Imports: 6 Imported by: 4

README

Golang TCP simple client and server

Import

import "github.com/leprosus/golang-p2p"

Create new TCP

tcp := p2p.NewTCP("localhost", 8080)

Server example

package main

import (
	"fmt"
	p2p "github.com/leprosus/golang-p2p"
)

func main() {
	var c int

	tcp := p2p.NewTCP("localhost", 8080)
	err := tcp.Handle(func(req *p2p.Request, res *p2p.Response) {
		fmt.Println(">", string(req.Body))

		c++
		err := res.Send([]byte(fmt.Sprintf("buy %d", c)))
		if err != nil {
			panic(err)
		}
	})
	if err != nil {
		panic(err)
	}
}

Client example

package main

import (
	"fmt"
	p2p "github.com/leprosus/golang-p2p"
)

func main() {
	for i := 0; i < 10; i++ {

		tcp := p2p.NewTCP("localhost", 8080)
		res, err := tcp.Send([]byte("hello"))
		if err != nil {
			panic(err)
		}

		fmt.Println("<", string(res))
	}
}

Running

If you run the server and the client separately then you see:

  • in the server stdout:
> hello
> hello
> hello
> hello
> hello
> hello
> hello
> hello
> hello
> hello
  • in the client stdout:
< buy 1
< buy 2
< buy 3
< buy 4
< buy 5
< buy 6
< buy 7
< buy 8
< buy 9
< buy 10

List all methods

TCP Initialization
  • p2p.NewTCP(host, port) - creates TCP connection
  • p2p.SetTimeout(timeout) - sets timeout on a communication
  • p2p.GetTimeout() - returns current timeout on a communication (30 seconds is default value)
  • p2p.SetRequestLimit(limit) - sets request body limit
  • p2p.GetRequestLimit() - returns request body limit (1024 bytes is default value)
  • p2p.Close() - closes all connections
Server
  • tcp.Handle(handler) - sets func(req *Request, res *Response) to handle requests and send responses
  • req.Body - the request field contains sent data of a client
  • res.Send(bs) - sends bytes back to client
Client
  • tcp.Send(bs) - sends bytes to server

Documentation

Index

Constants

View Source
const (
	DefaultTimeout          = 30 * time.Second
	DefaultRequestLimit int = 1024
)

Variables

View Source
var (
	RequestTooLarge = errors.New("request too large")
)

Functions

This section is empty.

Types

type Request

type Request struct {
	Body []byte
}

type Response

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

func (*Response) Send

func (res *Response) Send(bs []byte) (err error)

type TCP

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

func NewTCP

func NewTCP(host string, port uint) (tcp *TCP)

func (*TCP) Close

func (tcp *TCP) Close() (err error)

func (*TCP) GetRequestLimit

func (tcp *TCP) GetRequestLimit() (limit int)

func (*TCP) GetTimeout

func (tcp *TCP) GetTimeout() (timeout time.Duration)

func (*TCP) Handle

func (tcp *TCP) Handle(handler func(req *Request, res *Response)) (err error)

func (*TCP) Send

func (tcp *TCP) Send(bytes []byte) (result []byte, err error)

func (*TCP) SetRequestLimit

func (tcp *TCP) SetRequestLimit(limit int)

func (*TCP) SetTimeout

func (tcp *TCP) SetTimeout(timeout time.Duration)

Jump to

Keyboard shortcuts

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