Add bandwidth testing code

This commit is contained in:
simon.kagstrom 2009-11-08 12:50:56 +00:00
parent 1033f94d24
commit a909b551b2
3 changed files with 104 additions and 19 deletions

View File

@ -529,7 +529,6 @@ void C64::network_vblank()
if (this->network_connection_type == CLIENT)
this->TheDisplay->Update(remote->GetScreen());
}
remote->ResetNetworkUpdate();
/* Encode and send updates to the other side (what is determined by
* if this is the master or not) */
@ -555,7 +554,6 @@ void C64::network_vblank()
/* Disconnect or broken data */
printf("Could not send update\n");
}
else
remote->ResetNetworkUpdate();
static uint32_t last_traffic_update;

View File

@ -667,6 +667,8 @@ bool Network::MarshalData(NetworkUpdate *p)
case TEXT_MESSAGE:
case STOP:
break;
case BANDWIDTH_PING:
case BANDWIDTH_ACK:
case PING:
case ACK:
{
@ -774,6 +776,8 @@ bool Network::DeMarshalData(NetworkUpdate *p)
case STOP:
/* Nothing to do, just bytes */
break;
case BANDWIDTH_PING:
case BANDWIDTH_ACK:
case PING:
case ACK:
{
@ -916,10 +920,20 @@ bool Network::DecodeUpdate(C64Display *display, uint8 *js, MOS6581 *dst)
case LIST_PEERS:
{
} break;
case BANDWIDTH_PING:
case PING:
/* FIXME! Send an ack */
{
NetworkUpdatePingAck *ping = (NetworkUpdatePingAck *)p->data;
uint16 type = ACK;
if (ud->type == BANDWIDTH_PING)
type = BANDWIDTH_ACK;
this->SendPingAck(ping->seq, type, ud->size);
} break;
case BANDWIDTH_ACK:
case ACK:
/* We won't receive this, but it also doesn't really matter */
break;
case ACK: /* Should never receive this */
case DISCONNECT:
out = false;
break;
@ -973,20 +987,13 @@ bool Network::IpToStr(char *dst, uint8 *ip_in)
return true;
}
/* OK, this is a pretty ugly special case, but it's only used when
* communicating with the broker before a peer connection. */
void Network::SendPingAck(int seq)
void Network::SendPingAck(int seq, uint16 type, size_t size_to_send)
{
this->ResetNetworkUpdate();
NetworkUpdate *ud = InitNetworkUpdate(this->ud, ACK,
sizeof(NetworkUpdate) + sizeof(NetworkUpdatePingAck));
NetworkUpdate *ud = InitNetworkUpdate(this->ud, type, size_to_send);
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
p->seq = seq;
this->AddNetworkUpdate(ud);
this->SendUpdate();
this->ResetNetworkUpdate();
}
network_connection_error_t Network::WaitForPeerAddress()
@ -1000,7 +1007,9 @@ network_connection_error_t Network::WaitForPeerAddress()
{
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
/* Send ack and go back to this state again */
this->SendPingAck(p->seq);
this->SendPingAck(p->seq, ACK, ud->size);
this->SendUpdate();
this->ResetNetworkUpdate();
return AGAIN_ERROR;
}
if (this->ud->type != LIST_PEERS)
@ -1063,7 +1072,9 @@ network_connection_error_t Network::WaitForPeerList()
{
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
/* Send ack and go back to this state again */
this->SendPingAck(p->seq);
this->SendPingAck(p->seq, ACK, ud->size);
this->SendUpdate();
this->ResetNetworkUpdate();
return AGAIN_ERROR;
}
if (ud->type != LIST_PEERS)
@ -1134,6 +1145,57 @@ bool Network::ConnectToPeer()
return out;
}
network_connection_error_t Network::WaitForBandWidthReply()
{
/* Wait until we've got an ack */
while (1) {
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 0;
this->ResetNetworkUpdate();
if (this->ReceiveUpdate(&tv) == false)
return AGAIN_ERROR;
if (this->ud->type == BANDWIDTH_PING) {
NetworkUpdatePingAck *ping = (NetworkUpdatePingAck *)this->ud->data;
uint32 seq = ping->seq;
size_t sz = this->ud->size;
this->ResetNetworkUpdate();
this->SendPingAck(seq, BANDWIDTH_ACK, sz);
this->SendUpdate();
continue;
}
/* CONNECT_TO_PEER is sent twice, so we might get it here */
if (this->ud->type == CONNECT_TO_PEER)
continue;
if (this->ud->type == BANDWIDTH_ACK)
break;
else /* Everything else is an error */
return SERVER_GARBAGE_ERROR;
}
/* We got a bandwidth ACK */
uint32 now = SDL_GetTicks();
int32 ms_diff = now - this->bandwidth_ping_ms;
size_t sz = this->ud->size;
if (ms_diff <= 0) {
/* Fast indeed, or maybe wrong */
this->target_kbps = 240000;
} else {
int bits_per_second = ((sz * 1000) / ms_diff) * 8;
this->target_kbps = bits_per_second;
}
if (this->target_kbps > 300000)
this->target_kbps = 300000;
return OK;
}
network_connection_error_t Network::ConnectFSM()
{
network_connection_error_t err;
@ -1194,10 +1256,26 @@ network_connection_error_t Network::ConnectFSM()
if (this->ConnectToPeer() == false)
return AGAIN_ERROR;
if (this->WaitForPeerReply() == true)
this->network_connection_state = CONN_CONNECTED;
this->network_connection_state = CONN_BANDWIDTH_PING;
else
return AGAIN_ERROR;
break;
case CONN_BANDWIDTH_PING:
TheC64->TheDisplay->display_status_string((char*)"TESTING BANDWIDTH", 1);
this->ResetNetworkUpdate();
this->SendPingAck(this->is_master, BANDWIDTH_PING, 2048);
this->bandwidth_ping_ms = SDL_GetTicks();
this->SendUpdate();
this->ResetNetworkUpdate();
this->network_connection_state = CONN_BANDWIDTH_REPLY;
break;
case CONN_BANDWIDTH_REPLY:
{
network_connection_error_t err = this->WaitForBandWidthReply();
if (err == OK)
this->network_connection_state = CONN_CONNECTED;
return err;
} break;
case CONN_CONNECTED:
TheC64->TheDisplay->display_status_string((char*)"CONNECTED!", 1);
/* The lowest number is the default master */

View File

@ -32,6 +32,8 @@ typedef enum
SELECT_PEER = 93, /* (client) Select who to connect to */
PING = 95, /* (broker) are you alive? */
ACK = 94, /* Answer to broker */
BANDWIDTH_PING = 92, /* Large packet to calculate bandwidth */
BANDWIDTH_ACK = 91, /* Answer to BANDWIDTH_PING */
/* Non-data messages */
STOP = 55, /* End of this update sequence */
/* Data transfer of various kinds */
@ -56,8 +58,9 @@ typedef enum
CONN_SELECT_PEER,
CONN_WAIT_FOR_PEER_REPLY,
/* Client-only */
CONN_WAIT_FOR_PEER_LIST,
CONN_BANDWIDTH_PING,
CONN_BANDWIDTH_REPLY,
FAILED,
} network_connection_state_t;
@ -100,6 +103,7 @@ struct NetworkUpdateSelectPeer
struct NetworkUpdatePingAck
{
uint32 seq;
uint8 data[]; /* Only used for bandwidth ping/acks */
};
struct NetworkUpdateSoundInfo
@ -283,7 +287,7 @@ protected:
bool DecodeDisplayRaw(struct NetworkUpdate *src,
int x, int y);
void SendPingAck(int seq);
void SendPingAck(int seq, uint16 type, size_t data_size);
bool ReceiveUpdate(NetworkUpdate *dst, size_t sz, struct timeval *tv);
@ -323,6 +327,10 @@ protected:
bool WaitForPeerReply();
bool SendBandWidthTest();
network_connection_error_t WaitForBandWidthReply();
network_connection_error_t WaitForPeerList();
network_connection_error_t WaitForPeerAddress();
@ -364,6 +372,7 @@ protected:
const char *connection_error_message;
network_connection_state_t network_connection_state;
uint32 bandwidth_ping_ms;
NetworkUpdateSoundInfo sound_active[NETWORK_SOUND_BUF_SIZE];
int sound_head;