2012-04-29 05:25:57 +02:00
|
|
|
package xgb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
var (
|
2012-05-17 05:57:26 +02:00
|
|
|
logger = newLogger()
|
2012-05-10 23:01:42 +02:00
|
|
|
|
|
|
|
// ExtLock is a lock used whenever new extensions are initialized.
|
|
|
|
// It should not be used. It is exported for use in the extension
|
|
|
|
// sub-packages.
|
|
|
|
ExtLock sync.Mutex
|
|
|
|
)
|
2012-05-08 03:58:33 +02:00
|
|
|
|
2012-04-29 05:25:57 +02:00
|
|
|
const (
|
2012-05-06 00:22:24 +02:00
|
|
|
// cookieBuffer represents the queue size of cookies existing at any
|
|
|
|
// point in time. The size of the buffer is really only important when
|
|
|
|
// there are many requests without replies made in sequence. Once the
|
|
|
|
// buffer fills, a round trip request is made to clear the buffer.
|
|
|
|
cookieBuffer = 1000
|
2012-05-08 03:58:33 +02:00
|
|
|
|
|
|
|
// xidBuffer represents the queue size of the xid channel.
|
|
|
|
// I don't think this value matters much, since xid generation is not
|
|
|
|
// that expensive.
|
|
|
|
xidBuffer = 5
|
|
|
|
|
|
|
|
// seqBuffer represents the queue size of the sequence number channel.
|
2013-01-26 18:51:48 +01:00
|
|
|
// I don't think this value matters much, since sequence number generation
|
2012-05-08 03:58:33 +02:00
|
|
|
// is not that expensive.
|
|
|
|
seqBuffer = 5
|
|
|
|
|
|
|
|
// reqBuffer represents the queue size of the number of requests that
|
|
|
|
// can be made until new ones block. This value seems OK.
|
|
|
|
reqBuffer = 100
|
|
|
|
|
|
|
|
// eventBuffer represents the queue size of the number of events or errors
|
|
|
|
// that can be loaded off the wire and not grabbed with WaitForEvent
|
|
|
|
// until reading an event blocks. This value should be big enough to handle
|
|
|
|
// bursts of events.
|
2012-10-17 05:40:59 +02:00
|
|
|
eventBuffer = 5000
|
2012-04-29 05:25:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A Conn represents a connection to an X server.
|
|
|
|
type Conn struct {
|
|
|
|
host string
|
|
|
|
conn net.Conn
|
|
|
|
display string
|
2013-01-26 18:51:21 +01:00
|
|
|
DisplayNumber int
|
2012-05-10 23:01:42 +02:00
|
|
|
DefaultScreen int
|
|
|
|
SetupBytes []byte
|
|
|
|
|
|
|
|
setupResourceIdBase uint32
|
|
|
|
setupResourceIdMask uint32
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-07 07:00:45 +02:00
|
|
|
eventChan chan eventOrError
|
2012-05-10 23:01:42 +02:00
|
|
|
cookieChan chan *Cookie
|
2012-05-07 07:00:45 +02:00
|
|
|
xidChan chan xid
|
|
|
|
seqChan chan uint16
|
|
|
|
reqChan chan *request
|
2013-08-12 01:33:56 +02:00
|
|
|
closing chan chan struct{}
|
2012-05-05 08:56:15 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// Extensions is a map from extension name to major opcode. It should
|
|
|
|
// not be used. It is exported for use in the extension sub-packages.
|
|
|
|
Extensions map[string]byte
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-04 04:47:50 +02:00
|
|
|
// NewConn creates a new connection instance. It initializes locks, data
|
|
|
|
// structures, and performs the initial handshake. (The code for the handshake
|
|
|
|
// has been relegated to conn.go.)
|
|
|
|
func NewConn() (*Conn, error) {
|
|
|
|
return NewConnDisplay("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnDisplay is just like NewConn, but allows a specific DISPLAY
|
|
|
|
// string to be used.
|
|
|
|
// If 'display' is empty it will be taken from os.Getenv("DISPLAY").
|
|
|
|
//
|
|
|
|
// Examples:
|
2012-05-27 00:22:25 +02:00
|
|
|
// NewConn(":1") -> net.Dial("unix", "", "/tmp/.X11-unix/X1")
|
|
|
|
// NewConn("/tmp/launch-12/:0") -> net.Dial("unix", "", "/tmp/launch-12/:0")
|
|
|
|
// NewConn("hostname:2.1") -> net.Dial("tcp", "", "hostname:6002")
|
|
|
|
// NewConn("tcp/hostname:1.0") -> net.Dial("tcp", "", "hostname:6001")
|
2012-05-04 04:47:50 +02:00
|
|
|
func NewConnDisplay(display string) (*Conn, error) {
|
|
|
|
conn := &Conn{}
|
|
|
|
|
|
|
|
// First connect. This reads authority, checks DISPLAY environment
|
|
|
|
// variable, and loads the initial Setup info.
|
|
|
|
err := conn.connect(display)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
conn.Extensions = make(map[string]byte)
|
2012-05-04 04:47:50 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
conn.cookieChan = make(chan *Cookie, cookieBuffer)
|
2012-05-08 03:58:33 +02:00
|
|
|
conn.xidChan = make(chan xid, xidBuffer)
|
|
|
|
conn.seqChan = make(chan uint16, seqBuffer)
|
|
|
|
conn.reqChan = make(chan *request, reqBuffer)
|
|
|
|
conn.eventChan = make(chan eventOrError, eventBuffer)
|
2013-08-12 01:33:56 +02:00
|
|
|
conn.closing = make(chan chan struct{}, 1)
|
2012-05-05 08:56:15 +02:00
|
|
|
|
|
|
|
go conn.generateXIds()
|
|
|
|
go conn.generateSeqIds()
|
|
|
|
go conn.sendRequests()
|
|
|
|
go conn.readResponses()
|
2012-05-04 04:47:50 +02:00
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
|
2013-08-12 01:33:56 +02:00
|
|
|
// Close gracefully closes the connection to the X server.
|
2012-05-04 04:47:50 +02:00
|
|
|
func (c *Conn) Close() {
|
2013-08-12 01:33:56 +02:00
|
|
|
close(c.reqChan)
|
2012-05-04 04:47:50 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 09:38:29 +02:00
|
|
|
// Event is an interface that can contain any of the events returned by the
|
|
|
|
// server. Use a type assertion switch to extract the Event structs.
|
|
|
|
type Event interface {
|
2012-05-05 08:56:15 +02:00
|
|
|
Bytes() []byte
|
2012-05-06 00:22:24 +02:00
|
|
|
String() string
|
2012-04-29 09:38:29 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewEventFun is the type of function use to construct events from raw bytes.
|
|
|
|
// It should not be used. It is exported for use in the extension sub-packages.
|
|
|
|
type NewEventFun func(buf []byte) Event
|
2012-05-07 07:00:45 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewEventFuncs is a map from event numbers to functions that create
|
|
|
|
// the corresponding event. It should not be used. It is exported for use
|
|
|
|
// in the extension sub-packages.
|
|
|
|
var NewEventFuncs = make(map[int]NewEventFun)
|
2012-05-07 07:00:45 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewExtEventFuncs is a temporary map that stores event constructor functions
|
2012-05-08 03:58:33 +02:00
|
|
|
// for each extension. When an extension is initialized, each event for that
|
2013-01-26 18:51:48 +01:00
|
|
|
// extension is added to the 'NewEventFuncs' map. It should not be used. It is
|
2012-05-10 23:01:42 +02:00
|
|
|
// exported for use in the extension sub-packages.
|
|
|
|
var NewExtEventFuncs = make(map[string]map[int]NewEventFun)
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-03 07:00:01 +02:00
|
|
|
// Error is an interface that can contain any of the errors returned by
|
2012-04-29 09:38:29 +02:00
|
|
|
// the server. Use a type assertion switch to extract the Error structs.
|
2012-05-03 07:00:01 +02:00
|
|
|
type Error interface {
|
|
|
|
SequenceId() uint16
|
2012-05-10 18:47:19 +02:00
|
|
|
BadId() uint32
|
2012-05-03 07:00:01 +02:00
|
|
|
Error() string
|
2012-04-29 09:38:29 +02:00
|
|
|
}
|
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewErrorFun is the type of function use to construct errors from raw bytes.
|
|
|
|
// It should not be used. It is exported for use in the extension sub-packages.
|
|
|
|
type NewErrorFun func(buf []byte) Error
|
2012-05-08 03:58:33 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewErrorFuncs is a map from error numbers to functions that create
|
|
|
|
// the corresponding error. It should not be used. It is exported for use in
|
|
|
|
// the extension sub-packages.
|
|
|
|
var NewErrorFuncs = make(map[int]NewErrorFun)
|
2012-05-08 03:58:33 +02:00
|
|
|
|
2012-05-10 23:01:42 +02:00
|
|
|
// NewExtErrorFuncs is a temporary map that stores error constructor functions
|
2012-05-08 03:58:33 +02:00
|
|
|
// for each extension. When an extension is initialized, each error for that
|
2012-05-10 23:01:42 +02:00
|
|
|
// extension is added to the 'NewErrorFuncs' map. It should not be used. It is
|
|
|
|
// exported for use in the extension sub-packages.
|
|
|
|
var NewExtErrorFuncs = make(map[string]map[int]NewErrorFun)
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// eventOrError corresponds to values that can be either an event or an
|
|
|
|
// error.
|
|
|
|
type eventOrError interface{}
|
|
|
|
|
2012-06-05 06:15:14 +02:00
|
|
|
// NewId generates a new unused ID for use with requests like CreateWindow.
|
2012-05-04 04:47:50 +02:00
|
|
|
// If no new ids can be generated, the id returned is 0 and error is non-nil.
|
2012-05-13 03:36:31 +02:00
|
|
|
// This shouldn't be used directly, and is exported for use in the extension
|
|
|
|
// sub-packages.
|
|
|
|
// If you need identifiers, use the appropriate constructor.
|
|
|
|
// e.g., For a window id, use xproto.NewWindowId. For
|
|
|
|
// a new pixmap id, use xproto.NewPixmapId. And so on.
|
2012-05-10 18:47:19 +02:00
|
|
|
func (c *Conn) NewId() (uint32, error) {
|
2012-05-04 04:47:50 +02:00
|
|
|
xid := <-c.xidChan
|
|
|
|
if xid.err != nil {
|
|
|
|
return 0, xid.err
|
|
|
|
}
|
|
|
|
return xid.id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// xid encapsulates a resource identifier being sent over the Conn.xidChan
|
2012-05-05 08:56:15 +02:00
|
|
|
// channel. If no new resource id can be generated, id is set to 0 and a
|
2012-05-04 04:47:50 +02:00
|
|
|
// non-nil error is set in xid.err.
|
|
|
|
type xid struct {
|
2012-05-10 18:47:19 +02:00
|
|
|
id uint32
|
2012-05-04 04:47:50 +02:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// generateXids sends new Ids down the channel for NewId to use.
|
2012-05-13 04:17:10 +02:00
|
|
|
// generateXids should be run in its own goroutine.
|
2012-05-04 04:47:50 +02:00
|
|
|
// This needs to be updated to use the XC Misc extension once we run out of
|
|
|
|
// new ids.
|
2012-05-05 08:56:15 +02:00
|
|
|
// Thanks to libxcb/src/xcb_xid.c. This code is greatly inspired by it.
|
|
|
|
func (conn *Conn) generateXIds() {
|
2012-05-13 04:17:10 +02:00
|
|
|
defer close(conn.xidChan)
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// This requires some explanation. From the horse's mouth:
|
2013-01-26 18:51:48 +01:00
|
|
|
// "The resource-id-mask contains a single contiguous set of bits (at least
|
|
|
|
// 18). The client allocates resource IDs for types WINDOW, PIXMAP,
|
|
|
|
// CURSOR, FONT, GCONTEXT, and COLORMAP by choosing a value with only some
|
|
|
|
// subset of these bits set and ORing it with resource-id-base. Only values
|
|
|
|
// constructed in this way can be used to name newly created resources over
|
2012-05-05 08:56:15 +02:00
|
|
|
// this connection."
|
|
|
|
// So for example (using 8 bit integers), the mask might look like:
|
|
|
|
// 00111000
|
|
|
|
// So that valid values would be 00101000, 00110000, 00001000, and so on.
|
|
|
|
// Thus, the idea is to increment it by the place of the last least
|
|
|
|
// significant '1'. In this case, that value would be 00001000. To get
|
|
|
|
// that value, we can AND the original mask with its two's complement:
|
|
|
|
// 00111000 & 11001000 = 00001000.
|
|
|
|
// And we use that value to increment the last resource id to get a new one.
|
|
|
|
// (And then, of course, we OR it with resource-id-base.)
|
2012-05-10 23:01:42 +02:00
|
|
|
inc := conn.setupResourceIdMask & -conn.setupResourceIdMask
|
|
|
|
max := conn.setupResourceIdMask
|
2012-05-04 04:47:50 +02:00
|
|
|
last := uint32(0)
|
|
|
|
for {
|
|
|
|
// TODO: Use the XC Misc extension to look for released ids.
|
2012-05-07 07:00:45 +02:00
|
|
|
if last > 0 && last >= max-inc+1 {
|
2012-05-04 04:47:50 +02:00
|
|
|
conn.xidChan <- xid{
|
2012-05-10 18:47:19 +02:00
|
|
|
id: 0,
|
2012-05-04 04:47:50 +02:00
|
|
|
err: errors.New("There are no more available resource" +
|
|
|
|
"identifiers."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
last += inc
|
|
|
|
conn.xidChan <- xid{
|
2012-05-10 23:01:42 +02:00
|
|
|
id: last | conn.setupResourceIdBase,
|
2012-05-04 04:47:50 +02:00
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// newSeqId fetches the next sequence id from the Conn.seqChan channel.
|
|
|
|
func (c *Conn) newSequenceId() uint16 {
|
|
|
|
return <-c.seqChan
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-13 04:17:10 +02:00
|
|
|
// generateSeqIds returns new sequence ids. It is meant to be run in its
|
|
|
|
// own goroutine.
|
2012-05-05 08:56:15 +02:00
|
|
|
// A sequence id is generated for *every* request. It's the identifier used
|
|
|
|
// to match up replies with requests.
|
2013-01-26 18:51:48 +01:00
|
|
|
// Since sequence ids can only be 16 bit integers we start over at zero when it
|
2012-05-05 08:56:15 +02:00
|
|
|
// comes time to wrap.
|
2012-05-06 00:22:24 +02:00
|
|
|
// N.B. As long as the cookie buffer is less than 2^16, there are no limitations
|
|
|
|
// on the number (or kind) of requests made in sequence.
|
2012-05-05 08:56:15 +02:00
|
|
|
func (c *Conn) generateSeqIds() {
|
2012-05-13 04:17:10 +02:00
|
|
|
defer close(c.seqChan)
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
seqid := uint16(1)
|
|
|
|
for {
|
|
|
|
c.seqChan <- seqid
|
2012-05-07 07:00:45 +02:00
|
|
|
if seqid == uint16((1<<16)-1) {
|
2012-05-05 08:56:15 +02:00
|
|
|
seqid = 0
|
2012-04-29 05:25:57 +02:00
|
|
|
} else {
|
2012-05-05 08:56:15 +02:00
|
|
|
seqid++
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// request encapsulates a buffer of raw bytes (containing the request data)
|
|
|
|
// and a cookie, which when combined represents a single request.
|
|
|
|
// The cookie is used to match up the reply/error.
|
|
|
|
type request struct {
|
2012-05-07 07:00:45 +02:00
|
|
|
buf []byte
|
2012-05-10 23:01:42 +02:00
|
|
|
cookie *Cookie
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2013-01-26 18:51:48 +01:00
|
|
|
// NewRequest takes the bytes and a cookie of a particular request, constructs
|
2012-05-27 00:22:25 +02:00
|
|
|
// a request type, and sends it over the Conn.reqChan channel.
|
2012-05-06 00:22:24 +02:00
|
|
|
// Note that the sequence number is added to the cookie after it is sent
|
2012-05-27 00:22:25 +02:00
|
|
|
// over the request channel, but before it is sent to X.
|
2012-06-05 06:15:14 +02:00
|
|
|
//
|
|
|
|
// Note that you may safely use NewRequest to send arbitrary byte requests
|
|
|
|
// to X. The resulting cookie can be used just like any normal cookie and
|
|
|
|
// abides by the same rules, except that for replies, you'll get back the
|
|
|
|
// raw byte data. This may be useful for performance critical sections where
|
|
|
|
// every allocation counts, since all X requests in XGB allocate a new byte
|
|
|
|
// slice. In contrast, NewRequest allocates one small request struct and
|
|
|
|
// nothing else. (Except when the cookie buffer is full and has to be flushed.)
|
|
|
|
//
|
|
|
|
// If you're using NewRequest manually, you'll need to use NewCookie to create
|
|
|
|
// a new cookie.
|
|
|
|
//
|
|
|
|
// In all likelihood, you should be able to copy and paste with some minor
|
|
|
|
// edits the generated code for the request you want to issue.
|
2012-05-10 23:01:42 +02:00
|
|
|
func (c *Conn) NewRequest(buf []byte, cookie *Cookie) {
|
2012-05-05 08:56:15 +02:00
|
|
|
c.reqChan <- &request{buf: buf, cookie: cookie}
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// sendRequests is run as a single goroutine that takes requests and writes
|
|
|
|
// the bytes to the wire and adds the cookie to the cookie queue.
|
2012-05-13 04:17:10 +02:00
|
|
|
// It is meant to be run as its own goroutine.
|
2012-05-05 08:56:15 +02:00
|
|
|
func (c *Conn) sendRequests() {
|
2012-05-13 04:17:10 +02:00
|
|
|
defer close(c.cookieChan)
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
for req := range c.reqChan {
|
2012-05-06 00:22:24 +02:00
|
|
|
// ho there! if the cookie channel is nearly full, force a round
|
|
|
|
// trip to clear out the cookie buffer.
|
|
|
|
// Note that we circumvent the request channel, because we're *in*
|
|
|
|
// the request channel.
|
2012-05-07 07:00:45 +02:00
|
|
|
if len(c.cookieChan) == cookieBuffer-1 {
|
2013-08-12 01:33:56 +02:00
|
|
|
c.noop()
|
2012-05-06 00:22:24 +02:00
|
|
|
}
|
|
|
|
req.cookie.Sequence = c.newSequenceId()
|
2012-05-05 08:56:15 +02:00
|
|
|
c.cookieChan <- req.cookie
|
2012-05-08 03:58:33 +02:00
|
|
|
c.writeBuffer(req.buf)
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
2013-08-12 01:33:56 +02:00
|
|
|
response := make(chan struct{})
|
|
|
|
c.closing <- response
|
|
|
|
c.noop() // Flush the response reading goroutine.
|
|
|
|
<-response
|
|
|
|
c.conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// noop circumvents the usual request sending goroutines and forces a round
|
|
|
|
// trip request manually.
|
|
|
|
func (c *Conn) noop() {
|
|
|
|
cookie := c.NewCookie(true, true)
|
|
|
|
cookie.Sequence = c.newSequenceId()
|
|
|
|
c.cookieChan <- cookie
|
|
|
|
c.writeBuffer(c.getInputFocusRequest())
|
|
|
|
cookie.Reply() // wait for the buffer to clear
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-06 00:22:24 +02:00
|
|
|
// writeBuffer is a convenience function for writing a byte slice to the wire.
|
2012-05-08 03:58:33 +02:00
|
|
|
func (c *Conn) writeBuffer(buf []byte) {
|
2012-05-06 00:22:24 +02:00
|
|
|
if _, err := c.conn.Write(buf); err != nil {
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Write error: %s", err)
|
|
|
|
logger.Fatal("A write error is unrecoverable. Exiting...")
|
2012-05-06 00:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// readResponses is a goroutine that reads events, errors and
|
|
|
|
// replies off the wire.
|
|
|
|
// When an event is read, it is always added to the event channel.
|
|
|
|
// When an error is read, if it corresponds to an existing checked cookie,
|
|
|
|
// it is sent to that cookie's error channel. Otherwise it is added to the
|
|
|
|
// event channel.
|
|
|
|
// When a reply is read, it is added to the corresponding cookie's reply
|
|
|
|
// channel. (It is an error if no such cookie exists in this case.)
|
|
|
|
// Finally, cookies that came "before" this reply are always cleaned up.
|
|
|
|
func (c *Conn) readResponses() {
|
2012-05-13 04:17:10 +02:00
|
|
|
defer close(c.eventChan)
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
var (
|
2012-05-07 07:00:45 +02:00
|
|
|
err Error
|
|
|
|
event Event
|
|
|
|
seq uint16
|
2012-05-05 08:56:15 +02:00
|
|
|
replyBytes []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
for {
|
2013-08-12 01:33:56 +02:00
|
|
|
select {
|
|
|
|
case respond := <-c.closing:
|
|
|
|
respond <- struct{}{}
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2012-05-09 05:03:45 +02:00
|
|
|
buf := make([]byte, 32)
|
2012-05-05 08:56:15 +02:00
|
|
|
err, event, seq = nil, nil, 0
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
if _, err := io.ReadFull(c.conn, buf); err != nil {
|
2013-08-12 01:33:56 +02:00
|
|
|
logger.Println("A read error is unrecoverable.")
|
|
|
|
panic(err)
|
2012-05-05 08:56:15 +02:00
|
|
|
}
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
switch buf[0] {
|
|
|
|
case 0: // This is an error
|
|
|
|
// Use the constructor function for this error (that is auto
|
|
|
|
// generated) by looking it up by the error number.
|
2012-05-10 23:01:42 +02:00
|
|
|
newErrFun, ok := NewErrorFuncs[int(buf[1])]
|
2012-05-06 00:22:24 +02:00
|
|
|
if !ok {
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("BUG: Could not find error constructor function "+
|
2012-05-08 03:58:33 +02:00
|
|
|
"for error with number %d.", buf[1])
|
2012-05-06 00:22:24 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = newErrFun(buf)
|
2012-05-05 08:56:15 +02:00
|
|
|
seq = err.SequenceId()
|
|
|
|
|
|
|
|
// This error is either sent to the event channel or a specific
|
|
|
|
// cookie's error channel below.
|
|
|
|
case 1: // This is a reply
|
|
|
|
seq = Get16(buf[2:])
|
|
|
|
|
|
|
|
// check to see if this reply has more bytes to be read
|
|
|
|
size := Get32(buf[4:])
|
|
|
|
if size > 0 {
|
2012-05-07 07:00:45 +02:00
|
|
|
byteCount := 32 + size*4
|
2012-05-05 08:56:15 +02:00
|
|
|
biggerBuf := make([]byte, byteCount)
|
|
|
|
copy(biggerBuf[:32], buf)
|
|
|
|
if _, err := io.ReadFull(c.conn, biggerBuf[32:]); err != nil {
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Read error: %s", err)
|
|
|
|
logger.Fatal("A read error is unrecoverable. Exiting...")
|
2012-05-05 08:56:15 +02:00
|
|
|
}
|
|
|
|
replyBytes = biggerBuf
|
|
|
|
} else {
|
|
|
|
replyBytes = buf
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// This reply is sent to its corresponding cookie below.
|
|
|
|
default: // This is an event
|
|
|
|
// Use the constructor function for this event (like for errors,
|
|
|
|
// and is also auto generated) by looking it up by the event number.
|
|
|
|
// Note that we AND the event number with 127 so that we ignore
|
|
|
|
// the most significant bit (which is set when it was sent from
|
|
|
|
// a SendEvent request).
|
2012-05-06 00:22:24 +02:00
|
|
|
evNum := int(buf[0] & 127)
|
2012-05-10 23:01:42 +02:00
|
|
|
newEventFun, ok := NewEventFuncs[evNum]
|
2012-05-06 00:22:24 +02:00
|
|
|
if !ok {
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("BUG: Could not find event construct function "+
|
2012-05-08 03:58:33 +02:00
|
|
|
"for event with number %d.", evNum)
|
2012-05-06 00:22:24 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
event = newEventFun(buf)
|
2012-05-05 08:56:15 +02:00
|
|
|
|
|
|
|
// Put the event into the queue.
|
2012-05-08 03:58:33 +02:00
|
|
|
// FIXME: I'm not sure if using a goroutine here to guarantee
|
|
|
|
// a non-blocking send is the right way to go. I should implement
|
|
|
|
// a proper dynamic queue.
|
2012-05-13 03:55:57 +02:00
|
|
|
// I am pretty sure this also loses a guarantee of events being
|
|
|
|
// processed in order of being received.
|
|
|
|
select {
|
|
|
|
case c.eventChan <- event:
|
|
|
|
default:
|
2012-05-08 03:58:33 +02:00
|
|
|
go func() {
|
2012-10-17 05:40:59 +02:00
|
|
|
println("overflowing...")
|
2012-05-08 03:58:33 +02:00
|
|
|
c.eventChan <- event
|
|
|
|
}()
|
|
|
|
}
|
2012-05-05 08:56:15 +02:00
|
|
|
|
|
|
|
// No more processing for events.
|
|
|
|
continue
|
|
|
|
}
|
2012-04-29 05:25:57 +02:00
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// At this point, we have a sequence number and we're either
|
|
|
|
// processing an error or a reply, which are both responses to
|
|
|
|
// requests. So all we have to do is find the cookie corresponding
|
|
|
|
// to this error/reply, and send the appropriate data to it.
|
|
|
|
// In doing so, we make sure that any cookies that came before it
|
|
|
|
// are marked as successful if they are void and checked.
|
|
|
|
// If there's a cookie that requires a reply that is before this
|
|
|
|
// reply, then something is wrong.
|
|
|
|
for cookie := range c.cookieChan {
|
|
|
|
// This is the cookie we're looking for. Process and break.
|
|
|
|
if cookie.Sequence == seq {
|
|
|
|
if err != nil { // this is an error to a request
|
|
|
|
// synchronous processing
|
|
|
|
if cookie.errorChan != nil {
|
|
|
|
cookie.errorChan <- err
|
|
|
|
} else { // asynchronous processing
|
|
|
|
c.eventChan <- err
|
2012-05-06 00:22:24 +02:00
|
|
|
// if this is an unchecked reply, ping the cookie too
|
|
|
|
if cookie.pingChan != nil {
|
|
|
|
cookie.pingChan <- true
|
|
|
|
}
|
2012-05-05 08:56:15 +02:00
|
|
|
}
|
|
|
|
} else { // this is a reply
|
|
|
|
if cookie.replyChan == nil {
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Reply with sequence id %d does not "+
|
2012-05-08 06:27:00 +02:00
|
|
|
"have a cookie with a valid reply channel.", seq)
|
2012-05-06 00:22:24 +02:00
|
|
|
continue
|
2012-05-05 08:56:15 +02:00
|
|
|
} else {
|
|
|
|
cookie.replyChan <- replyBytes
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 08:56:15 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// Checked requests with replies
|
|
|
|
case cookie.replyChan != nil && cookie.errorChan != nil:
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Found cookie with sequence id %d that is "+
|
2012-05-08 03:58:43 +02:00
|
|
|
"expecting a reply but will never get it. Currently "+
|
2012-05-08 03:58:33 +02:00
|
|
|
"on sequence number %d", cookie.Sequence, seq)
|
2012-05-05 08:56:15 +02:00
|
|
|
// Unchecked requests with replies
|
|
|
|
case cookie.replyChan != nil && cookie.pingChan != nil:
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Found cookie with sequence id %d that is "+
|
2012-05-08 03:58:43 +02:00
|
|
|
"expecting a reply (and not an error) but will never "+
|
2012-05-08 03:58:33 +02:00
|
|
|
"get it. Currently on sequence number %d",
|
2012-05-07 07:11:41 +02:00
|
|
|
cookie.Sequence, seq)
|
2012-05-05 08:56:15 +02:00
|
|
|
// Checked requests without replies
|
|
|
|
case cookie.pingChan != nil && cookie.errorChan != nil:
|
|
|
|
cookie.pingChan <- true
|
2012-05-07 07:00:45 +02:00
|
|
|
// Unchecked requests without replies don't have any channels,
|
|
|
|
// so we can't do anything with them except let them pass by.
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-05 08:56:15 +02:00
|
|
|
}
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
|
2012-05-05 08:56:15 +02:00
|
|
|
// processEventOrError takes an eventOrError, type switches on it,
|
|
|
|
// and returns it in Go idiomatic style.
|
|
|
|
func processEventOrError(everr eventOrError) (Event, Error) {
|
|
|
|
switch ee := everr.(type) {
|
|
|
|
case Event:
|
|
|
|
return ee, nil
|
|
|
|
case Error:
|
|
|
|
return nil, ee
|
|
|
|
default:
|
2012-05-17 05:57:26 +02:00
|
|
|
logger.Printf("Invalid event/error type: %T", everr)
|
2012-05-06 00:22:24 +02:00
|
|
|
return nil, nil
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForEvent returns the next event from the server.
|
|
|
|
// It will block until an event is available.
|
2012-05-27 00:22:25 +02:00
|
|
|
// WaitForEvent returns either an Event or an Error. (Returning neither or both
|
|
|
|
// is a bug.) Note than an Error here is an X error and not an XGB error. That
|
|
|
|
// is, X errors are sometimes completely expected (and you may want to ignore
|
|
|
|
// them in some cases).
|
2012-05-05 08:56:15 +02:00
|
|
|
func (c *Conn) WaitForEvent() (Event, Error) {
|
|
|
|
return processEventOrError(<-c.eventChan)
|
|
|
|
}
|
|
|
|
|
2013-01-26 18:51:48 +01:00
|
|
|
// PollForEvent returns the next event from the server if one is available in
|
|
|
|
// the internal queue without blocking. Note that unlike WaitForEvent, both
|
|
|
|
// Event and Error could be nil. Indeed, they are both nil when the event queue
|
2012-05-27 00:22:25 +02:00
|
|
|
// is empty.
|
2012-05-05 08:56:15 +02:00
|
|
|
func (c *Conn) PollForEvent() (Event, Error) {
|
|
|
|
select {
|
|
|
|
case everr := <-c.eventChan:
|
|
|
|
return processEventOrError(everr)
|
|
|
|
default:
|
|
|
|
return nil, nil
|
2012-04-29 05:25:57 +02:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|