Git Product home page Git Product logo

framing's Introduction

Build Status Coverage Status GoDoc Go Version

framing

Framing provides a prefix length framed net.Conn connection. This is useful if you have connections that send packages that are prefixed with 1, 2 or 4 bytes of message length before the actual data.

Example

package main

import (
    "encoding/binary"
    "fmt"
    "log"
    "net"
    "strconv"

    "github.com/eproxus/framing"
)

const prefixLength = 4

var endianess = binary.BigEndian

func main() {
    message := "13 bytes long"

    l, err := net.Listen("tcp", ":0") // Listen on localhost, random port
    if err != nil {
        log.Fatal(err)
    }
    defer l.Close()

    port := l.Addr().(*net.TCPAddr).Port

    // Send message in a go routine
    go func() {
        conn, err := net.Dial("tcp", ":"+strconv.Itoa(port))
        if err != nil {
            log.Fatal(err)
        }

        framed, err := framing.NewConn(conn, prefixLength, endianess)
        if err != nil {
            log.Fatal(err)
        }
        defer framed.Close()

        if _, err := fmt.Fprintf(framed, message); err != nil {
            log.Fatal(err)
        }
    }()

    conn, err := l.Accept()
    if err != nil {
        log.Fatal(err)
    }

    framed, err := framing.NewConn(conn, prefixLength, endianess)
    if err != nil {
        log.Fatal(err)
    }
    defer framed.Close()

    // Receive message
    frame, err := framed.ReadFrame()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Recieved \"%v\"\n", string(frame[:13]))
}

framing's People

Contributors

eproxus avatar

Watchers

James Cloos avatar  avatar

Forkers

pjwerneck

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.