Functions
Protocol behavior callback functions
Function client on_data (ctxt, payload)
//.code
end
-
ctxt - TCP client processing context
-
payload – event payload
-
payload.data - TCP data received, available as a stream of bytes
-
Policy extension functions
function self-type: function-name(parameter1: parameter1-type, and so on): return-type
statements
end
function NSTEXT:COMBINE_HEADERS() : NSTEXT
HTTP.REQ.FULL_HEADER.AFTER_STR("HTTP/1.1\r\n").COMBINE_HEADERS()
HTTP.REQ.FULL_HEADER.AFTER_STR("HTTP/1.1\r\n"), which is a text value. The result of the COMBINE_HEADERS() call is text, and since there is nothing to the right of this call, the result of the entire expression is text.
Local function definition
local function function-name(parameter1-name, parameter2-name, and so on)
statements
end
Function body and return
return -- returns nil
return expression -- one return value
return expression1, expression2, ... -- multiple return values
local function fsum(a)
local sum = 0
for i = 1, #a do
sum = sum + a[i]
end
return sum
end
Local function fsum_and_average(a)
local sum = 0
for i = 1, #a do
sum = sum + a[i]
end
return sum, sum/#a
end
Function calls
function f(p1, p2) -- p2 is optional
p2 = p2 or 0 -- if p2 is nil, set to a default of 0
. . .
end
local my_array = {1, 2, 3, 4}
local my_sum, my_ave = sum_and_average(my_array)
Iterator functions and generic for loops
for variable in iterator(parameter1, parameter2, and so on) do
statements in the for loop body
end
local t = {k1 = "v1", k2 = "v2", k3 = "v3"}
local a = {} -- array to accumulate key-value pairs
local n = 0 -- number of key-value pairs
for key, value in pairs(t) do
n = n + 1
a[n] = key.. " = ".. Value -- add key-value pair to the array
end
local s = table.concat(a, ";") -- concatenate all key-value pairs into one string
string.gmatch() function, which is used in the following COMBINE_HEADERS() example.