Giter Site home page Giter Site logo

bufconn's Introduction

bufconn - higher level interface for sockets in golang

Please note i have designed this for myself for rapid prototyping socket servicies. I have not desinged it to really be used by someone else and for this reason I have not put much effort into conforming with go standards

What does this do?

bufconn provides a type bufconn.Conn which wraps a net.Conn. It is particularly useful for prototyping message passing services (for example LOAD data.txt;STORE data2.txt; is two messages).

Examples

Create Conn

c, err := net.Dial("tcp6", ":8000")
if err != nil {
    panic(err)
}
// For msgRecvHandler look at examples below
conn := bufconn.NewConn(c, msgRecvHandler, ';')

Message handler

The message handler will be called whenever a message is completely read into the buffer and an operation is not currently ongoing. It can block for as long as it needs to, however not other operations or message handlers can be called when it is running

func msgRecvHandler(c *bufconn.C) {
    msg, _ := c.ReadMsg(0)
    if msg != "ping"{
        fmt.Println("Remote did not the message ping")
        return
    }
    c.SendMsg("pong")
    msg2, err := c.ReadMsg(time.Second*5)
    if err != nil{
        fmt.Println("Remote did not send message back in time")
    }
    if msg2 != "pling"{
        fmt.Println("Remote did not send the message pling")
        return
    }
    c.SendMsg("plong")
    fmt.Println("Sequence complete")
}

Operation

We can also create an operation (send the first message)

conn.QueueOperation(func(c *bufconn.C) {
    c.WriteMsg("ping")
    msg, err := c.ReadMsg(time.Second*5)
    if err != nil{
        fmt.Println("Remote did not send message back in time")
    }
    if msg != "pong"{
        fmt.Println("Remote did not send the message pong")
        return
    }
    c.WriteMsg("pling")
    msg2, err := c.ReadMsg(time.Second*5)
    if err != nil{
        fmt.Println("Remote did not send message back in time")
    }
    if msg2 != "plong"{
        fmt.Println("Remote did not send the message plong")
        return
    }
    fmt.Println("Sequence complete")
})

Why bother with all the extra code

It can be annoying to have to deal with multiple goroutines using the same socket. This module allows concurrency whilst not allowing different operations on the socket to interfere with each other

bufconn's People

Contributors

joshpattman avatar

Watchers

James Cloos avatar  avatar

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.