spdystream

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2016 License: Apache-2.0, CC-BY-SA-4.0, Apache-2.0 Imports: 11 Imported by: 0

README

SpdyStream

A multiplexed stream library using spdy

Usage

Client example (connecting to mirroring server without auth)

package main

import (
	"fmt"
	"github.com/docker/spdystream"
	"net"
	"net/http"
)

func main() {
	conn, err := net.Dial("tcp", "localhost:8080")
	if err != nil {
		panic(err)
	}
	spdyConn, err := spdystream.NewConnection(conn, false)
	if err != nil {
		panic(err)
	}
	go spdyConn.Serve(spdystream.NoOpStreamHandler)
	stream, err := spdyConn.CreateStream(http.Header{}, nil, false)
	if err != nil {
		panic(err)
	}

	stream.Wait()

	fmt.Fprint(stream, "Writing to stream")

	buf := make([]byte, 25)
	stream.Read(buf)
	fmt.Println(string(buf))

	stream.Close()
}

Server example (mirroring server without auth)

package main

import (
	"github.com/docker/spdystream"
	"net"
)

func main() {
	listener, err := net.Listen("tcp", "localhost:8080")
	if err != nil {
		panic(err)
	}
	for {
		conn, err := listener.Accept()
		if err != nil {
			panic(err)
		}
		spdyConn, err := spdystream.NewConnection(conn, true)
		if err != nil {
			panic(err)
		}
		go spdyConn.Serve(spdystream.MirrorStreamHandler)
	}
}

Copyright © 2014-2015 Docker, Inc. All rights reserved, except as follows. Code is released under the Apache 2.0 license. The README.md file, and files in the "docs" folder are licensed under the Creative Commons Attribution 4.0 International License under the terms and conditions set forth in the file "LICENSE.docs". You may obtain a duplicate copy of the same license, titled CC-BY-SA-4.0, at http://creativecommons.org/licenses/by/4.0/.

Documentation

Index

Constants

View Source
const (
	FRAME_WORKERS = 5
	QUEUE_SIZE    = 50
)

Variables

View Source
var (
	ErrInvalidStreamId   = errors.New("Invalid stream id")
	ErrTimeout           = errors.New("Timeout occured")
	ErrReset             = errors.New("Stream reset")
	ErrWriteClosedStream = errors.New("Write on closed stream")
)
View Source
var (
	DEBUG = os.Getenv("DEBUG")
)
View Source
var (
	ErrUnreadPartialData = errors.New("unread partial data")
)

Functions

func MirrorStreamHandler

func MirrorStreamHandler(stream *Stream)

MirrorStreamHandler mirrors all streams.

func NoOpStreamHandler

func NoOpStreamHandler(stream *Stream)

NoopStreamHandler does nothing when stream connects, most likely used with RejectAuthHandler which will not allow any streams to make it to the stream handler.

Types

type AuthHandler

type AuthHandler func(header http.Header, slot uint8, parent uint32) bool

type Connection

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

func NewConnection

func NewConnection(conn net.Conn, server bool) (*Connection, error)

NewConnection creates a new spdy connection from an existing network connection.

func (*Connection) Close

func (s *Connection) Close() error

Closes spdy connection by sending GoAway frame and initiating shutdown

func (*Connection) CloseChan

func (s *Connection) CloseChan() <-chan bool

func (*Connection) CloseWait

func (s *Connection) CloseWait() error

CloseWait closes the connection and waits for shutdown to finish. Note the underlying network Connection is not closed until the end of shutdown.

func (*Connection) CreateStream

func (s *Connection) CreateStream(headers http.Header, parent *Stream, fin bool) (*Stream, error)

CreateStream creates a new spdy stream using the parameters for creating the stream frame. The stream frame will be sent upon calling this function, however this function does not wait for the reply frame. If waiting for the reply is desired, use the stream Wait or WaitTimeout function on the stream returned by this function.

func (*Connection) FindStream

func (s *Connection) FindStream(streamId uint32) *Stream

FindStream looks up the given stream id and either waits for the stream to be found or returns nil if the stream id is no longer valid.

func (*Connection) NotifyClose

func (s *Connection) NotifyClose(c chan<- *Stream, timeout time.Duration)

NotifyClose registers a channel to be called when the remote peer inidicates connection closure. The last stream to be received by the remote will be sent on the channel. The notify timeout will determine the duration between go away received and the connection being closed.

func (*Connection) PeekNextStreamId

func (s *Connection) PeekNextStreamId() spdy.StreamId

PeekNextStreamId returns the next sequential id and keeps the next id untouched

func (*Connection) Ping

func (s *Connection) Ping() (time.Duration, error)

Ping sends a ping frame across the connection and returns the response time

func (*Connection) Serve

func (s *Connection) Serve(newHandler StreamHandler)

Serve handles frames sent from the server, including reply frames which are needed to fully initiate connections. Both clients and servers should call Serve in a separate goroutine before creating streams.

func (*Connection) SetCloseTimeout

func (s *Connection) SetCloseTimeout(timeout time.Duration)

SetCloseTimeout sets the amount of time close will wait for streams to finish before terminating the underlying network connection. Setting the timeout to 0 will cause close to wait forever, which is the default.

func (*Connection) SetIdleTimeout

func (s *Connection) SetIdleTimeout(timeout time.Duration)

SetIdleTimeout sets the amount of time the connection may sit idle before it is forcefully terminated.

func (*Connection) Wait

func (s *Connection) Wait(waitTimeout time.Duration) error

Wait waits for the connection to finish shutdown or for the wait timeout duration to expire. This needs to be called either after Close has been called or the GOAWAYFRAME has been received. If the wait timeout is 0, this function will block until shutdown finishes. If wait is never called and a shutdown error occurs, that error will be logged as an unhandled error.

type PriorityFrameQueue

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

func NewPriorityFrameQueue

func NewPriorityFrameQueue(size int) *PriorityFrameQueue

func (*PriorityFrameQueue) Drain

func (q *PriorityFrameQueue) Drain()

func (*PriorityFrameQueue) Pop

func (q *PriorityFrameQueue) Pop() spdy.Frame

func (*PriorityFrameQueue) Push

func (q *PriorityFrameQueue) Push(frame spdy.Frame, priority uint8)

type Stream

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

func (*Stream) Cancel

func (s *Stream) Cancel() error

Cancel sends a reset frame with the status canceled. This can be used at any time by the creator of the Stream to indicate the stream is no longer needed.

func (*Stream) Close

func (s *Stream) Close() error

Close closes the stream by sending an empty data frame with the finish flag set, indicating this side is finished with the stream.

func (*Stream) CreateSubStream

func (s *Stream) CreateSubStream(headers http.Header, fin bool) (*Stream, error)

CreateSubStream creates a stream using the current as the parent

func (*Stream) Headers

func (s *Stream) Headers() http.Header

Headers returns the headers used to create the stream

func (*Stream) Identifier

func (s *Stream) Identifier() uint32

Identifier returns a 32 bit identifier for the stream

func (*Stream) IsFinished

func (s *Stream) IsFinished() bool

IsFinished returns whether the stream has finished sending data

func (*Stream) LocalAddr

func (s *Stream) LocalAddr() net.Addr

func (*Stream) Parent

func (s *Stream) Parent() *Stream

Parent returns the parent stream

func (*Stream) Read

func (s *Stream) Read(p []byte) (n int, err error)

Read reads bytes from a stream, a single read will never get more than what is sent on a single data frame, but a multiple calls to read may get data from the same data frame.

func (*Stream) ReadData

func (s *Stream) ReadData() ([]byte, error)

ReadData reads an entire data frame and returns the byte array from the data frame. If there is unread data from the result of a Read call, this function will return an ErrUnreadPartialData.

func (*Stream) ReceiveHeader

func (s *Stream) ReceiveHeader() (http.Header, error)

ReceiveHeader receives a header sent on the other side of the stream. This function will block until a header is received or stream is closed.

func (*Stream) Refuse

func (s *Stream) Refuse() error

Refuse sends a reset frame with the status refuse, only valid to be called once when handling a new stream. This may be used to indicate that a stream is not allowed when http status codes are not being used.

func (*Stream) RemoteAddr

func (s *Stream) RemoteAddr() net.Addr

func (*Stream) Reset

func (s *Stream) Reset() error

Reset sends a reset frame, putting the stream into the fully closed state.

func (*Stream) SendHeader

func (s *Stream) SendHeader(headers http.Header, fin bool) error

SendHeader sends a header frame across the stream

func (*Stream) SendReply

func (s *Stream) SendReply(headers http.Header, fin bool) error

SendReply sends a reply on a stream, only valid to be called once when handling a new stream

func (*Stream) SetDeadline

func (s *Stream) SetDeadline(t time.Time) error

func (*Stream) SetPriority

func (s *Stream) SetPriority(priority uint8)

SetPriority sets the stream priority, does not affect the remote priority of this stream after Open has been called. Valid values are 0 through 7, 0 being the highest priority and 7 the lowest.

func (*Stream) SetReadDeadline

func (s *Stream) SetReadDeadline(t time.Time) error

func (*Stream) SetWriteDeadline

func (s *Stream) SetWriteDeadline(t time.Time) error

func (*Stream) String

func (s *Stream) String() string

String returns the string version of stream using the streamId to uniquely identify the stream

func (*Stream) Wait

func (s *Stream) Wait() error

Wait waits for the stream to receive a reply.

func (*Stream) WaitTimeout

func (s *Stream) WaitTimeout(timeout time.Duration) error

WaitTimeout waits for the stream to receive a reply or for timeout. When the timeout is reached, ErrTimeout will be returned.

func (*Stream) Write

func (s *Stream) Write(data []byte) (n int, err error)

Write writes bytes to a stream, calling write data for each call.

func (*Stream) WriteData

func (s *Stream) WriteData(data []byte, fin bool) error

WriteData writes data to stream, sending a dataframe per call

type StreamHandler

type StreamHandler func(stream *Stream)

Directories

Path Synopsis
Package spdy implements the SPDY protocol (currently SPDY/3), described in http://www.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3.
Package spdy implements the SPDY protocol (currently SPDY/3), described in http://www.chromium.org/spdy/spdy-protocol/spdy-protocol-draft3.

Jump to

Keyboard shortcuts

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