8. 网络套接字库

NETWORK 模块实现了基本的 TCP 套接字监听服务器(目前只有一个连接)。 它最终也将扩展到支持客户端。

其目前的形式用于 Daslang Visual Studio Code 插件和即将推出的调试服务器。

所有函数和符号都在 “network ”模块中,使用 require 可以访问该模块。

require network

8.1. 处理结构

NetworkServer

Base impliemntation of the server.

8.2.

Server

Single socket listener combined with single socket connection.

它的定义如下

_server : smart_ptr< network::NetworkServer >
Server.make_server_adapter(self: Server)

Creates new instance of the server adapter. Adapter is responsible for communicating with the Server class.

Server.init(self: Server; port: int const)

init returns bool

argument

argument type

self

network::Server

port

int const

Initializes server with specific port

Server.restore(self: Server; shared_orphan: smart_ptr<NetworkServer>&)

argument

argument type

self

network::Server

shared_orphan

smart_ptr< network::NetworkServer >&

Restore server state from after the context switch.

Server.save(self: Server; shared_orphan: smart_ptr<NetworkServer>&)

argument

argument type

self

network::Server

shared_orphan

smart_ptr< network::NetworkServer >&

Saves server to orphaned state to support context switching and live reloading. The idea is that server is saved to the orphaned state, which is not part of the context state.

Server.has_session(self: Server)

has_session returns bool

Returns true if network session already exists. This is used to determine if the server should be initialized or not.

Server.is_open(self: Server)

is_open returns bool

Returns true if server is listening to the port.

Server.is_connected(self: Server)

is_connected returns bool

Returns true if server is connected to the client.

Server.tick(self: Server)

This needs to be called periodically to support the server communication and connections.

Server.send(self: Server; data: uint8? const; size: int const)

send returns bool

argument

argument type

self

network::Server

data

uint8? const

size

int const

Send data.

Server.onConnect(self: Server)

This callback is called when server accepts the connection.

Server.onDisconnect(self: Server)

This callback is called when server or client drops the connection.

Server.onData(self: Server; buf: uint8? const; size: int const)

argument

argument type

self

network::Server

buf

uint8? const

size

int const

This callback is called when data is received from the client.

Server.onError(self: Server; msg: string const; code: int const)

argument

argument type

self

network::Server

msg

string const

code

int const

This callback is called on any error.

Server.onLog(self: Server; msg: string const)

argument

argument type

self

network::Server

msg

string const

This is how server logs are printed.

8.3. Low lever NetworkServer IO

make_server(class: void? const implicit; info: StructInfo const? const implicit)

make_server returns bool

argument

argument type

class

void? const implicit

info

rtti::StructInfo const? const implicit

Creates new instance of the server.

server_init(server: smart_ptr<NetworkServer> const implicit; port: int const)

server_init returns bool

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

port

int const

Initializes server with given port.

server_is_open(server: smart_ptr<NetworkServer> const implicit)

server_is_open returns bool

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

Returns true if server is listening to the port.

server_is_connected(server: smart_ptr<NetworkServer> const implicit)

server_is_connected returns bool

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

Returns true if server is connected to the client.

server_tick(server: smart_ptr<NetworkServer> const implicit)

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

This needs to be called periodically for the server to work.

server_send(server: smart_ptr<NetworkServer> const implicit; data: uint8? const implicit; size: int const)

server_send returns bool

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

data

uint8? const implicit

size

int const

Sends data from server to the client.

server_restore(server: smart_ptr<NetworkServer> const implicit; class: void? const implicit; info: StructInfo const? const implicit)

argument

argument type

server

smart_ptr< network::NetworkServer > const implicit

class

void? const implicit

info

rtti::StructInfo const? const implicit

Restores server from orphaned state.