JSDoc: Class: TCPConnection

# Class: TCPConnection

## server.TCPConnection(connID, transport, driverFactory)

Marionette client connection.

Dispatches packets received to their correct service destinations and sends back the service endpoint's return values.

## Constructor

#### new TCPConnection(connID, transport, driverFactory)

##### Parameters:

Name | Type | Description
--- | --- | ---
`connID` | number | Unique identifier of the connection this dispatcher should handle.
`transport` | DebuggerTransport | Debugger transport connection to the client.
`driverFactory` | function | Factory function that produces a |GeckoDriver|.

Source:
  - server.js, line 438

### Methods

#### createResponse(msgID) → {message.Response}

Fail-safe creation of a new instance of |message.Response|.

##### Parameters:

Name | Type | Description
--- | --- | ---
`msgID` | number | Message ID to respond to. If it is not a number, -1 is used.

Source:
  - server.js, line 583

##### Returns:

Response to the message with |msgID|.

Type
  message.Response

#### (async) despatch(cmd, resp)

Despatches command to appropriate Marionette service.

##### Parameters:

Name | Type | Description
--- | --- | ---
`cmd` | Command | Command to run.
`resp` | Response | Mutable response where the command's return value will be assigned.

Source:
  - server.js, line 553

##### Throws:

A command's implementation may throw at any time.

Type
  Error

#### (async) execute(cmd)

Executes a Marionette command and sends back a response when it has finished executing.

If the command implementation sends the response itself by calling `resp.send()`, the response is guaranteed to not be sent twice.

Errors thrown in commands are marshaled and sent back, and if they are not `WebDriverError` instances, they are additionally propagated and reported to `Components.utils.reportError`.

##### Parameters:

Name | Type | Description
--- | --- | ---
`cmd` | Command | Command to execute.

Source:
  - server.js, line 532

#### onClosed()

Debugger transport callback that cleans up after a connection is closed.

Source:
  - server.js, line 463

#### onPacket(data)

Callback that receives data packets from the client.

If the message is a Response, we look up the command previously issued to the client and run its callback, if any. In case of a Command, the corresponding is executed.

##### Parameters:

Name | Type | Description
--- | --- | ---
`data` | Array.<number, number, ?, ?> | A four element array where the elements, in sequence, signifies message type, message ID, method name or error, and parameters or result.

Source:
  - server.js, line 482

#### sayHello()

When a client connects we send across a JSON Object defining the protocol level.

This is the only message sent by Marionette that does not follow the regular message format.

Source:
  - server.js, line 602

#### send(msg)

Delegates message to client based on the provided {@code cmdID}. The message is sent over the debugger transport socket.

The command ID is a unique identifier assigned to the client's request that is used to distinguish the asynchronous responses.

Whilst responses to commands are synchronous and must be sent in the correct order.

##### Parameters:

Name | Type | Description
--- | --- | ---
`msg` | Message | The command or response to send.

Source:
  - server.js, line 623

#### sendMessage(msg)

Marshal message to the Marionette message format and send it.

##### Parameters:

Name | Type | Description
--- | --- | ---
`msg` | Message | The message to send.

Source:
  - server.js, line 652

#### sendRaw(payload)

Send the given payload over the debugger transport socket to the connected client.

##### Parameters:

Name | Type | Description
--- | --- | ---
`payload` | Object.<string, ?> | The payload to ship.

Source:
  - server.js, line 665

#### sendToClient(resp)

Send given response to the client over the debugger transport socket.

##### Parameters:

Name | Type | Description
--- | --- | ---
`resp` | Response | The response to send back to the client.

Source:
  - server.js, line 641