tcp

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 11 Imported by: 1

README

tcp

A golang language library for tcp server and client


server
package main

import (
	"fmt"
	"github.com/gitteamer/tcp"
	"net"
)

func main()  {
 	// new tcp server
 	server := tcp.NewServer()
 
 	// on connect event
 	server.OnConnect(func(conn *net.TCPConn, addr *tcp.Addr) {
 		fmt.Println(fmt.Sprintf("one client connect, remote address=%s.", conn.RemoteAddr().String()))
 	})
 
 	// on receive data event
 	server.OnRecv(func(addr *tcp.Addr, req *tcp.Message) {
 		fmt.Println(fmt.Sprintf("req.Type=%d, req.Data=%s.", req.Type, string(req.Data)))
 
 		_ = server.Send(*addr, &tcp.Message{
 			Type: 1,
 			Data: []byte(fmt.Sprintf("hello: %s.", addr.GetAddress())),
 		})
 	})
 
 	// on disconnect event
 	server.OnDisconnect(func(addr *tcp.Addr) {
 
 	})
 
 	server.Run(":8080")
}
client
package main

import (
	"fmt"
	"github.com/gitteamer/tcp"
	"os"
	"os/signal"
)

func main() {
	client := tcp.NewClient("127.0.0.1:8080")

	// send data
	err := client.Send(&tcp.Message{
		Type: 1,
		Data: []byte("hello server"),
	})
	if err != nil {
		fmt.Println("send data error:", err.Error())
	}

	// on receive data event
	client.OnRecv(func(recv *tcp.Message) {
		fmt.Println(fmt.Sprintf("recv data, recv.Type=%d, recv.Data=%s.", recv.Type, string(recv.Data)))
	})
}

message type
type Message struct {
	Type uint
	Data []byte
}
  • Type
    Because of the heartbeat is define 0. (heartbeat = 0 // heartbeat package)
    You can define other flag.
  • Data
    the data that you want to transfer. It could be JSON, gob, protobuf and so on.

Documentation

Overview

*

  • Copyright gitteamer 2020
  • @date: 2020/11/10
  • @note: address

*

  • Copyright gitteamer 2020
  • @date: 2020/11/10
  • @note: server connect manager

*

  • Copyright gitteamer 2020
  • @date: 2020/11/10
  • @note: error

*

  • Copyright gitteamer 2020
  • @date: 2020/11/10
  • @note: message

*

  • Copyright gitteamer 2020
  • @date: 2020/11/10
  • @note: tcp server

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addr

type Addr struct {
	Ip   string
	Port string
}

*

  • author gitteamer 2020/11/10
  • address info

func NewAddr

func NewAddr(address string) *Addr

*

  • author gitteamer 2020/11/10 *
  • new a Addr obj
  • @param string address: input address
  • @return Addr obj pointer

func (*Addr) GetAddress

func (a *Addr) GetAddress() string

*

  • author gitteamer 2020/11/10
  • get address: ip:port
  • @return string

type Client

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

*

  • author gitteamer 2020/11/10
  • tcp client struct

func NewClient

func NewClient(address string) *Client

*

  • author gitteamer 2020/11/10
  • new client by address, ip:port

func (*Client) Close

func (c *Client) Close()

*

  • author gitteamer 2020/11/10
  • close connect

func (*Client) OnRecv

func (c *Client) OnRecv(handle ClientRecvHandle)

*

  • author gitteamer 2020/11/10
  • register receive event callback function
  • @param ClientRecvHandle handle: callback function

func (*Client) Send

func (c *Client) Send(msg *Message) error

*

  • author gitteamer 2020/11/10
  • send data to server
  • @param *Message msg: send message data

type ClientRecvHandle

type ClientRecvHandle = func(recv *Message)

*

  • author gitteamer 2020/11/10
  • receive event callback function

type Connect

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

*

  • author gitteamer 2020/11/10
  • client connect info, receive chan, send chan
  • Please use NewConnect() to create connect obj.

func NewConnect

func NewConnect(server *Server, conn *net.TCPConn, addr *Addr) *Connect

*

  • author gitteamer 2020/11/10
  • new server connect obj

func (*Connect) Close

func (c *Connect) Close()

*

  • author gitteamer 2020/11/10
  • close connect

type ConnectHandle

type ConnectHandle = func(conn *net.TCPConn, addr *Addr)

type DisconnectHandle

type DisconnectHandle = func(addr *Addr)

type Message

type Message struct {
	Type uint
	Data []byte
}

*

  • receive, send struct

type Server

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

*

  • author gitteamer 2020/11/10
  • tcp server.
  • Please use NewServer() to create server obj.

func NewServer

func NewServer() *Server

func (*Server) Close

func (s *Server) Close()

*

  • author gitteamer 2020/11/10
  • close server and recycling resources

func (*Server) OnConnect

func (s *Server) OnConnect(handle ConnectHandle)

*

  • author gitteamer 2020/11/10
  • register callback function when a client connect
  • @param ConnectHandle handle: callback function

func (*Server) OnDisconnect

func (s *Server) OnDisconnect(handle DisconnectHandle)

*

  • author gitteamer 2020/11/10
  • register callback function when a client disconnect
  • @param DisconnectHandle handle: callback function

func (*Server) OnRecv

func (s *Server) OnRecv(handle ServerRecvHandle)

*

  • author gitteamer 2020/11/10
  • register callback function when receive data
  • @param RecvHandle handle: callback function

func (*Server) Run

func (s *Server) Run(addr string)

*

  • author gitteamer 2020/11/10
  • run tcp server.
  • If you want to execute in the background, Please run as: go server.Run(addr)
  • @param string addr: Listen address, as ip:port or :port

func (*Server) Send

func (s *Server) Send(addr Addr, msg *Message) error

*

  • author gitteamer 2020/11/10
  • send a message to tcp client
  • @param Addr addr: the client address struct
  • @param *Message msg: the message sent to client

type ServerRecvHandle

type ServerRecvHandle = func(addr *Addr, req *Message)

*

  • author gitteamer 2020/11/10
  • the callback function for event
  • @param *Message req: the message recv from client connect
  • @return *Message: the message response to client. if the message is nil, then do nothing

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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