Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

T1663R

macrumors newbie
Original poster
Apr 4, 2014
1
0
Hey,

im working on adding Network support to my game. on 127.0.0.1 it runs fine. If i try with a Friend over the internet we get really bad gaming Performance.

this code is sending the moving Blocks in our world to the Clients :
-(void)didSimulatePhysics {
// center camera on player
_camera.position = _playerSprite.position;
[self centerOnNode: [self childNodeWithName: @"//camera"]];
// send all non resting Body Positions over the TriggerPoint to all clients
float static triggerPoint = 0.000;
for (Block *block in _world.children) {
if ([block isKindOfClass:[Block class]] && !block.physicsBody.isResting && (block.physicsBody.velocity.dx > triggerPoint || block.physicsBody.velocity.dy > triggerPoint || block.physicsBody.velocity.dx < triggerPoint || block.physicsBody.velocity.dy < triggerPoint)) {
BlockPosition *pos = [[BlockPosition alloc]initWithUUID:block.uuid.UUIDString Position:block.position Rotation:block.zRotation];

// UDP
[_gameServer sendObjectToAllClients_UDP: pos];

}
}

this is the sendObjecttoall clients method :

-(void)sendObjectToAllClients_UDP: (id)obj{

// Encode Object to NSData
NSData *encodedPos = [NSKeyedArchiver archivedDataWithRootObject: obj];
// send Data to all connected Clients
for (AsyncConnection *con in self.server.connections) {
[self.broadcaster.broadcastSocket sendData:encodedPos toHost:con.host port:40001 withTimeout:-1.0 tag:0];
NSLog([NSString stringWithFormat:mad:"server sending UDP msg to: %@:%lu", con.host, con.port]);

}

But as mentioned we get really bad Performance. (with bad Performance i mean moving Boxes constanly warping for the Client, but only for few Pixels, increasing if the moving speed of the Boxes Increases)
And it also lags for the client, increasing if he moves faster.
Why is that ? And how can i fix that ?

thanks in advance. Greetings

EDIT
i forgot to mention i use the AsyncNetwork.framework
 
Last edited:
There's a lot of information missing here. How much data are you sending over the wire? How big is an archived BlockPosition? What is the latency between you and your client? How much overhead is there for your UDP packets? How many BlockPositions are normally sent by this? Could you bundle all BlockPositions together to reduce chatter? What processing is done on the other side? How much time does it take to archive this thing? Are you maintaining the connection or is it being closed/reopened frequently? Have you ever worked with this networking library before? Have you worked with network programming in general?

Start measuring and asking these questions. Find your bottleneck. Optimize it. Test again. Tweak. Keep going until it's acceptable. Don't assume anything is as it should be. Be willing to abandon all of your assumptions.

Post more of your code and we may be able to help more specifically.

-Lee

Edit: The "are you maintaining a connection" is likely irrelevant here. I deal with TCP much more frequently.
 
Last edited:
EDIT
i forgot to mention i use the AsyncNetwork.framework

Exactly which version? Post its URL.

There's this one:
https://github.com/jdiehl/async-network

It says there:
In client-server networking, clients connect to servers to exchange messages. In AsyncNetwork, every client will automatically connect to every discovered server with the same service name. Servers are discovered using Bonjour.
...
In peer-to-peer networking, every peer can exchange messages with every other peer on the network. To implement peer-to-peer networking on AsyncNetwork, you may either use the AsyncBroadcaster or have every peer instantiate an AsyncServer and a AsyncClient.

With the AsyncBroadcaster messages can be send to all peers on the same subnet. The broadcaster can only send binary data (NSData) and it is not guaranteed that the data arrives in order.
...
From the name of one of your properties (self.broadcaster.broadcastSocket), it looks like you're using broadcasting. Unfortunately, the above reference says "on the same subnet", which necessaril implies "not across the internet".

Also, it's unclear what routers or gateways you're going through. Some of them might not handle multicast (Bonjour) or broadcast properly.

Without knowing more details about exactly what's on the wire going over the internet, I don't see an easy way to diagnose the problem. The posted code is too abstracted from the low-level "over the netwrk" layer.

There's no data about what's on the wire on the local subnet (LAN), so no one knows what to expect if those packets are sent over the internet. Get a tool like wireshark or tcpdump, and look at the actual packets, so you understand what data is going where. Then work up from there.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.