Skip to content

Commit

Permalink
updated reconnect logic + fix for v5.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
evolutionleo committed Mar 9, 2023
1 parent 0c1c3dc commit 8a36ac6
Show file tree
Hide file tree
Showing 25 changed files with 600 additions and 48 deletions.
10 changes: 5 additions & 5 deletions Client/Client.resource_order
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
{"name":"SimpleUIBinding","order":3,"path":"scripts/SimpleUIBinding/SimpleUIBinding.yy",},
{"name":"oSimpleUI","order":2,"path":"objects/oSimpleUI/oSimpleUI.yy",},
{"name":"SUIText","order":4,"path":"scripts/SUIText/SUIText.yy",},
{"name":"NetworkingEvents","order":4,"path":"scripts/NetworkingEvents/NetworkingEvents.yy",},
{"name":"WarpEvents","order":4,"path":"scripts/WarpEvents/WarpEvents.yy",},
{"name":"SUISprite","order":3,"path":"scripts/SUISprite/SUISprite.yy",},
{"name":"__NetworkingConfig","order":2,"path":"scripts/__NetworkingConfig/__NetworkingConfig.yy",},
{"name":"__WarpConfig","order":2,"path":"scripts/__WarpConfig/__WarpConfig.yy",},
{"name":"oMainMenu","order":4,"path":"objects/oMainMenu/oMainMenu.yy",},
{"name":"sSimpleUITextCursor","order":3,"path":"sprites/sSimpleUITextCursor/sSimpleUITextCursor.yy",},
{"name":"sWall","order":1,"path":"sprites/sWall/sWall.yy",},
{"name":"Timestamps","order":3,"path":"scripts/Timestamps/Timestamps.yy",},
{"name":"NetworkTimestamps","order":3,"path":"scripts/NetworkTimestamps/NetworkTimestamps.yy",},
{"name":"PacketQueue","order":2,"path":"scripts/PacketQueue/PacketQueue.yy",},
{"name":"oPlayer","order":2,"path":"objects/oPlayer/oPlayer.yy",},
{"name":"oWall","order":1,"path":"objects/oWall/oWall.yy",},
{"name":"rLogin","order":1,"path":"rooms/rLogin/rLogin.yy",},
{"name":"NetworkInitializers","order":3,"path":"scripts/NetworkInitializers/NetworkInitializers.yy",},
{"name":"WarpInitializers","order":3,"path":"scripts/WarpInitializers/WarpInitializers.yy",},
{"name":"network_write","order":3,"path":"scripts/network_write/network_write.yy",},
{"name":"rTest2","order":2,"path":"rooms/rTest2/rTest2.yy",},
{"name":"rTest","order":1,"path":"rooms/rTest/rTest.yy",},
Expand All @@ -63,7 +63,7 @@
{"name":"oDualInstance","order":1,"path":"objects/oDualInstance/oDualInstance.yy",},
{"name":"oWarpPortal","order":3,"path":"objects/oWarpPortal/oWarpPortal.yy",},
{"name":"SUIButton","order":2,"path":"scripts/SUIButton/SUIButton.yy",},
{"name":"Interpolation","order":5,"path":"scripts/Interpolation/Interpolation.yy",},
{"name":"NetworkInterpolation","order":5,"path":"scripts/NetworkInterpolation/NetworkInterpolation.yy",},
{"name":"oFriendList","order":6,"path":"objects/oFriendList/oFriendList.yy",},
{"name":"SimpleUI","order":2,"path":"scripts/SimpleUI/SimpleUI.yy",},
{"name":"rMenu","order":2,"path":"rooms/rMenu/rMenu.yy",},
Expand Down
10 changes: 5 additions & 5 deletions Client/Client.yyp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions Client/objects/oClient/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
#region Connect!

connect = function() {
// if a packet is split in half, we use this
// -1 if no half-pack, a buffer otherwise
if (connected)
disconnect()

connecting = true
connected = false

// if a packet is split in half, we use this
// -1 if no half-pack, a buffer otherwise
halfpack = -1

alarm[0] = CONNECT_TIMEOUT

if (variable_instance_exists(id, "socket"))
network_destroy(socket)

socket = network_create_socket(SOCKET_TYPE)
var port

Expand All @@ -28,6 +29,19 @@ connect = function() {
network_connect_raw_async(socket, IP, real(port));
}

disconnect = function() {
if (connected) {
if (socket)
network_destroy(socket)

connected = false
connecting = false
socket = undefined
global.ping = -1
onDisconnect()
}
}

// connect/disconnect events are defined in __NetworkingConfig.gml
//onConnect = global.onConnect
//onDisconnect = global.onDisconnect
Expand All @@ -38,5 +52,6 @@ packet_queue = []

connecting = false
connected = false
socket = undefined

connect()
9 changes: 6 additions & 3 deletions Client/objects/oClient/Other_68.gml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ switch(type) {
case network_type_non_blocking_connect:
if (!async_load[? "succeeded"]) {
trace("Non-blocking connect failed.")
connecting = false
connected = false
break
} // otherwise fall into the connect case
case network_type_connect:
Expand All @@ -103,8 +105,9 @@ switch(type) {
break
case network_type_disconnect:
trace("Disconnected from the server!")
connected = false
connecting = false
onDisconnect()
disconnect()
//connected = false
//connecting = false
//onDisconnect()
break
}
14 changes: 10 additions & 4 deletions Client/objects/oEntityManager/Step_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ if (base_update == -1) exit;

base_update = min(base_update, len)

//array_delete(entity_updates, 0, max(0, base_update-1))
//base_update = min(base_update, 0)
array_delete(entity_updates, 0, max(0, base_update-1))
base_update = min(base_update, 1)

len = array_length(entity_updates)

Expand Down Expand Up @@ -78,6 +78,12 @@ for(var i = 0; i < l; i++) {

var uuid = entity.id
var type = asset_get_index(entity.obj)
if (type == -1) { // object with this type doesn't exist
if (ALLOW_UKNOWN_ENTITIES)
type = oEntity
else
throw "Error: Received unknown entity type: " + string(entity.obj) + ". Set ALLOW_UNKNOWN_ENTITIES to true to disable this error."
}
var props = entity.props
var existed = instance_exists(find_by_uuid(uuid, type))

Expand All @@ -91,7 +97,7 @@ for(var i = 0; i < l; i++) {
continue
}

var inst = find_or_create(uuid, type)
var inst = find_or_create(uuid, type, , props)

// if it was just created - it's remote
if (!existed) {
Expand All @@ -103,7 +109,7 @@ for(var i = 0; i < l; i++) {
if (uuid == global.player_uuid) {
inst.remote = false
}

// the reason I'm not using a with() statement here is because for some reason it is not equivallent to this, and produces weird errors (due to this being called in an Async event)
inst.image_xscale = entity.xscale
inst.image_yscale = entity.yscale
Expand Down
2 changes: 1 addition & 1 deletion Client/scripts/HandlePacket/handlepacket.gml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function handlePacket(data) {
var new_t = local_timestamp()
var ping = round(new_t - old_t)

global.start_server_time = data.t + round(ping/2)
global.start_server_time = data.t + ping
global.start_local_time = new_t

trace("server time: %; client time: %", global.start_server_time, global.start_local_time)
Expand Down
6 changes: 3 additions & 3 deletions Client/scripts/InstanceID/InstanceID.gml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function use_uuid() {
self.remote = false // whether an entity is remote or local - local by default
}

function find_by_uuid(uuid, object_type = all, remote = undefined) {
function find_by_uuid(uuid, object_type = oEntity, remote = undefined) {
with(object_type) {
if (variable_instance_exists(self, "uuid") and self.uuid == uuid
and (is_undefined(remote)
Expand All @@ -17,10 +17,10 @@ function find_by_uuid(uuid, object_type = all, remote = undefined) {
return noone
}

function find_or_create(uuid, object_type, remote = undefined) {
function find_or_create(uuid, object_type, remote = undefined, props = {}) {
var inst = find_by_uuid(uuid, object_type, remote)
if inst == noone {
inst = instance_create_layer(0, 0, "Instances", object_type)
inst = instance_create_layer(0, 0, "Instances", object_type, props)
inst.uuid = uuid
if (!is_undefined(remote)) {
inst.remote = remote
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ trace("Welcome to Warp % by Evoleo!", WARP_VERSION)

network_set_config(network_config_use_non_blocking_socket, true)

#macro ALLOW_UKNOWN_ENTITIES true

// purges all entities from the room (so that there aren't any local duplicates when loading everything from the server)
// toggle this off if you have some specific case where you need to keep the entities that are in the room locally
#macro PURGE_ENTITIES_ON_ROOM_START true

// a value between 0 and 1, bigger number = less smooth, but more accurate
#macro POS_INTERPOLATION 1
// instantly teleports if an entity has moved > than this on a single axis in a single tick
#macro POS_INTERP_THRESH 100

Expand All @@ -23,6 +23,8 @@ network_set_config(network_config_use_non_blocking_socket, true)
// offset in ms, a buffer between server and client time to allow for network latency & inconsistency
global.server_time_delay = 100
#macro SERVER_TIME_DELAY global.server_time_delay
// if set to true, global.server_time_delay will automatically increase when lag spikes occur
#macro AUTOADJUST_SERVER_DELAY true


#macro SOCKET_TYPE SOCKET_TYPES.WS
Expand All @@ -47,8 +49,6 @@ enum SOCKET_TYPES {
//#macro Prod:IP "195.2.80.50" // your external server IP
#macro Prod:PORT "1337"
#macro Prod:WS_PORT "3000"
//#macro Prod:PORT "1337"
//#macro Prod:WS_PORT "3000"
#macro Prod:DUAL_INSTANCE true
//#macro Prod:DUAL_INSTANCE false

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions Client/scripts/network_write/network_write.gml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
///@arg *socket
// most of the time you just want to send the data to oClient.socket
function network_write(data, sock = oClient.sock) {
if (!oClient.connected)
return -1

if (TIMESTAMPS_ENABLED) {
data.t = local_timestamp() // ms since the client started
}
Expand All @@ -19,19 +22,23 @@ function network_write(data, sock = oClient.sock) {
buffer_copy(buff, 0, size, new_buff, 4)

// send!
network_send_raw(sock, new_buff, size+4)
var status = network_send_raw(sock, new_buff, size+4)

// cleanup
buffer_delete(new_buff)
}
else {
// send without the size bits
network_send_raw(sock, buff, size, network_send_binary)
var status = network_send_raw(sock, buff, size, network_send_binary)
}


// Clean up
buffer_delete(buff)

if (status < 0) {
trace("Failed to send data: status code %", status)
oClient.disconnect()
}
}


Expand All @@ -40,11 +47,4 @@ function network_write(data, sock = oClient.sock) {
///@arg *socket
function send(data, sock = oClient.socket) {
return network_write(data, sock)
}

// P.s:
// You might need to change "_u16" and "2" everywhere to a higher power of 2
// if you're sending something more than 65535 bytes in size
// (that's because packet size is represented by a uint16)

// jk already changed it to _u32 because entities weight a ton
}
5 changes: 3 additions & 2 deletions TypescriptServer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
"development": "node . --env=dev",
"prod": "node . --env=prod",
"production": "node . --env=prod",
"profile": "node --prof --inspect . --env=dev",
"postprofile": "node --profile"
"profile": "node --prof --env=dev",
"postprofile": "node --profile",
"build": "npx tsc"
},
"dependencies": {
"@msgpack/msgpack": "^2.8.0",
Expand Down
Loading

0 comments on commit 8a36ac6

Please sign in to comment.