Skip to content

Commit

Permalink
Move CDP read write into CDP handler
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Dec 23, 2021
1 parent 6a12e2a commit af825f4
Showing 1 changed file with 41 additions and 46 deletions.
87 changes: 41 additions & 46 deletions tests/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type Server struct {
Context context.Context
}

// NewServer returns a fully configured and running WS test server
// NewServer returns a fully configured and running WS test server.
func NewServer(t testing.TB, opts ...func(*Server)) *Server {
t.Helper()

Expand Down Expand Up @@ -152,7 +152,7 @@ func WithClosureAbnormalHandler(path string) func(*Server) {
}
}

// NewServerWithEcho attaches an echo handler to Server.
// WithEchoHandler attaches an echo handler to Server.
func WithEchoHandler(path string) func(*Server) {
handler := func(w http.ResponseWriter, req *http.Request) {
conn, err := (&websocket.Upgrader{}).Upgrade(w, req, w.Header())
Expand Down Expand Up @@ -202,16 +202,31 @@ func WithCDPHandler(
done := make(chan struct{})
writeCh := make(chan cdproto.Message)

// Read
go func() {
read := func(conn *websocket.Conn) (*cdproto.Message, error) {
_, buf, err := conn.ReadMessage()
if err != nil {
return nil, err
}

var msg cdproto.Message
decoder := jlexer.Lexer{Data: buf}
msg.UnmarshalEasyJSON(&decoder)
if err := decoder.Error(); err != nil {
return nil, err
}

return &msg, nil
}

for {
select {
case <-done:
return
default:
}

msg, err := CDPReadMsg(conn)
msg, err := read(conn)
if err != nil {
close(done)
return
Expand All @@ -224,12 +239,31 @@ func WithCDPHandler(
fn(conn, msg, writeCh, done)
}
}()
// Write

go func() {
write := func(conn *websocket.Conn, msg *cdproto.Message) {
encoder := jwriter.Writer{}
msg.MarshalEasyJSON(&encoder)
if err := encoder.Error; err != nil {
return
}

writer, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
if _, err := encoder.DumpTo(writer); err != nil {
return
}
if err := writer.Close(); err != nil {
return
}
}

for {
select {
case msg := <-writeCh:
CDPWriteMsg(conn, &msg)
write(conn, &msg)
case <-done:
return
}
Expand All @@ -243,9 +277,7 @@ func WithCDPHandler(
}
}

// TODO: make a websocket.Conn wrapper for CDPxxx methods

// CDPDefaultCDPHandler is a default handler for the CDP WS server
// CDPDefaultHandler is a default handler for the CDP WS server.
func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan cdproto.Message, done chan struct{}) {
if msg.SessionID != "" && msg.Method != "" {
switch msg.Method {
Expand Down Expand Up @@ -276,40 +308,3 @@ func CDPDefaultHandler(conn *websocket.Conn, msg *cdproto.Message, writeCh chan
}
}
}

// CDPReadMsg reads a CDP message to the provided Websocket connection
func CDPReadMsg(conn *websocket.Conn) (*cdproto.Message, error) {
_, buf, err := conn.ReadMessage()
if err != nil {
return nil, err
}

var msg cdproto.Message
decoder := jlexer.Lexer{Data: buf}
msg.UnmarshalEasyJSON(&decoder)
if err := decoder.Error(); err != nil {
return nil, err
}

return &msg, nil
}

// CDPWriteMsg writes a CDP message to the provided Websocket connection
func CDPWriteMsg(conn *websocket.Conn, msg *cdproto.Message) {
encoder := jwriter.Writer{}
msg.MarshalEasyJSON(&encoder)
if err := encoder.Error; err != nil {
return
}

writer, err := conn.NextWriter(websocket.TextMessage)
if err != nil {
return
}
if _, err := encoder.DumpTo(writer); err != nil {
return
}
if err := writer.Close(); err != nil {
return
}
}

0 comments on commit af825f4

Please sign in to comment.