C# Class Bauglir.Ex.WebSocketServer

WebSocket server
Show file Open project: gwupe/Gwupe Class Usage Examples

Protected Properties

Property Type Description
connLock Object
fAddress System.Net.IPAddress
fConnections List
fIndex int
fIsRunning bool
fPort int
fSsl bool
fSslCertificate string
fTerminated bool
listener TcpListener

Public Methods

Method Description
CloseAllConnection ( int aCloseCode, string aCloseReason ) : void

close all server connections

GetConnection ( int index ) : WebSocketServerConnection

return connection by its position in list

GetConnectionByIndex ( int index ) : WebSocketServerConnection

return connection by its position Index property

GetConnectionInstance ( TcpClient aClient, Bauglir.Ex.WebSocketHeaders aHeaders, string aHost, string aPort, string aResourceName, string aOrigin, string aCookie, string aVersion, string &aProtocol, string &aExtension, int &aHttpCode ) : WebSocketServerConnection

Function to create WebSocketServerConnection instance. Function should return null if connection should not be accepted or some non 101 HTTP result code (see below) if application implementation needs to use it's own inplementation of WebSocketServerConnection (based on WebSocketServerConnection class) application should create new WebSocketServer (based on this one) and override this method to return its own instance of connection based on either application logic or passed parameters All parameters, except for aClient and aHttpCode refers to properties in WebSocketConnection, check the documentation of WebSocketConnection Function does not have to set up phis variables to result object properties, it's done automatically by server after object is returned.

LockConnections ( ) : void

this function should be used when application needs to travers through connections function locks the connection list for removal from different thread, no connection will be removed until UnlockConnections is called UnlockConnections MUST be called after traversing

Start ( IPAddress aAddress, int aPort ) : bool

start server true server is able to listen and has started, false if error occures or server is already running

Stop ( ) : void

Stop server

UnlockConnections ( ) : void

this function should be used when application needs to travers through connections function unlocks the connection list for removal from different thread, function should be called after LockConnections is called

WebSocketServer ( ) : System

constructor function

Protected Methods

Method Description
AddConnection ( TcpClient aClient, Stream aStream ) : WebSocketServerConnection

basic function to add WebSocket connection to list of connections

Execute ( ) : void

thread function this function actually waits for incomming connections and spawns new server threads

RemoveConnection ( WebSocketServerConnection aConnection ) : void
SafeRemoveConnection ( WebSocketServerConnection aConnection ) : void
httpCode ( int aCode ) : string

Method Details

AddConnection() protected method

basic function to add WebSocket connection to list of connections
protected AddConnection ( TcpClient aClient, Stream aStream ) : WebSocketServerConnection
aClient System.Net.Sockets.TcpClient incomming connection
aStream Stream
return WebSocketServerConnection

CloseAllConnection() public method

close all server connections
public CloseAllConnection ( int aCloseCode, string aCloseReason ) : void
aCloseCode int WebSocketCloseCode constant reason
aCloseReason string textual data (max 123 bytes)
return void

Execute() protected method

thread function this function actually waits for incomming connections and spawns new server threads
protected Execute ( ) : void
return void

GetConnection() public method

return connection by its position in list
public GetConnection ( int index ) : WebSocketServerConnection
index int position in list
return WebSocketServerConnection

GetConnectionByIndex() public method

return connection by its position Index property
public GetConnectionByIndex ( int index ) : WebSocketServerConnection
index int connection's Index ptoperty
return WebSocketServerConnection

GetConnectionInstance() public method

Function to create WebSocketServerConnection instance. Function should return null if connection should not be accepted or some non 101 HTTP result code (see below) if application implementation needs to use it's own inplementation of WebSocketServerConnection (based on WebSocketServerConnection class) application should create new WebSocketServer (based on this one) and override this method to return its own instance of connection based on either application logic or passed parameters All parameters, except for aClient and aHttpCode refers to properties in WebSocketConnection, check the documentation of WebSocketConnection Function does not have to set up phis variables to result object properties, it's done automatically by server after object is returned.
public GetConnectionInstance ( TcpClient aClient, Bauglir.Ex.WebSocketHeaders aHeaders, string aHost, string aPort, string aResourceName, string aOrigin, string aCookie, string aVersion, string &aProtocol, string &aExtension, int &aHttpCode ) : WebSocketServerConnection
aClient System.Net.Sockets.TcpClient TcpClient requesting connection, client MUST NOT be closed by this function
aHeaders Bauglir.Ex.WebSocketHeaders
aHost string
aPort string
aResourceName string
aOrigin string If "-" passed, no origin was passed by client
aCookie string If "-" passed, no cookies was passed by client
aVersion string
aProtocol string In value is list of protocol requested by client ("-" if non requested). Out values should be list of protocol supported by server based on client request, if "-" is returned, no protocol will be negotitated
aExtension string In value is list of extensions requested by client ("-" if non requested). Out values should be list of extensions supported by server based on client request, if "-" is returned, no extensions will be negotitated
aHttpCode int HTTP result code to return, if other than 101 is returned, connection will be automatically closed. 101 is default value
return WebSocketServerConnection

LockConnections() public method

this function should be used when application needs to travers through connections function locks the connection list for removal from different thread, no connection will be removed until UnlockConnections is called UnlockConnections MUST be called after traversing
public LockConnections ( ) : void
return void

RemoveConnection() protected method

protected RemoveConnection ( WebSocketServerConnection aConnection ) : void
aConnection WebSocketServerConnection
return void

SafeRemoveConnection() protected method

protected SafeRemoveConnection ( WebSocketServerConnection aConnection ) : void
aConnection WebSocketServerConnection
return void

Start() public method

start server true server is able to listen and has started, false if error occures or server is already running
public Start ( IPAddress aAddress, int aPort ) : bool
aAddress System.Net.IPAddress
aPort int
return bool

Stop() public method

Stop server
public Stop ( ) : void
return void

UnlockConnections() public method

this function should be used when application needs to travers through connections function unlocks the connection list for removal from different thread, function should be called after LockConnections is called
public UnlockConnections ( ) : void
return void

WebSocketServer() public method

constructor function
public WebSocketServer ( ) : System
return System

httpCode() protected method

protected httpCode ( int aCode ) : string
aCode int
return string

Property Details

connLock protected property

protected Object connLock
return Object

fAddress protected property

protected IPAddress,System.Net fAddress
return System.Net.IPAddress

fConnections protected property

protected List fConnections
return List

fIndex protected property

protected int fIndex
return int

fIsRunning protected property

protected bool fIsRunning
return bool

fPort protected property

protected int fPort
return int

fSsl protected property

protected bool fSsl
return bool

fSslCertificate protected property

protected string fSslCertificate
return string

fTerminated protected property

protected bool fTerminated
return bool

listener protected property

protected TcpListener listener
return TcpListener