netchan

package module
v0.0.0-...-7503f82 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2018 License: MIT Imports: 7 Imported by: 0

README

netchan

Build Status

Send and receive over the network with the built-in Go channel.

Install

With a correctly configured Go toolchain:

go get -u github.com/helinwang/netchan

Examples

Send and receive over TCP and UNIX:

func Example() {
	const (
		unixAddr = "tmp"
		tcpAddr  = ":8003"
		name     = "test"
	)

	// Struct's field names should be exported, otherwise, they
	// will not be sent since they are ignored by the gob encoder.
	type data struct {
		A int
		B float32
	}

	tcpSend := make(chan interface{})
	unixSend := make(chan interface{})
	recv := make(chan interface{})

	// this example shows sending and receiving over tcp and unix,
	// but you can use only tcp or unix, just send or just
	// receive.
	sr := netchan.NewSendRecv()
	go func() {
		err := sr.ListenAndServe("tcp", tcpAddr)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := sr.ListenAndServe("unix", unixAddr)
		if err != nil {
			panic(err)
		}
	}()

	// wait for the server to start
	time.Sleep(30 * time.Millisecond)

	s := netchan.NewHandler(sr)
	go func() {
		err := s.HandleSend("tcp", tcpAddr, name, tcpSend)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := s.HandleSend("unix", unixAddr, name, unixSend)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := s.HandleRecv(name, recv, reflect.TypeOf(data{}))
		if err != nil {
			panic(err)
		}
	}()

	d := data{A: 1, B: 2}
	tcpSend <- d
	r := (<-recv).(data)
	fmt.Println(r)

	d = data{A: 3, B: 4}
	unixSend <- d
	r = (<-recv).(data)
	fmt.Println(r)
	// Output:
	// {1 2}
	// {3 4}

	err := os.Remove(unixAddr)
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

Example
package main

import (
	"fmt"
	"os"
	"reflect"
	"time"

	"github.com/helinwang/netchan"
)

func main() {
	const (
		unixAddr = "tmp"
		tcpAddr  = ":8003"
		name     = "test"
	)

	type data struct {
		A int
		B float32
	}

	tcpSend := make(chan interface{})
	unixSend := make(chan interface{})
	recv := make(chan interface{})

	// this example shows sending and receiving over tcp and unix,
	// but you can use only tcp or unix, just send or just
	// receive.
	sr := netchan.NewSendRecv()
	go func() {
		err := sr.ListenAndServe("tcp", tcpAddr)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := sr.ListenAndServe("unix", unixAddr)
		if err != nil {
			panic(err)
		}
	}()

	// wait for the server to start
	time.Sleep(30 * time.Millisecond)

	s := netchan.NewHandler(sr)
	go func() {
		err := s.HandleSend("tcp", tcpAddr, name, tcpSend)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := s.HandleSend("unix", unixAddr, name, unixSend)
		if err != nil {
			panic(err)
		}
	}()

	go func() {
		err := s.HandleRecv(name, recv, reflect.TypeOf(data{}))
		if err != nil {
			panic(err)
		}
	}()

	d := data{A: 1, B: 2}
	tcpSend <- d
	r := (<-recv).(data)
	fmt.Println(r)

	d = data{A: 3, B: 4}
	unixSend <- d
	r = (<-recv).(data)
	fmt.Println(r)

	err := os.Remove(unixAddr)
	if err != nil {
		panic(err)
	}
}
Output:
{1 2}
{3 4}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

func NewHandler

func NewHandler(sr SendRecver) *Handler

func (*Handler) HandleRecv

func (s *Handler) HandleRecv(name string, ch chan<- interface{}, t reflect.Type) error

func (*Handler) HandleSend

func (s *Handler) HandleSend(network, address, name string, ch <-chan interface{}) error

type RecvServer

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

func (*RecvServer) Put

func (s *RecvServer) Put(req Request, _ *int) error

type Request

type Request struct {
	Name string
	Data []byte
}

type SendRecv

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

func NewSendRecv

func NewSendRecv() *SendRecv

func (*SendRecv) ListenAndServe

func (s *SendRecv) ListenAndServe(network, address string) error

func (*SendRecv) Recv

func (s *SendRecv) Recv(name string) []byte

func (*SendRecv) Send

func (s *SendRecv) Send(network, address, name string, body []byte) error

type SendRecver

type SendRecver interface {
	Send(network, address, name string, body []byte) error
	Recv(name string) []byte
}

Jump to

Keyboard shortcuts

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