gonetc

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2020 License: Apache-2.0, MIT Imports: 2 Imported by: 0

README

gonetc Build Status codecov Go Report Card PkgGoDev

A simple Go Network wrapper client interface.

Usage

package main

import (
    "log"
    "strings"

    "github.com/joseluisq/gonetc"
)

func main() {
    // Code example using IPC Unix Sockets

    // 1. Create a simple listening Unix socket with echo functionality
    // using the `socat` tool -> http://www.dest-unreach.org/socat/
    // Then execute the following commands on your terminal:
    //  rm -f /tmp/mysocket && socat UNIX-LISTEN:/tmp/mysocket,fork exec:'/bin/cat'

    // 2. Now just run this client code example in order to exchange data with current socket.
    //  go run examples/main.go

    // 2.1 Connect to the listening socket
    sock := gonetc.New("unix", "/tmp/mysocket")
    err := sock.Connect()
    if err != nil {
        log.Fatalln("unable to communicate with socket:", err)
    }

    // 2.2 Send some sequential data to current socket (example only)
    pangram := strings.Split("The quick brown fox jumps over the lazy dog", " ")
    for _, word := range pangram {
        log.Println("client data sent:", word)
        _, err := sock.Write([]byte(word), func(resp []byte, err error, done func()) {
            if err != nil {
                log.Fatalln("unable to write data to socket", err)
            }
            log.Println("client data received:", string(resp))
            // Finish the current write handling response if we are done
            done()
        })
        if err != nil {
            log.Fatalln("unable to write to socket:", err)
        }
    }

    sock.Close()

    // 3. Finally after running the client you'll see a similar output like:
    //
    // 2020/12/11 00:32:27 client data sent: The
    // 2020/12/11 00:32:27 client data received: The
    // 2020/12/11 00:32:28 client data sent: quick
    // 2020/12/11 00:32:28 client data received: quick
    // 2020/12/11 00:32:29 client data sent: brown
    // 2020/12/11 00:32:29 client data received: brown
    // 2020/12/11 00:32:30 client data sent: fox
    // 2020/12/11 00:32:30 client data received: fox
    // 2020/12/11 00:32:31 client data sent: jumps
    // 2020/12/11 00:32:31 client data received: jumps
    // 2020/12/11 00:32:32 client data sent: over
    // 2020/12/11 00:32:32 client data received: over
    // 2020/12/11 00:32:33 client data sent: the
    // 2020/12/11 00:32:33 client data received: the
    // 2020/12/11 00:32:34 client data sent: lazy
    // 2020/12/11 00:32:34 client data received: lazy
    // 2020/12/11 00:32:35 client data sent: dog
    // 2020/12/11 00:32:35 client data received: dog
}

Contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in current work by you, as defined in the Apache-2.0 license, shall be dual licensed as described below, without any additional terms or conditions.

Feel free to send some Pull request or issue.

License

This work is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

© 2020-present Jose Quintana

Documentation

Overview

Package gonetc is a simple client interface that wraps Go net.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NetClient

type NetClient struct {

	// It specifies the maximum size of bytes per read (2048 by default).
	MaxReadBytes int
	// contains filtered or unexported fields
}

NetClient defines a client network.

func New

func New(network string, address string) *NetClient

New creates a new client network instance. Parameters are the same as Go `net.Dial`.

func (*NetClient) Close

func (c *NetClient) Close() error

Close closes current client network connection.

func (*NetClient) Conn

func (c *NetClient) Conn() net.Conn

Conn gets the inner Go net connection instance.

func (*NetClient) Connect

func (c *NetClient) Connect() error

Connect establishes a new network connection.

func (*NetClient) Listen

func (c *NetClient) Listen(respHandler func(data []byte, err error, done func()))

Listen listens for incoming response data.

func (*NetClient) Write

func (c *NetClient) Write(data []byte, respHandler func(data []byte, err error, done func())) (n int, err error)

Write writes bytes to current client network connection. It also provides an optional data response handler. When a `respHandler` function is provided then three params are provided: `data []byte`, `err error`, `done func()`. The `done()` function param acts as a callback completion in order to finish the current write execution.

Directories

Path Synopsis