cNet (Copper Networking)
cNet is the networking library of Copper OS.
Libraries
CryptoNet
cNet.CryptoNet
can be used to access functions of the CryptoNet library. However, this is rarely needed
Enums
MessageEventType
The possible types of the CryptoNet message event.
Event Type | Trigger |
---|---|
CONNECTION_CLOSED |
Client disconnected from server |
CONNECTION_OPENED |
Client connected to server |
ENCRYPTED_MESSAGE |
Encrypted message is received |
LOGIN |
Client successfully logged in |
LOGIN_FAILED |
Client failed to log in |
LOGOUT |
Client logged out |
MODEM_MESSAGE |
Modem message is received |
PLAIN_MESSAGE |
Unencrypted message is received |
CttpRequestMethod
The possible methods for a CTTP Request. These change nothing about the functionality of the request, but help to organize them.
CttpStatus
The possible statuses for CTTP Responses.
Classes
CttpRequest
requestMethod |
The method of the request |
path |
|
header |
The header of the request |
data |
Any data that is sent with the request |
- new(
requestMethod
: CttpRequestMethod,path
: string,header?
: CttpHeader,data?
: any) : CttpRequest
Creates a new cttprequest
CttpResponse
statusCode |
The method of the response |
statusMessage |
|
header |
The header of the response |
data |
Any data that is sent with the response |
- new(
status
: CttpStatus,header?
: CttpHeader,data?
: any) : CttpResponse
Creates a new cttpresponse
CtcpConnection
Socket |
The CryptoNet Socket |
seqNumber |
The CTCP sequence number |
ackNumber |
The CTCP acknowledgment number |
Creates a new ctcpconnection
Functions
Calculates a checksum for any serializable object
-- Checksum Example
local cNet = require "cNet"
local a = { a=1, b=2, c=3 }
local b = { a=1, b=2, c=3 }
if cNet.checksum(a) == cNet.checksum(b) then
print("A and B are equal")
else
print("A and B are different")
end
- startEventLoop(
onStart
: function)
Starts the CryptoNet event loop.
onStart
is a callback function that is called when CryptoNet is set up.
-- Hosting Example
local cNet = require "cNet"
cNet.setRestApi("GET", "/path", function ()
return {
status = 200,
headers = {
["Content-Type"] = "text/plain"
},
body = "Hello, World!"
}
end)
cNet.startEventLoop(function ()
cNet.host("server-id", "top")
end)
-- Hosting Example
local cNet = require "cNet"
cNet.setRestApi("GET", "/path", function ()
return {
status = 200,
headers = {
["Content-Type"] = "text/plain"
},
body = "Hello, World!"
}
end)
cNet.startEventLoop(function ()
cNet.host("server-id", "top")
end)
- connectCtcp(
serverId
: string,timeout?
: number,modemQuery?
: string) : CtcpConnection/nil
Opens a connection to a server using the CTCP Handshake
-- CTTP Request Example
local cNet = require "cNet"
cNet.startEventLoop(function ()
local connection = cNet.connectCtcp("server-id", 10, "top")
if connection == nil then
print("Failed to connect")
return
end
local request = cNet.CttpRequest:new("GET", "/path")
local response = cNet.sendCttpRequest(connection, request, 10)
if response == nil then
print("Timeout was reached before receiving a response")
return
end
print("Status: " .. response.statusCode)
print("Status Message: " .. response.statusMessage)
local acknowledged = cNet.disconnectCtcp(connection, 10)
if not acknowledged then
print("Server did not acknowledge the disconnect. Disconnected anyway.")
end
end)
- disconnectCtcp(
ctcpConnection
: CtcpConnection,timeout?
: number) : boolean
Closes a CTCP connection using the Finishing Handshake.
-- CTTP Request Example
local cNet = require "cNet"
cNet.startEventLoop(function ()
local connection = cNet.connectCtcp("server-id", 10, "top")
if connection == nil then
print("Failed to connect")
return
end
local request = cNet.CttpRequest:new("GET", "/path")
local response = cNet.sendCttpRequest(connection, request, 10)
if response == nil then
print("Timeout was reached before receiving a response")
return
end
print("Status: " .. response.statusCode)
print("Status Message: " .. response.statusMessage)
local acknowledged = cNet.disconnectCtcp(connection, 10)
if not acknowledged then
print("Server did not acknowledge the disconnect. Disconnected anyway.")
end
end)
- sendCttpRequest(
ctcpConnection
: CtcpConnection,request
: CttpRequest,timeout
: number) : CttpResponse/nil, boolean
Sends a CTTP Request to a server.
-- CTTP Request Example
local cNet = require "cNet"
cNet.startEventLoop(function ()
local connection = cNet.connectCtcp("server-id", 10, "top")
if connection == nil then
print("Failed to connect")
return
end
local request = cNet.CttpRequest:new("GET", "/path")
local response = cNet.sendCttpRequest(connection, request, 10)
if response == nil then
print("Timeout was reached before receiving a response")
return
end
print("Status: " .. response.statusCode)
print("Status Message: " .. response.statusMessage)
local acknowledged = cNet.disconnectCtcp(connection, 10)
if not acknowledged then
print("Server did not acknowledge the disconnect. Disconnected anyway.")
end
end)
- connectAndSendCttpRequest(
serverId
: string,request
: CttpRequest,timeout?
: number,modemQuery?
: string) : CttpResponse/nil, boolean, boolean, boolean
Connects to a server, sends a CTTP Request and disconnects.
Returns the response, if the connection was successful, if the request was acknowledged and if the disconnect was acknowledged.
-- Short CTTP Request Example
local cNet = require "cNet"
cNet.startEventLoop(function ()
local request = cNet.CttpRequest:new("GET", "/path")
local response, connectAck, requestAck, disconnectAck = cNet.connectAndSendCttpRequest("server-id", request, 10, "top")
if not connectAck then
print("Failed to connect to server")
return
end
if not requestAck then
print("Failed to send request")
return
end
if response == nil then
print("Timeout was reached before receiving a response")
return
end
if not disconnectAck then
print("Disconnect was not acknowledged. Disconnected anyway.")
return
end
end)
- setMessageHandler(
eventType
: MessageEventType,handler
: function)
Sets a custom handler for CryptoNet Message Events
-- Custom Message Handler Example
- setRestApi(
requestMethod
: CttpRequestMethod,path
: string,handler
: function)
Sets a handler for a CTTP Request
-- Hosting Example
local cNet = require "cNet"
cNet.setRestApi("GET", "/path", function ()
return {
status = 200,
headers = {
["Content-Type"] = "text/plain"
},
body = "Hello, World!"
}
end)
cNet.startEventLoop(function ()
cNet.host("server-id", "top")
end)
- register(
serverId
: string,username
: string,password
: string,timeout
: number,modemQuery
: string) : integer
-- Login Example
- login(
connection
: CtcpConnection/table,username
: string,password
: string)
-- Login Example
- logout(
connection
: CtcpConnection)
-- Login Example
- setLogger(
logger
: function)
-- Setting Logger Example
local cNet = require "cNet"
local function log(text)
print("prefix " .. text)
end
cNet.setLogger(log)