2018-05-03 17:31:09 +00:00
|
|
|
package ws
|
|
|
|
|
|
|
|
import (
|
2018-05-05 15:38:07 +00:00
|
|
|
"unicode/utf8"
|
2018-05-03 19:10:02 +00:00
|
|
|
"time"
|
2018-05-05 15:38:07 +00:00
|
|
|
"sync"
|
2018-05-03 17:31:09 +00:00
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
2018-05-03 17:50:30 +00:00
|
|
|
"git.xdrm.io/gws/internal/http/upgrade/request"
|
2018-05-03 17:31:09 +00:00
|
|
|
"net"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Represents a client socket utility (reader, writer, ..)
|
|
|
|
type clientIO struct {
|
|
|
|
sock net.Conn
|
|
|
|
reader *bufio.Reader
|
2018-05-03 19:10:02 +00:00
|
|
|
kill chan<- *client // unregisters client
|
2018-05-05 15:38:07 +00:00
|
|
|
closing bool
|
|
|
|
closingMu sync.Mutex
|
|
|
|
reading sync.WaitGroup
|
2018-05-05 16:43:16 +00:00
|
|
|
writing bool
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents all channels that need a client
|
|
|
|
type clientChannelSet struct{
|
|
|
|
receive chan Message
|
2018-05-05 16:43:16 +00:00
|
|
|
send chan Message
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Represents a websocket client
|
|
|
|
type client struct {
|
|
|
|
io clientIO
|
|
|
|
iface *Client
|
|
|
|
ch clientChannelSet
|
2018-05-03 19:10:02 +00:00
|
|
|
status MessageError // close status ; 0 = nothing ; else -> must close
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create creates a new client
|
|
|
|
func buildClient(s net.Conn, ctl ControllerSet, serverCh serverChannelSet) (*client, error){
|
|
|
|
|
|
|
|
/* (1) Manage UPGRADE request
|
|
|
|
---------------------------------------------------------*/
|
2018-05-03 17:50:30 +00:00
|
|
|
/* (1) Parse request */
|
|
|
|
req, _ := request.Parse(s)
|
|
|
|
|
|
|
|
/* (3) Build response */
|
|
|
|
res := req.BuildResponse()
|
|
|
|
|
|
|
|
/* (4) Write into socket */
|
|
|
|
_, err := res.Send(s)
|
2018-05-03 17:31:09 +00:00
|
|
|
if err != nil {
|
2018-05-03 17:50:30 +00:00
|
|
|
return nil, fmt.Errorf("Upgrade write error: %s", err)
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 17:50:30 +00:00
|
|
|
if res.GetStatusCode() != 101 {
|
2018-05-03 17:31:09 +00:00
|
|
|
s.Close()
|
2018-05-03 17:50:30 +00:00
|
|
|
return nil, fmt.Errorf("Upgrade error (HTTP %d)\n", res.GetStatusCode())
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Initialise client
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Get upgrade data */
|
2018-05-03 17:50:30 +00:00
|
|
|
clientURI := req.GetURI()
|
|
|
|
clientProtocol := res.GetProtocol()
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (2) Initialise client */
|
2018-05-05 15:38:07 +00:00
|
|
|
cli := &client{
|
2018-05-03 17:31:09 +00:00
|
|
|
io: clientIO{
|
|
|
|
sock: s,
|
|
|
|
reader: bufio.NewReader(s),
|
2018-05-05 15:38:07 +00:00
|
|
|
kill: serverCh.unregister,
|
2018-05-03 17:31:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
iface: &Client{
|
|
|
|
Protocol: string(clientProtocol),
|
|
|
|
Arguments: [][]string{ []string{ clientURI } },
|
|
|
|
},
|
|
|
|
|
|
|
|
ch: clientChannelSet{
|
2018-05-05 16:43:16 +00:00
|
|
|
receive: make(chan Message, 1),
|
|
|
|
send: make(chan Message, 1),
|
2018-05-03 17:31:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Find controller by URI
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Try to find one */
|
|
|
|
controller, arguments := ctl.Match(clientURI);
|
|
|
|
|
|
|
|
/* (2) If nothing found -> error */
|
|
|
|
if controller == nil {
|
|
|
|
return nil, fmt.Errorf("No controller found, no default controller set\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Copy arguments */
|
2018-05-05 15:38:07 +00:00
|
|
|
cli.iface.Arguments = arguments
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Launch client routines
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Launch client controller */
|
2018-05-03 19:10:02 +00:00
|
|
|
go controller.Fun(
|
2018-05-05 15:38:07 +00:00
|
|
|
cli.iface, // pass the client
|
|
|
|
cli.ch.receive, // the receiver
|
|
|
|
cli.ch.send, // the sender
|
2018-05-03 17:31:09 +00:00
|
|
|
serverCh.broadcast, // broadcast sender
|
|
|
|
)
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (2) Launch message reader */
|
2018-05-05 15:38:07 +00:00
|
|
|
go clientReader(cli)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (3) Launc writer */
|
2018-05-05 15:38:07 +00:00
|
|
|
go clientWriter(cli)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
return cli, nil
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
|
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
// reader reads and parses messages from the buffer
|
2018-05-05 15:38:07 +00:00
|
|
|
func clientReader(c *client){
|
|
|
|
|
|
|
|
errorCode := NORMAL
|
|
|
|
clientAck := true
|
|
|
|
c.io.reading.Add(1)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
/* if currently closing -> exit */
|
|
|
|
if c.io.closing {
|
|
|
|
fmt.Printf("[reader] killed because closing")
|
2018-05-03 19:10:02 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
/*** Parse message ***/
|
2018-05-03 17:31:09 +00:00
|
|
|
msg, err := readMessage(c.io.reader)
|
|
|
|
if err != nil {
|
|
|
|
// fmt.Printf(" [reader] %s\n", err)
|
2018-05-05 15:38:07 +00:00
|
|
|
break
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (4) CLOSE */
|
2018-05-03 17:31:09 +00:00
|
|
|
if msg.Type == CLOSE {
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf(" [reader] CLOSE ; size %d\n", msg.Size)
|
|
|
|
// if msg.Size >= 2 {
|
|
|
|
// errCode := binary.BigEndian.Uint16(msg.Data[0:2])
|
|
|
|
// fmt.Printf(" ; status %d\n", errCode)
|
|
|
|
// fmt.Printf(" ; msg '%s'\n", msg.Data[2:])
|
|
|
|
// }
|
|
|
|
clientAck = false
|
|
|
|
break
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (5) PING size error */
|
2018-05-03 17:31:09 +00:00
|
|
|
if msg.Type == PING && msg.Size > 125 {
|
2018-05-05 15:38:07 +00:00
|
|
|
fmt.Printf(" [reader] PING payload too big\n")
|
|
|
|
// fmt.Printf("[reader] PING err\n")
|
|
|
|
errorCode = PROTOCOL_ERR
|
|
|
|
break
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (6) Send PONG */
|
2018-05-03 17:31:09 +00:00
|
|
|
if msg.Type == PING {
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[reader] PING -> PONG\n")
|
2018-05-05 21:53:06 +00:00
|
|
|
if c.io.writing {
|
|
|
|
msg.Final = true
|
|
|
|
msg.Type = PONG
|
|
|
|
c.ch.send <- *msg
|
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
/* (7) Invalid UTF8 */
|
|
|
|
if msg.Type == TEXT && !utf8.Valid(msg.Data) {
|
|
|
|
fmt.Printf(" [reader] invalid utf-8\n")
|
|
|
|
errorCode = INVALID_PAYLOAD
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (8) Unknown opcode */
|
2018-05-03 17:31:09 +00:00
|
|
|
if msg.Type != TEXT && msg.Type != BINARY {
|
2018-05-05 15:38:07 +00:00
|
|
|
fmt.Printf(" [reader] unknown OpCode %d\n", msg.Type)
|
|
|
|
errorCode = PROTOCOL_ERR
|
|
|
|
break
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
/* (9) Dispatch to receiver */
|
2018-05-03 17:31:09 +00:00
|
|
|
c.ch.receive <- *msg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
close(c.ch.receive)
|
2018-05-05 15:38:07 +00:00
|
|
|
c.io.reading.Done()
|
|
|
|
|
|
|
|
/* (8) close channel (if not already done) */
|
|
|
|
// fmt.Printf("[reader] end\n")
|
|
|
|
c.close(errorCode, clientAck)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// writer writes into websocket
|
|
|
|
// and is triggered by client.ch.send channel
|
2018-05-05 15:38:07 +00:00
|
|
|
func clientWriter(c *client){
|
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = true // if channel still exists
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
for msg := range c.ch.send {
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (2) Send message */
|
2018-05-03 17:31:09 +00:00
|
|
|
err := msg.Send(c.io.sock)
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
/* (3) Fail on error */
|
2018-05-03 17:31:09 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" [writer] %s\n", err)
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = false
|
2018-05-03 17:31:09 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:43:16 +00:00
|
|
|
c.io.writing = false
|
2018-05-05 15:38:07 +00:00
|
|
|
|
|
|
|
/* (4) close channel (if not already done) */
|
|
|
|
// fmt.Printf("[writer] end\n")
|
|
|
|
c.close(NORMAL, true)
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// close writes the error message (if needed)
|
|
|
|
// and it closes the socket
|
2018-05-05 15:38:07 +00:00
|
|
|
// if 'clientACK' is true, reads the next message (CLOSE acknowledge)
|
|
|
|
// before closing the socket
|
|
|
|
func (c *client) close(status MessageError, clientACK bool){
|
|
|
|
|
|
|
|
/* (1) Fail if already closing */
|
|
|
|
alreadyClosing := false
|
|
|
|
c.io.closingMu.Lock()
|
|
|
|
alreadyClosing = c.io.closing
|
|
|
|
c.io.closing = true
|
|
|
|
c.io.closingMu.Unlock()
|
|
|
|
|
|
|
|
if alreadyClosing {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-05 16:47:38 +00:00
|
|
|
/* (2) kill writer' if still running */
|
2018-05-05 16:43:16 +00:00
|
|
|
if c.io.writing {
|
|
|
|
close(c.ch.send)
|
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-05 16:47:38 +00:00
|
|
|
/* (3) kill reader if still running */
|
|
|
|
c.io.sock.SetReadDeadline(time.Now().Add(time.Second*-1))
|
|
|
|
c.io.reading.Wait()
|
|
|
|
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
|
|
|
|
if status == NONE {
|
|
|
|
status = NORMAL
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Build message */
|
|
|
|
msg := &Message{
|
|
|
|
Final: true,
|
|
|
|
Type: CLOSE,
|
|
|
|
Size: 2,
|
|
|
|
Data: make([]byte, 2),
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint16(msg.Data, uint16(status))
|
|
|
|
|
|
|
|
/* (4) Send message */
|
|
|
|
err := msg.Send(c.io.sock)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[close] send error (%s0\n", err)
|
|
|
|
}
|
|
|
|
// fmt.Printf("[close] frame sent\n")
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Wait for client CLOSE if needed */
|
|
|
|
if clientACK {
|
|
|
|
|
|
|
|
c.io.sock.SetReadDeadline(time.Now().Add(time.Millisecond))
|
|
|
|
|
|
|
|
/* Wait for message */
|
|
|
|
msg, err := readMessage(c.io.reader)
|
|
|
|
if err != nil || msg.Type != CLOSE {
|
|
|
|
if err == nil {
|
|
|
|
fmt.Printf("[close] received OpCode = %d\n", msg.Type)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("[close] read error (%v)\n", err)
|
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[close] received ACK\n")
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
/* (3) Close socket */
|
2018-05-03 17:31:09 +00:00
|
|
|
c.io.sock.Close()
|
2018-05-05 15:38:07 +00:00
|
|
|
// fmt.Printf("[close] socket closed\n")
|
2018-05-03 17:31:09 +00:00
|
|
|
|
|
|
|
/* (4) Unregister */
|
|
|
|
c.io.kill <- c
|
|
|
|
|
2018-05-05 15:38:07 +00:00
|
|
|
return
|
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
}
|