-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
basic support bidirectional stream #9
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package exposed | ||
|
||
import ( | ||
"errors" | ||
"github.com/thesyncim/exposed/encoding" | ||
) | ||
|
||
type StreamServer struct { | ||
ID uint32 | ||
isServer bool | ||
|
||
codec encoding.Codec | ||
|
||
inMessages chan *request | ||
errOutCh chan error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
sendOutMessages func(id uint32, m *response, resp chan error) | ||
inClosed bool | ||
outClosed bool | ||
} | ||
|
||
func NewServerStream(id uint32, codec encoding.Codec, inMessages chan *request, sendf func(id uint32, m *response, resp chan error)) *StreamServer { | ||
return &StreamServer{ | ||
ID: id, | ||
codec: codec, | ||
inMessages: inMessages, | ||
sendOutMessages: sendf, | ||
} | ||
} | ||
|
||
func (sc *StreamServer) SendMsg(m Message) error { | ||
if sc.outClosed { | ||
return errClosedWriteStream | ||
} | ||
v, err := sc.codec.Marshal(m) | ||
if err != nil { | ||
|
||
return err | ||
} | ||
resp := AcquireResponse() | ||
resp.SwapPayload(v) | ||
errch := make(chan error, 1) | ||
sc.sendOutMessages(sc.ID, resp, errch) | ||
return <-errch | ||
} | ||
|
||
func (sc *StreamServer) RecvMsg(m Message) (err error) { | ||
if sc.inClosed { | ||
return errClosedReadStream | ||
} | ||
//todo timout | ||
var mr *request | ||
var open bool | ||
if mr, open = <-sc.inMessages; !open { | ||
return errClosedReadChannel | ||
} | ||
err = sc.codec.Unmarshal(mr.payload, m) | ||
releaseRequest(mr) | ||
return | ||
} | ||
|
||
type StreamClient struct { | ||
ID uint32 | ||
isServer bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
op uint64 | ||
|
||
codec encoding.Codec | ||
|
||
inMessages <-chan *response | ||
errOutCh chan error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
sendOutMessages func(id uint32, m *request, resp chan error) | ||
inClosed bool | ||
outClosed bool | ||
} | ||
|
||
func NewStreamClient(id uint32, op uint64, codec encoding.Codec, inMessages <-chan *response, sendf func(id uint32, m *request, resp chan error)) *StreamClient { | ||
return &StreamClient{ | ||
ID: id, | ||
op: op, | ||
codec: codec, | ||
inMessages: inMessages, | ||
sendOutMessages: sendf, | ||
} | ||
} | ||
|
||
func (sc *StreamClient) SendMsg(m Message) error { | ||
if sc.outClosed { | ||
return errClosedWriteStream | ||
} | ||
v, err := sc.codec.Marshal(m) | ||
if err != nil { | ||
return err | ||
} | ||
req := acquireRequest() | ||
req.SetOperation(sc.op) | ||
req.SwapPayload(v) | ||
errch := make(chan error, 1) | ||
sc.sendOutMessages(sc.ID, req, errch) | ||
return <-errch | ||
} | ||
|
||
func (sc *StreamClient) RecvMsg(m Message) (err error) { | ||
if sc.inClosed { | ||
return errClosedReadStream | ||
} | ||
//todo timout | ||
var mr *response | ||
var ok bool | ||
if mr, ok = <-sc.inMessages; !ok { | ||
return errClosedReadChannel | ||
} | ||
|
||
err = sc.codec.Unmarshal(mr.payload, m) | ||
ReleaseResponse(mr) | ||
return err | ||
} | ||
|
||
var ( | ||
errClosedReadStream = errors.New("closed read StreamServer") | ||
errClosedReadChannel = errors.New("closed read StreamServer channel") | ||
errClosedWriteStream = errors.New("closed write StreamServer") | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isServer
is unused