You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nghttp2-go/data_provider.go

147 lines
3.2 KiB
Go

6 years ago
package nghttp2
/*
#include "_nghttp2.h"
*/
import "C"
import (
"bytes"
"errors"
"io"
"log"
6 years ago
"sync"
"time"
"unsafe"
)
// dataProvider provider data for libnghttp2 library
// libnghttp2 callback will Read to read the data,
// application call Write to provider data,
// application call Close will cause Read return io.EOF
type dataProvider struct {
buf *bytes.Buffer
closed bool
lock *sync.Mutex
sessLock *sync.Mutex
6 years ago
session *C.nghttp2_session
streamID int
deferred bool
6 years ago
}
// Read read from data provider
func (dp *dataProvider) Read(buf []byte) (n int, err error) {
if dp.buf == nil || dp.lock == nil || dp.sessLock == nil || dp.session == nil {
log.Println("db read invalid state")
return 0, errors.New("invalid state")
}
6 years ago
dp.lock.Lock()
defer dp.lock.Unlock()
n, err = dp.buf.Read(buf)
6 years ago
if err != nil && !dp.closed {
return 0, errAgain
}
return
}
// Write provider data for data provider
func (dp *dataProvider) Write(buf []byte) (n int, err error) {
if dp.buf == nil || dp.lock == nil || dp.sessLock == nil || dp.session == nil {
log.Println("dp write invalid state")
return 0, errors.New("invalid state")
}
6 years ago
dp.lock.Lock()
defer dp.lock.Unlock()
6 years ago
if dp.closed {
return 0, io.EOF
}
6 years ago
if dp.deferred {
dp.sessLock.Lock()
C.nghttp2_session_resume_data(dp.session, C.int(dp.streamID))
dp.sessLock.Unlock()
6 years ago
dp.deferred = false
}
6 years ago
return dp.buf.Write(buf)
}
// Close end to provide data
func (dp *dataProvider) Close() error {
if dp.buf == nil || dp.lock == nil || dp.sessLock == nil || dp.session == nil {
log.Println("dp close, invalid state")
return errors.New("invalid state")
}
6 years ago
dp.lock.Lock()
defer dp.lock.Unlock()
6 years ago
if dp.closed {
return nil
}
6 years ago
dp.closed = true
//log.Printf("dp close stream %d", dp.streamID)
if dp.deferred {
dp.sessLock.Lock()
C.nghttp2_session_resume_data(dp.session, C.int(dp.streamID))
dp.sessLock.Unlock()
6 years ago
dp.deferred = false
}
6 years ago
return nil
}
func newDataProvider(cdp unsafe.Pointer, sessionLock *sync.Mutex, t int) *dataProvider {
6 years ago
dp := &dataProvider{
buf: new(bytes.Buffer),
lock: new(sync.Mutex),
sessLock: sessionLock,
6 years ago
}
C.data_provider_set_callback(C.size_t(uintptr(cdp)),
C.size_t(uintptr(unsafe.Pointer(dp))), C.int(t))
return dp
6 years ago
}
// bodyProvider provide data for http body
// Read will block when data not yet avaliable
type bodyProvider struct {
buf *bytes.Buffer
closed bool
lock *sync.Mutex
}
// Read read data from provider
// will block when data not yet avaliable
func (bp *bodyProvider) Read(buf []byte) (int, error) {
var delay = 100 * time.Millisecond
6 years ago
6 years ago
for {
bp.lock.Lock()
n, err := bp.buf.Read(buf)
bp.lock.Unlock()
if err != nil && !bp.closed {
time.Sleep(delay)
6 years ago
continue
}
return n, err
}
}
// Write provide data for dataProvider
// libnghttp2 data chunk recv callback will call this
func (bp *bodyProvider) Write(buf []byte) (int, error) {
bp.lock.Lock()
defer bp.lock.Unlock()
6 years ago
6 years ago
return bp.buf.Write(buf)
}
// Close end to provide data
func (bp *bodyProvider) Close() error {
bp.lock.Lock()
defer bp.lock.Unlock()
6 years ago
6 years ago
bp.closed = true
return nil
}