dotcp

package module
v0.0.0-...-4c6fcff Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2018 License: MIT Imports: 6 Imported by: 0

README

DoTCP

API documentation Go Report Card

TCP server with RPC mode for JSON. Coverage 83.5%

TCH server for the exchange of JSON dates. Before starting the server, the procedures are registered, which will then be called in a RPC style. The server validates the received data.

The library has two main entities: a server and an client.

Server
  • works in RPC mode
  • valid incoming JSON
Client
  • sends requests to the server

Usage

When using the server, the correct port number is important.

Example

import (
	"fmt"
	"net"

	"github.com/claygod/dotcp"
)

func main() {
	fmt.Println("Begin app")

	s := dotcp.Server(dotcp.NetworkTsp).IP(net.IPv4(127, 0, 0, 1)).Port(9999)
	s.Register("handleArticle", handleArticle, newArticle, schemeArticle)
	s.Start()

	msg := []byte(`{"method": "handleArticle", "query": {"id": 777, "text": "Hello world!"}}`)
	c := dotcp.Client(dotcp.NetworkTsp).IP(net.IPv4(127, 0, 0, 1)).Port(9999)

	res, err := c.Send(msg)

	if err != nil {
		// error handling
		fmt.Println(err)
	} else {
		fmt.Println("Server response:", string(res))
	}

	fmt.Println("End app")
}
Struct (for example)
type Article struct {
	ID   uint   `json:"id"`
	Text string `json:"text"`
}
New struct (for example)
func newArticle() interface{} {
	return &Article{}
}
Scheme (for example)
var schemeArticle string = `{
	"additionalProperties":false,
	"properties": {
		"id": {"type": "integer", "minimum": 0, "maximum": 4294967295},
		"text": {"type": "string", "minLength":2, "maxLength":10}
	},
	"required": ["id", "text"]
}`
Handle (for example)

Blah-blah

func handleArticle(d interface{}) []byte {
	return []byte{77, 77, 77}
}

API

  • Server()
  • Server.IP()
  • Server.Port()
  • Server.Zone()
  • Server.Register()
  • Client()
  • Client.IP()
  • Client.Port()
  • Client.Zone()
  • Client.Send()

F.A.Q.

Why is your server slower than the standard TCP server?

  • For the RPC-realization a JSON is used. The operation of unmarshalling is slow. In addition, the JSON should be validated. This is necessary for safety.

Which ports are best to use?

  • This should be asked from the administrator)) Ports that are used dynamically: 49152 - 65535. Read more on WiKi

ToDo

  • API
  • add example
  • F.A.Q.
  • increase coverage
  • more tests

Bench

i3-5005U (2.0 GHz):

  • BenchmarkSequence-2 50000 26403 ns/op
  • BenchmarkParallel-2 100000 15819 ns/op

Documentation

Index

Constants

View Source
const (
	NetworkTsp  networkType = "tcp"
	NetworkTsp4 networkType = "tcp4"
	NetworkTsp6 networkType = "tcp6"
)

The network must be a TCP network name

Variables

This section is empty.

Functions

func Client

func Client(network networkType) *tspClient

Client - create a new tcp client.

func Server

func Server(network networkType) *tcpServer

Server - create a new tcp server.

Types

type Box

type Box struct {
	Method string          `json:"method"`
	Query  json.RawMessage `json:"query"`
}

Box - container for arguments of the called method.

Jump to

Keyboard shortcuts

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