packetsvr

package module
v0.0.0-...-0b5d5cf Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 0 Imported by: 0

README

Packet Server

A simple, multi-threaded TCP/IP packet server framework written in Go. Originally forked off of firstrow/tcp_server. Intended to be used as a starting point for networking projects.

Example Usage

package main

import (
  "log"

  "github.com/lukehollenback/packet-server/tcp"
)

func main() {
  var err error

  //
  // Create a new server that will bind to port 9999.
  //
  server, err := tcp.CreateServer(&tcp.ServerConfig{
    Address:                  "localhost:9999",
    OnNewClient:              func(c *tcp.Client) { log.Print("Client connected.") },
    OnNewMessage:             func(c *tcp.Client, msg string) { log.Print(msg) },
    OnClientConnectionClosed: func(client *tcp.Client) { log.Print("Client disconnected.") },
    Delim:                    '\n',
  })
  if err != nil {
    log.Fatalf("The server failed to create. (Error: %s)", err)
  }


  //
  // Bind the server and have it begin listening for connections.
  //
  err = server.Start()
  if err != nil {
    log.Fatalf("The server failed to start! (Error: %s)", err)
  }

  //
  // ...Do something here. Consider something like looping to handle operating system interupts...
  //

  //
  // Tell the server to stop and wait for it to finish doing so.
  //
  chStopped, err := server.Stop()
  if err != nil {
    log.Fatalf("The server failed to stop! (Error: %s)", err)
  }

  <-chStopped
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server interface {
	//
	// Start begins the start-up process of the packet server. This method may return faster than the
	// server actually takes to start up. To handle such scenarios, a channel is returned that can be
	// blocked on. A "true" value will be written to said channel once server start-up is complete.
	//
	// It is up to the caller of this method to ensure that subsequent calls are not made prior to
	// completion of the start-up process. Failure to do so may result in a corrupt program state.
	//
	Start() (<-chan bool, error)

	//
	// Stop begins the shut-down process of the packet server. This method may return faster than the
	// server actually takes to shut down. To handle such scenarios, a channel is returned that can be
	// blocked on. A "true" value will be written to said channel once server shut-down is complete.
	//
	// It is up to the caller of this method to ensure that subsequent calls are not made prior to
	// completion of the shut-down process. Failure to do so may result in a corrupt program state.
	//
	Stop() (<-chan bool, error)
}

Server provides a protocol-agnostic interface for packet server implementations.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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