GetNetworkStats: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
 
(5 intermediate revisions by 3 users not shown)
Line 2: Line 2:
{{Server client function}}
{{Server client function}}
This function returns network status information.
This function returns network status information.
{{New feature|3.0110|1.1|
Available in 1.1 and onwards
}}


==Syntax==
==Syntax==
Line 15: Line 11:
<section name="Server" class="server" show="true">
<section name="Server" class="server" show="true">
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table getNetworkStats ( element thePlayer )
table getNetworkStats ( [ element thePlayer = nil ] )
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>
===Optional Arguments===
*'''thePlayer:''' The player you want to retrieve network stats from.


===Returns===
===Returns===
Returns a table of network information:
Returns a table, the indexes in the table are the following:
 
* '''bytesReceived''' - Total number of bytes received since the connection was started
* '''bytesSent''' - Total number of bytes sent since the connection was started
* '''packetsReceived''' - Total number of packets received since the connection was started
* '''packetsSent''' - Total number of packets sent since the connection was started
* '''packetlossTotal''' - (0-100) Total packet loss percentage of sent data, since the connection was started
* '''packetlossLastSecond''' - (0-100) Packet loss percentage of sent data, during the previous second
* '''messagesInSendBuffer'''
* '''messagesInResendBuffer''' - Number of packets queued to be resent (due to packet loss)
* '''isLimitedByCongestionControl'''
* '''isLimitedByOutgoingBandwidthLimit'''
* '''encryptionStatus'''
 
==Example==
<section name="Client" class="client" show="true">
This example outputs the local players network status information to their console when using the /netstatus command
<syntaxhighlight lang="lua">
function netStatus()
for index, value in pairs(getNetworkStats()) do
outputConsole(index..": "..value)
end
outputChatBox("Network status output to console", 0, 255, 0)
end
addCommandHandler("netstatus", netStatus)
</syntaxhighlight>
This example outputs a warning to local player if packet loss occured in the last second
<syntaxhighlight lang="lua">
function packetLossCheck()
local loss = getNetworkStats()["packetlossLastSecond"]
if (loss > 0) then
outputChatBox("Packet loss detected when communicating with server, gameplay may be affected", 255, 0, 0)
end
end
setTimer(packetLossCheck, 1000, 0)
</syntaxhighlight>
This example tracks the average and peak packetloss over the last 60 seconds '''PLEASE NOTE:''' this example is untested.
<syntaxhighlight lang="lua">
PACKETLOSS_HISTORY_LENGTH = 60 -- Sample period in seconds
packetlossHistory = {}
packetlossAvg = 0 -- (Output) Average packet loss over last 60 seconds
packetlossPeak = 0 -- (Output) Peak packet loss over last 60 seconds


* "bytesReceived"
function samplePacketLoss()
* "bytesSent"
table.insert( packetlossHistory, getNetworkStats().packetlossLastSecond )
* "packetsReceived"
while( #packetlossHistory > PACKETLOSS_HISTORY_LENGTH ) do
* "packetsSent"
table.remove( packetlossHistory, 1 )
* "packetlossTotal"
end
* "packetlossLastSecond"
packetlossAvg = 0
* "messagesInSendBuffer"
packetlossPeak = 0
* "messagesInResendBuffer"
for _,value in ipairs(packetlossHistory) do
* "isLimitedByCongestionControl"
packetlossAvg = packetlossAvg + value
* "isLimitedByOutgoingBandwidthLimit"
packetlossPeak = math.max( packetlossPeak, value )
* "encryptionStatus"
end
packetlossAvg = packetlossAvg / #packetlossHistory
end


'''Note: this page is unfinished (not sure on the actual table structure)'''
setTimer(samplePacketLoss,1000,0)
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Client_utility_functions}}
{{Client_utility_functions}}

Latest revision as of 02:10, 11 April 2012

This function returns network status information.

Syntax

Click to collapse [-]
Client
table getNetworkStats ( )
Click to collapse [-]
Server
table getNetworkStats ( [ element thePlayer = nil ] )

Optional Arguments

  • thePlayer: The player you want to retrieve network stats from.

Returns

Returns a table, the indexes in the table are the following:

  • bytesReceived - Total number of bytes received since the connection was started
  • bytesSent - Total number of bytes sent since the connection was started
  • packetsReceived - Total number of packets received since the connection was started
  • packetsSent - Total number of packets sent since the connection was started
  • packetlossTotal - (0-100) Total packet loss percentage of sent data, since the connection was started
  • packetlossLastSecond - (0-100) Packet loss percentage of sent data, during the previous second
  • messagesInSendBuffer
  • messagesInResendBuffer - Number of packets queued to be resent (due to packet loss)
  • isLimitedByCongestionControl
  • isLimitedByOutgoingBandwidthLimit
  • encryptionStatus

Example

Click to collapse [-]
Client

This example outputs the local players network status information to their console when using the /netstatus command

function netStatus()
	for index, value in pairs(getNetworkStats()) do
		outputConsole(index..": "..value)
	end
	outputChatBox("Network status output to console", 0, 255, 0)
end
addCommandHandler("netstatus", netStatus)

This example outputs a warning to local player if packet loss occured in the last second

function packetLossCheck()
	local loss = getNetworkStats()["packetlossLastSecond"]
	if (loss > 0) then
		outputChatBox("Packet loss detected when communicating with server, gameplay may be affected", 255, 0, 0)
	end
end
setTimer(packetLossCheck, 1000, 0)

This example tracks the average and peak packetloss over the last 60 seconds PLEASE NOTE: this example is untested.

PACKETLOSS_HISTORY_LENGTH = 60	-- Sample period in seconds
packetlossHistory = {}
packetlossAvg = 0		-- (Output) Average packet loss over last 60 seconds
packetlossPeak = 0		-- (Output) Peak packet loss over last 60 seconds

function samplePacketLoss()
	table.insert( packetlossHistory, getNetworkStats().packetlossLastSecond )
	while( #packetlossHistory > PACKETLOSS_HISTORY_LENGTH ) do
		table.remove( packetlossHistory, 1 )
	end
	packetlossAvg = 0
	packetlossPeak = 0
	for _,value in ipairs(packetlossHistory) do
		packetlossAvg = packetlossAvg + value
		packetlossPeak = math.max( packetlossPeak, value )
	end
	packetlossAvg = packetlossAvg / #packetlossHistory
end

setTimer(samplePacketLoss,1000,0)

See Also