2009-01-19 07:44:19 +01:00
|
|
|
/*
|
|
|
|
* Network.h - Network handling
|
|
|
|
*
|
|
|
|
* Frodo (C) 2009 Simon Kagstrom
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysdeps.h"
|
|
|
|
#include "Network.h"
|
|
|
|
#include "Display.h"
|
2009-03-01 09:27:53 +01:00
|
|
|
#include "Prefs.h"
|
2009-11-01 09:38:13 +01:00
|
|
|
#include "main.h"
|
|
|
|
#include "C64.h"
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2010-02-06 15:00:12 +01:00
|
|
|
#include "utils.hh"
|
|
|
|
#include "data_store.hh"
|
2010-02-12 07:35:24 +01:00
|
|
|
#include "gui/gui.hh"
|
2010-02-14 10:24:49 +01:00
|
|
|
#include "gui/status_bar.hh"
|
2010-02-12 07:35:24 +01:00
|
|
|
#include "gui/network_user_menu.hh"
|
2010-02-06 15:00:12 +01:00
|
|
|
|
2009-03-01 16:47:50 +01:00
|
|
|
#if defined(GEKKO)
|
|
|
|
# include <wiiuse/wpad.h>
|
|
|
|
#endif
|
|
|
|
|
2009-01-24 21:57:23 +01:00
|
|
|
#define N_SQUARES_W 16
|
|
|
|
#define N_SQUARES_H 8
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
#define SQUARE_W (DISPLAY_X / N_SQUARES_W)
|
|
|
|
#define SQUARE_H (DISPLAY_Y / N_SQUARES_H)
|
|
|
|
|
|
|
|
#define SQUARE_TO_X(square) ( ((square) % N_SQUARES_W) * SQUARE_W )
|
|
|
|
#define SQUARE_TO_Y(square) ( ((square) / N_SQUARES_W) * SQUARE_H )
|
|
|
|
|
2009-01-28 21:57:48 +01:00
|
|
|
/* Worst cases for RLE and DIFF */
|
|
|
|
#define RAW_SIZE ( (SQUARE_W * SQUARE_H) / 2 )
|
|
|
|
#define RLE_SIZE ( RAW_SIZE * 4 + 8)
|
|
|
|
#define DIFF_SIZE ( RAW_SIZE * 4 + 8)
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-10-28 19:04:30 +01:00
|
|
|
Network::Network(const char *remote_host, int port)
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
|
|
|
const size_t size = NETWORK_UPDATE_SIZE;
|
|
|
|
|
2009-03-08 20:02:19 +01:00
|
|
|
this->InitNetwork();
|
|
|
|
|
2009-11-04 17:43:47 +01:00
|
|
|
this->is_master = true; /* Assume true */
|
2009-02-08 12:27:56 +01:00
|
|
|
this->connected = false;
|
2009-02-01 20:47:21 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
/* "big enough" buffer */
|
2009-01-24 16:48:43 +01:00
|
|
|
this->ud = (NetworkUpdate*)malloc( size );
|
2009-04-10 13:39:46 +02:00
|
|
|
assert(this->ud);
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
this->ResetNetworkUpdate();
|
2009-01-30 18:49:47 +01:00
|
|
|
this->traffic = 0;
|
|
|
|
this->last_traffic = 0;
|
2009-04-13 12:57:01 +02:00
|
|
|
this->target_kbps = 160000; /* kilobit per seconds */
|
2009-01-30 18:49:47 +01:00
|
|
|
this->kbps = 0;
|
2009-01-25 11:07:51 +01:00
|
|
|
|
2009-01-28 21:57:48 +01:00
|
|
|
this->raw_buf = (Uint8*)malloc(RAW_SIZE);
|
|
|
|
this->rle_buf = (Uint8*)malloc(RLE_SIZE);
|
|
|
|
this->diff_buf = (Uint8*)malloc(DIFF_SIZE);
|
|
|
|
assert(this->raw_buf && this->rle_buf && this->diff_buf);
|
|
|
|
|
2009-03-28 13:37:06 +01:00
|
|
|
/* Go from lower right to upper left */
|
|
|
|
this->refresh_square = N_SQUARES_W * N_SQUARES_H - 1;
|
2009-01-25 11:07:51 +01:00
|
|
|
this->square_updated = (Uint32*)malloc( N_SQUARES_W * N_SQUARES_H * sizeof(Uint32));
|
|
|
|
assert(this->square_updated);
|
|
|
|
memset(this->square_updated, 0, N_SQUARES_W * N_SQUARES_H * sizeof(Uint32));
|
2009-02-01 20:47:21 +01:00
|
|
|
|
|
|
|
this->screen = (Uint8 *)malloc(DISPLAY_X * DISPLAY_Y);
|
|
|
|
assert(this->screen);
|
|
|
|
|
2009-11-04 18:41:54 +01:00
|
|
|
this->sound_head = this->sound_tail = 0;
|
2009-11-04 21:09:04 +01:00
|
|
|
this->sound_last_cycles = SDL_GetTicks();
|
2009-11-04 18:41:54 +01:00
|
|
|
memset(this->sound_active, 0, sizeof(this->sound_active));
|
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
/* Assume black screen */
|
|
|
|
memset(this->screen, 0, DISPLAY_X * DISPLAY_Y);
|
2009-11-01 09:38:13 +01:00
|
|
|
memset(this->screenshot, 0, sizeof(this->screenshot));
|
2009-02-07 12:08:50 +01:00
|
|
|
|
2009-04-26 08:02:57 +02:00
|
|
|
Network::networking_started = true;
|
2010-02-14 09:05:08 +01:00
|
|
|
this->peer_selected = -1;
|
2009-02-08 12:27:56 +01:00
|
|
|
/* Peer addresses, if it fails we are out of luck */
|
2010-02-14 09:57:55 +01:00
|
|
|
panic_if (this->InitSocket() == false,
|
|
|
|
"Could not init the socket\n");
|
|
|
|
|
|
|
|
/* Setup the socket addresses */
|
|
|
|
memset(&this->peer_addr, 0, sizeof(this->peer_addr));
|
|
|
|
memset(&this->server_addr, 0, sizeof(this->server_addr));
|
|
|
|
panic_if (this->InitSockaddr(&this->server_addr, remote_host, port) == false,
|
|
|
|
"Can't initialize socket address to server\n");
|
|
|
|
|
2009-02-16 20:20:00 +01:00
|
|
|
this->network_connection_state = CONN_CONNECT_TO_BROKER;
|
2009-04-04 09:40:17 +02:00
|
|
|
this->connection_error_message = "Connection OK";
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Network::~Network()
|
|
|
|
{
|
|
|
|
free(this->ud);
|
2009-01-28 21:57:48 +01:00
|
|
|
free(this->square_updated);
|
|
|
|
free(this->raw_buf);
|
|
|
|
free(this->rle_buf);
|
|
|
|
free(this->diff_buf);
|
2009-02-01 20:47:21 +01:00
|
|
|
free(this->screen);
|
|
|
|
|
|
|
|
this->CloseSocket();
|
2009-03-28 10:34:31 +01:00
|
|
|
this->ShutdownNetwork();
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
2009-01-30 18:49:47 +01:00
|
|
|
void Network::Tick(int ms)
|
|
|
|
{
|
|
|
|
int last_kbps = ((this->traffic - this->last_traffic) * 8) * (1000 / ms);
|
|
|
|
|
|
|
|
/* 1/3 of the new value, 2/3 of the old */
|
|
|
|
this->kbps = 2 * (this->kbps / 3) + (last_kbps / 3);
|
|
|
|
this->last_traffic = this->traffic;
|
|
|
|
}
|
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
bool Network::DecodeDisplayDiff(struct NetworkUpdate *src,
|
2009-01-25 11:07:51 +01:00
|
|
|
int x_start, int y_start)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
|
2009-01-25 11:07:51 +01:00
|
|
|
int p = 0;
|
|
|
|
int x = x_start;
|
|
|
|
int y = y_start;
|
2009-02-03 19:10:09 +01:00
|
|
|
int sz = src->size - sizeof(NetworkUpdate) - sizeof(NetworkUpdateDisplay);
|
2009-01-25 11:07:51 +01:00
|
|
|
|
|
|
|
/* Something is wrong if this is true... */
|
|
|
|
if (sz % 2 != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (p < sz)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
Uint8 len = dp->data[p];
|
|
|
|
Uint8 color = dp->data[p+1];
|
2009-01-25 11:07:51 +01:00
|
|
|
int x_diff = (x - x_start + len) % SQUARE_W;
|
|
|
|
int y_diff = (x - x_start + len) / SQUARE_W;
|
|
|
|
|
|
|
|
x = x_start + x_diff;
|
|
|
|
y = y + y_diff;
|
2009-04-13 08:31:12 +02:00
|
|
|
this->screen[y * DISPLAY_X + x] = color;
|
2009-01-25 11:07:51 +01:00
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
bool Network::DecodeDisplayRLE(struct NetworkUpdate *src,
|
2009-01-19 07:44:19 +01:00
|
|
|
int x_start, int y_start)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
|
2009-01-19 07:44:19 +01:00
|
|
|
int p = 0;
|
|
|
|
int x = x_start;
|
|
|
|
int y = y_start;
|
2009-02-03 19:10:09 +01:00
|
|
|
int sz = src->size - sizeof(NetworkUpdate) - sizeof(NetworkUpdateDisplay);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
/* Something is wrong if this is true... */
|
2009-01-24 21:57:23 +01:00
|
|
|
if (sz % 2 != 0)
|
2009-01-19 07:44:19 +01:00
|
|
|
return false;
|
|
|
|
|
2009-01-24 21:57:23 +01:00
|
|
|
while (p < sz)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
Uint8 len = dp->data[p];
|
|
|
|
Uint8 color = dp->data[p+1];
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
2009-04-13 08:31:12 +02:00
|
|
|
this->screen[y * DISPLAY_X + x] = color;
|
2009-01-19 07:44:19 +01:00
|
|
|
len--;
|
|
|
|
x++;
|
|
|
|
if ((x - x_start) % SQUARE_W == 0)
|
|
|
|
{
|
|
|
|
x = x_start;
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
bool Network::DecodeDisplayRaw(struct NetworkUpdate *src,
|
2009-01-19 07:44:19 +01:00
|
|
|
int x_start, int y_start)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
|
2009-01-19 07:44:19 +01:00
|
|
|
const int raw_w = SQUARE_W / 2;
|
|
|
|
|
2009-01-24 21:57:23 +01:00
|
|
|
for (int y = y_start; y < y_start + SQUARE_H; y++)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-24 21:57:23 +01:00
|
|
|
for (int x = x_start; x < x_start + SQUARE_W; x += 2)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
Uint8 v = dp->data[(y - y_start) * raw_w + (x - x_start) / 2];
|
2009-01-20 18:42:21 +01:00
|
|
|
Uint8 a = v >> 4;
|
|
|
|
Uint8 b = v & 0xf;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
this->screen[ y * DISPLAY_X + x ] = a;
|
|
|
|
this->screen[ y * DISPLAY_X + x + 1 ] = b;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
bool Network::CompareSquare(Uint8 *a, Uint8 *b)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < SQUARE_H; y++)
|
|
|
|
{
|
2009-01-25 11:07:51 +01:00
|
|
|
for (int x = 0; x < SQUARE_W; x += 4)
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
2009-01-25 11:07:51 +01:00
|
|
|
Uint32 va = *((Uint32*)&a[ y * DISPLAY_X + x ]);
|
|
|
|
Uint32 vb = *((Uint32*)&b[ y * DISPLAY_X + x ]);
|
2009-01-24 16:48:43 +01:00
|
|
|
|
2009-01-24 21:57:23 +01:00
|
|
|
if (va != vb)
|
2009-01-24 16:48:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-01 09:38:13 +01:00
|
|
|
void Network::EncodeScreenshot(Uint8 *dst, Uint8 *master)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
int cnt = 0;
|
|
|
|
int p = 0;
|
|
|
|
|
|
|
|
memset(dst, 0, (SCREENSHOT_X * SCREENSHOT_Y) / 2);
|
|
|
|
for (y = 0; y < DISPLAY_Y; y += SCREENSHOT_FACTOR)
|
|
|
|
{
|
|
|
|
for (x = 0; x < DISPLAY_X; x += SCREENSHOT_FACTOR)
|
|
|
|
{
|
|
|
|
Uint8 col_s = master[ y * DISPLAY_X + x ];
|
|
|
|
bool is_odd = (cnt & 1) == 1;
|
|
|
|
int raw_shift = (is_odd ? 0 : 4);
|
|
|
|
|
|
|
|
/* Every second is shifted */
|
|
|
|
dst[ p ] |= (col_s << raw_shift);
|
|
|
|
if (is_odd)
|
|
|
|
p++;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
void Network::EncodeDisplay(Uint8 *master, Uint8 *remote)
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
2009-11-04 17:43:47 +01:00
|
|
|
if (!this->network_connection_state == MASTER)
|
2009-02-01 20:47:21 +01:00
|
|
|
return;
|
2009-01-24 16:48:43 +01:00
|
|
|
for ( int sq = 0; sq < N_SQUARES_H * N_SQUARES_W; sq++ )
|
|
|
|
{
|
|
|
|
Uint8 *p_master = &master[ SQUARE_TO_Y(sq) * DISPLAY_X + SQUARE_TO_X(sq) ];
|
2009-01-26 22:00:23 +01:00
|
|
|
Uint8 *p_remote = &remote[ SQUARE_TO_Y(sq) * DISPLAY_X + SQUARE_TO_X(sq) ];
|
2009-01-24 16:48:43 +01:00
|
|
|
|
2009-03-28 13:37:06 +01:00
|
|
|
/* Refresh periodically or if the squares differ */
|
|
|
|
if ( (this->refresh_square == sq && this->kbps < this->target_kbps * 0.7) ||
|
|
|
|
this->CompareSquare(p_master, p_remote) == false)
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
NetworkUpdate *dst = (NetworkUpdate *)this->cur_ud;
|
2009-01-24 16:48:43 +01:00
|
|
|
|
|
|
|
/* Updated, encode this */
|
2009-03-28 13:37:06 +01:00
|
|
|
this->EncodeDisplaySquare(dst, master, remote, sq,
|
2009-03-28 14:03:50 +01:00
|
|
|
this->refresh_square != sq);
|
2009-01-26 22:00:23 +01:00
|
|
|
this->AddNetworkUpdate(dst);
|
2009-03-28 13:37:06 +01:00
|
|
|
|
|
|
|
/* This has been refreshed, move to the next one */
|
|
|
|
if (this->refresh_square == sq)
|
|
|
|
{
|
|
|
|
this->refresh_square--;
|
|
|
|
if (this->refresh_square < 0)
|
|
|
|
this->refresh_square = N_SQUARES_H * N_SQUARES_W - 1;
|
|
|
|
}
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
2009-01-25 11:07:51 +01:00
|
|
|
else
|
|
|
|
this->square_updated[sq] = 0;
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
|
|
|
memcpy(remote, master, DISPLAY_X * DISPLAY_Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-28 21:57:48 +01:00
|
|
|
size_t Network::EncodeDisplaySquare(struct NetworkUpdate *dst,
|
2009-03-28 13:37:06 +01:00
|
|
|
Uint8 *screen, Uint8 *remote, int square,
|
|
|
|
bool use_diff)
|
2009-01-28 21:57:48 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)dst->data;
|
2009-01-28 21:57:48 +01:00
|
|
|
const int x_start = SQUARE_TO_X(square);
|
|
|
|
const int y_start = SQUARE_TO_Y(square);
|
|
|
|
Uint8 rle_color = screen[ y_start * DISPLAY_X + x_start ];
|
|
|
|
int rle_len = 0, diff_len = 0;
|
|
|
|
size_t rle_sz = 0, diff_sz = 0;
|
|
|
|
const int raw_w = SQUARE_W / 2;
|
|
|
|
int type = DISPLAY_UPDATE_RAW;
|
|
|
|
size_t out;
|
|
|
|
|
|
|
|
for (int y = y_start; y < y_start + SQUARE_H; y++)
|
|
|
|
{
|
|
|
|
memset( &this->raw_buf[(y - y_start) * raw_w], 0, raw_w );
|
|
|
|
|
|
|
|
for (int x = x_start; x < x_start + SQUARE_W; x++)
|
|
|
|
{
|
|
|
|
Uint8 col_s = screen[ y * DISPLAY_X + x ];
|
|
|
|
Uint8 col_r = remote[ y * DISPLAY_X + x ];
|
|
|
|
bool is_odd = (x & 1) == 1;
|
|
|
|
int raw_shift = (is_odd ? 0 : 4);
|
|
|
|
|
|
|
|
/* Every second is shifted */
|
|
|
|
this->raw_buf[ (y - y_start) * raw_w + (x - x_start) / 2 ] |=
|
|
|
|
(col_s << raw_shift);
|
|
|
|
|
|
|
|
if (rle_color != col_s ||
|
|
|
|
rle_len >= 255)
|
|
|
|
{
|
|
|
|
this->rle_buf[rle_sz] = rle_len;
|
|
|
|
this->rle_buf[rle_sz + 1] = rle_color;
|
|
|
|
rle_sz += 2;
|
|
|
|
|
|
|
|
rle_len = 0;
|
|
|
|
rle_color = col_s;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (col_r != col_s || diff_len >= 255)
|
|
|
|
{
|
|
|
|
this->diff_buf[diff_sz] = diff_len;
|
|
|
|
this->diff_buf[diff_sz + 1] = col_s;
|
|
|
|
diff_sz += 2;
|
|
|
|
diff_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff_len++;
|
|
|
|
rle_len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The last section for RLE */
|
|
|
|
if (rle_len != 0)
|
|
|
|
{
|
|
|
|
this->rle_buf[rle_sz] = rle_len;
|
|
|
|
this->rle_buf[rle_sz + 1] = rle_color;
|
|
|
|
|
|
|
|
rle_sz += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = RAW_SIZE;
|
2009-03-28 13:37:06 +01:00
|
|
|
if (use_diff && (diff_sz < rle_sz && diff_sz < RAW_SIZE))
|
2009-01-28 21:57:48 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
memcpy(dp->data, this->diff_buf, diff_sz);
|
2009-01-28 21:57:48 +01:00
|
|
|
type = DISPLAY_UPDATE_DIFF;
|
|
|
|
out = diff_sz;
|
|
|
|
}
|
|
|
|
else if (rle_sz < RAW_SIZE)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
memcpy(dp->data, this->rle_buf, rle_sz);
|
2009-01-28 21:57:48 +01:00
|
|
|
type = DISPLAY_UPDATE_RLE;
|
|
|
|
out = rle_sz;
|
|
|
|
}
|
|
|
|
else
|
2009-02-03 19:10:09 +01:00
|
|
|
memcpy(dp->data, this->raw_buf, RAW_SIZE);
|
2009-01-28 21:57:48 +01:00
|
|
|
|
|
|
|
/* Setup the structure */
|
2009-02-03 19:10:09 +01:00
|
|
|
dp->square = square;
|
|
|
|
dst = InitNetworkUpdate(dst, type,
|
|
|
|
sizeof(struct NetworkUpdate) + sizeof(struct NetworkUpdateDisplay) + out);
|
2009-01-28 21:57:48 +01:00
|
|
|
this->square_updated[square] = out | (type << 16);
|
|
|
|
|
|
|
|
return dst->size;
|
|
|
|
}
|
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
bool Network::DecodeDisplayUpdate(struct NetworkUpdate *src)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateDisplay *dp = (struct NetworkUpdateDisplay *)src->data;
|
|
|
|
int square = dp->square;
|
2009-01-19 07:44:19 +01:00
|
|
|
const int square_x = SQUARE_TO_X(square);
|
|
|
|
const int square_y = SQUARE_TO_Y(square);
|
|
|
|
|
2009-01-25 11:07:51 +01:00
|
|
|
if (src->type == DISPLAY_UPDATE_DIFF)
|
2009-04-13 08:31:12 +02:00
|
|
|
return this->DecodeDisplayDiff(src, square_x, square_y);
|
2009-01-25 11:07:51 +01:00
|
|
|
else if (src->type == DISPLAY_UPDATE_RAW)
|
2009-04-13 08:31:12 +02:00
|
|
|
return this->DecodeDisplayRaw(src, square_x, square_y);
|
2009-01-25 11:07:51 +01:00
|
|
|
else if (src->type == DISPLAY_UPDATE_RLE)
|
2009-04-13 08:31:12 +02:00
|
|
|
return this->DecodeDisplayRLE(src, square_x, square_y);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
|
|
|
/* Error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
void Network::EncodeTextMessage(char *str)
|
|
|
|
{
|
|
|
|
NetworkUpdate *dst = (NetworkUpdate *)this->cur_ud;
|
|
|
|
char *p = (char*)dst->data;
|
|
|
|
size_t len = strlen(str) + 1;
|
|
|
|
|
|
|
|
len += (len & 3);
|
|
|
|
dst = InitNetworkUpdate(dst, TEXT_MESSAGE,
|
|
|
|
sizeof(NetworkUpdate) + len);
|
|
|
|
memset(p, 0, len);
|
|
|
|
strncpy(p, str, len - 1);
|
|
|
|
|
|
|
|
this->AddNetworkUpdate(dst);
|
|
|
|
}
|
|
|
|
|
2009-02-02 20:51:58 +01:00
|
|
|
|
2009-11-06 18:16:35 +01:00
|
|
|
void Network::EnqueueSound(uint32 linecnt_diff, uint8 adr, uint8 val)
|
2009-11-04 18:41:54 +01:00
|
|
|
{
|
|
|
|
NetworkUpdateSoundInfo *cur = &this->sound_active[this->sound_head];
|
|
|
|
|
|
|
|
cur->adr = adr;
|
|
|
|
cur->val = val;
|
2009-11-06 18:16:35 +01:00
|
|
|
cur->delay_cycles = linecnt_diff;
|
2009-11-04 18:41:54 +01:00
|
|
|
|
|
|
|
this->sound_head++;
|
|
|
|
|
|
|
|
if (this->sound_head >= NETWORK_SOUND_BUF_SIZE)
|
|
|
|
this->sound_head = 0;
|
2009-11-06 18:16:35 +01:00
|
|
|
|
|
|
|
/* Head has reached tail */
|
2009-11-07 09:32:51 +01:00
|
|
|
if (this->sound_head == this->sound_tail)
|
2009-11-04 18:41:54 +01:00
|
|
|
this->sound_tail = (this->sound_head + 1) % NETWORK_SOUND_BUF_SIZE;
|
2009-11-06 18:16:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::RegisterSidWrite(uint32 linecnt, uint8 adr, uint8 val)
|
|
|
|
{
|
|
|
|
this->EnqueueSound(linecnt - this->sound_last_cycles, adr, val);
|
|
|
|
|
|
|
|
/* Update the cycle counter */
|
|
|
|
sound_last_cycles = linecnt;
|
2009-11-04 18:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Network::FlushSound(void)
|
|
|
|
{
|
2009-11-06 18:16:35 +01:00
|
|
|
NetworkUpdate *dst = this->cur_ud;
|
|
|
|
NetworkUpdateSound *snd = (NetworkUpdateSound *)dst->data;
|
|
|
|
NetworkUpdateSoundInfo *snd_info = snd->info;
|
2009-11-04 18:41:54 +01:00
|
|
|
|
2009-11-04 21:09:04 +01:00
|
|
|
snd->flags = 0;
|
|
|
|
snd->n_items = this->sound_head - this->sound_tail;
|
|
|
|
|
|
|
|
if (this->sound_head < this->sound_tail) {
|
2009-11-07 10:36:01 +01:00
|
|
|
snd->n_items = NETWORK_SOUND_BUF_SIZE - this->sound_tail + this->sound_head;
|
2009-11-04 21:09:04 +01:00
|
|
|
memcpy(snd_info, &this->sound_active[this->sound_tail],
|
|
|
|
(NETWORK_SOUND_BUF_SIZE - this->sound_tail) * sizeof(struct NetworkUpdateSoundInfo));
|
|
|
|
memcpy(snd_info + NETWORK_SOUND_BUF_SIZE - this->sound_tail,
|
2009-11-07 10:36:01 +01:00
|
|
|
&this->sound_active[0],
|
2009-11-04 21:09:04 +01:00
|
|
|
this->sound_head * sizeof(struct NetworkUpdateSoundInfo));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-11-07 10:36:01 +01:00
|
|
|
memcpy(snd_info, &this->sound_active[this->sound_tail],
|
2009-11-04 21:09:04 +01:00
|
|
|
(this->sound_head - this->sound_tail) * sizeof(struct NetworkUpdateSoundInfo));
|
|
|
|
}
|
2009-11-07 09:39:07 +01:00
|
|
|
this->sound_tail = this->sound_head;
|
2009-11-04 21:09:04 +01:00
|
|
|
|
|
|
|
this->sound_last_send = SDL_GetTicks();
|
|
|
|
|
2009-11-07 09:32:51 +01:00
|
|
|
InitNetworkUpdate(dst, SOUND_UPDATE, sizeof(NetworkUpdate) +
|
2009-11-04 21:09:04 +01:00
|
|
|
sizeof(NetworkUpdateSound) + sizeof(NetworkUpdateSoundInfo) * snd->n_items);
|
|
|
|
this->AddNetworkUpdate(dst);
|
2009-11-07 11:12:49 +01:00
|
|
|
this->sound_last_cycles = TheC64->linecnt;
|
2009-11-04 18:41:54 +01:00
|
|
|
}
|
|
|
|
|
2009-11-06 18:16:35 +01:00
|
|
|
struct NetworkUpdateSoundInfo *Network::DequeueSound()
|
2009-11-05 07:24:36 +01:00
|
|
|
{
|
|
|
|
struct NetworkUpdateSoundInfo *out;
|
|
|
|
|
|
|
|
if (this->sound_tail == this->sound_head)
|
|
|
|
return NULL;
|
|
|
|
out = &this->sound_active[this->sound_tail];
|
|
|
|
this->sound_tail = (this->sound_tail + 1) % NETWORK_SOUND_BUF_SIZE;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2009-11-04 18:41:54 +01:00
|
|
|
|
2009-01-29 22:11:04 +01:00
|
|
|
void Network::EncodeJoystickUpdate(Uint8 v)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-29 22:11:04 +01:00
|
|
|
struct NetworkUpdate *dst = this->cur_ud;
|
2009-02-03 19:10:09 +01:00
|
|
|
struct NetworkUpdateJoystick *j = (NetworkUpdateJoystick *)dst->data;
|
2009-01-29 22:11:04 +01:00
|
|
|
|
2009-11-04 18:43:05 +01:00
|
|
|
if (TheC64->network_connection_type == MASTER || this->cur_joystick_data == v)
|
2009-02-01 20:47:21 +01:00
|
|
|
return;
|
2009-02-03 19:10:09 +01:00
|
|
|
dst = InitNetworkUpdate(dst, JOYSTICK_UPDATE,
|
|
|
|
sizeof(NetworkUpdate) + sizeof(NetworkUpdateJoystick));
|
|
|
|
j->val = v;
|
2009-01-29 22:11:04 +01:00
|
|
|
|
|
|
|
this->AddNetworkUpdate(dst);
|
2009-02-01 20:47:21 +01:00
|
|
|
this->cur_joystick_data = v;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-24 16:48:43 +01:00
|
|
|
void Network::ResetNetworkUpdate(void)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-24 16:48:43 +01:00
|
|
|
memset(this->ud, 0, NETWORK_UPDATE_SIZE);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-03 19:10:09 +01:00
|
|
|
this->cur_ud = InitNetworkUpdate(this->ud, STOP, sizeof(NetworkUpdate));
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-25 11:07:51 +01:00
|
|
|
void Network::DrawTransferredBlocks(SDL_Surface *screen)
|
|
|
|
{
|
|
|
|
const int x_border = (DISPLAY_X - FULL_DISPLAY_X / 2);
|
|
|
|
const int y_border = (DISPLAY_Y - FULL_DISPLAY_Y / 2);
|
|
|
|
|
|
|
|
for (int sq = 0; sq < N_SQUARES_W * N_SQUARES_H; sq++)
|
|
|
|
{
|
|
|
|
int x = SQUARE_TO_X(sq) * 2 - x_border;
|
|
|
|
int y = SQUARE_TO_Y(sq) * 2 - y_border;
|
|
|
|
int w = SQUARE_W * 2;
|
|
|
|
int h = SQUARE_H * 2;
|
|
|
|
|
|
|
|
if (this->square_updated[sq])
|
|
|
|
{
|
|
|
|
SDL_Rect l = {x, y, 1, h};
|
|
|
|
SDL_Rect r = {x + w, y, 1, h};
|
|
|
|
SDL_Rect u = {x, y, w, 1};
|
|
|
|
SDL_Rect d = {x, y + h, w, 1};
|
|
|
|
Uint32 raw = this->square_updated[sq];
|
2009-01-26 22:00:23 +01:00
|
|
|
SDL_Rect size = {x, y, 2 * ((raw & 0xffff) / 17), 4};
|
2009-01-25 11:07:51 +01:00
|
|
|
Uint32 color = 4;
|
|
|
|
|
2009-01-28 21:57:48 +01:00
|
|
|
if ((raw >> 16) == DISPLAY_UPDATE_RLE)
|
2009-01-25 11:07:51 +01:00
|
|
|
color = 5;
|
2009-01-28 21:57:48 +01:00
|
|
|
else if ((raw >> 16) == DISPLAY_UPDATE_DIFF)
|
2009-01-25 11:07:51 +01:00
|
|
|
color = 6;
|
|
|
|
|
|
|
|
SDL_FillRect(screen, &l, 19);
|
|
|
|
SDL_FillRect(screen, &r, 19);
|
|
|
|
SDL_FillRect(screen, &u, 19);
|
|
|
|
SDL_FillRect(screen, &d, 19);
|
|
|
|
|
|
|
|
SDL_FillRect(screen, &size, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
bool Network::ReceiveUpdate()
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
2009-02-08 12:27:56 +01:00
|
|
|
return this->ReceiveUpdate(this->ud, NETWORK_UPDATE_SIZE, &tv);
|
|
|
|
}
|
2009-02-02 19:36:51 +01:00
|
|
|
|
2009-02-08 12:52:09 +01:00
|
|
|
bool Network::ReceiveUpdate(struct timeval *tv)
|
2009-02-08 12:27:56 +01:00
|
|
|
{
|
2009-02-08 12:52:09 +01:00
|
|
|
return this->ReceiveUpdate(this->ud, NETWORK_UPDATE_SIZE, tv);
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-02-07 12:08:50 +01:00
|
|
|
bool Network::ReceiveUpdate(NetworkUpdate *dst, size_t total_sz,
|
|
|
|
struct timeval *tv)
|
2009-01-24 21:57:23 +01:00
|
|
|
{
|
2009-03-16 21:56:04 +01:00
|
|
|
Uint8 *p = (Uint8*)dst;
|
2009-02-07 12:08:50 +01:00
|
|
|
size_t sz_left = total_sz;
|
2009-11-07 14:52:22 +01:00
|
|
|
size_t received = 0;
|
2009-03-16 21:56:04 +01:00
|
|
|
bool has_stop = false;
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
if (this->Select(this->sock, tv) == false)
|
2009-01-26 22:00:23 +01:00
|
|
|
return false;
|
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
if (sz_left <= 0)
|
|
|
|
return false;
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-02-07 19:24:50 +01:00
|
|
|
/* Receive the header */
|
2009-03-16 21:56:04 +01:00
|
|
|
do {
|
2009-11-07 14:52:22 +01:00
|
|
|
ssize_t actual_sz = this->ReceiveFrom(p, this->sock,
|
2009-03-16 21:56:04 +01:00
|
|
|
4096, NULL);
|
2009-03-28 16:18:50 +01:00
|
|
|
if (actual_sz <= 0)
|
2009-03-16 21:56:04 +01:00
|
|
|
return false;
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-11-07 14:52:22 +01:00
|
|
|
received += actual_sz;
|
2009-11-08 10:34:00 +01:00
|
|
|
if (ntohs(dst->magic) != FRODO_NETWORK_MAGIC) {
|
|
|
|
printf("Packet with wrong magic received\n");
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-07 14:52:22 +01:00
|
|
|
if (this->ScanDataForStop(dst, received) == true)
|
|
|
|
break;
|
|
|
|
|
2009-03-16 21:56:04 +01:00
|
|
|
sz_left -= actual_sz;
|
|
|
|
p = p + actual_sz;
|
|
|
|
} while (!has_stop);
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-11-07 14:52:22 +01:00
|
|
|
if (this->DeMarshalAllData(dst, received) == false) {
|
|
|
|
printf("Demarshal error\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-14 09:57:55 +01:00
|
|
|
bool Network::SendUpdate(struct sockaddr_in *addr)
|
2009-01-26 22:00:23 +01:00
|
|
|
{
|
|
|
|
NetworkUpdate *src = this->ud;
|
2009-02-03 19:10:09 +01:00
|
|
|
NetworkUpdate *stop = InitNetworkUpdate(this->cur_ud, STOP, sizeof(NetworkUpdate));
|
2009-01-26 22:00:23 +01:00
|
|
|
size_t sz;
|
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
/* Nothing to send, that's OK */
|
|
|
|
if ( src == stop )
|
|
|
|
return true;
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
/* Add a stop at the end of the update */
|
|
|
|
this->AddNetworkUpdate(stop);
|
|
|
|
|
|
|
|
if (this->MarshalAllData(src) == false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
sz = this->GetNetworkUpdateSize();
|
|
|
|
if (sz <= 0)
|
|
|
|
return false;
|
2009-03-16 21:56:04 +01:00
|
|
|
size_t cur_sz = 0;
|
|
|
|
Uint8 *p = (Uint8*)src;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t size_to_send = this->FillNetworkBuffer((NetworkUpdate*)p);
|
|
|
|
ssize_t v;
|
|
|
|
|
|
|
|
v = this->SendTo((void*)p, this->sock,
|
2010-02-14 09:57:55 +01:00
|
|
|
size_to_send, addr);
|
2010-02-06 15:00:12 +01:00
|
|
|
if (v < 0 || (size_t)v != size_to_send)
|
2009-03-16 21:56:04 +01:00
|
|
|
return false;
|
|
|
|
cur_sz += size_to_send;
|
|
|
|
p += size_to_send;
|
|
|
|
} while (cur_sz < sz);
|
|
|
|
this->traffic += cur_sz;
|
2009-01-26 22:00:23 +01:00
|
|
|
|
|
|
|
return true;
|
2009-01-24 21:57:23 +01:00
|
|
|
}
|
|
|
|
|
2009-03-16 21:56:04 +01:00
|
|
|
size_t Network::FillNetworkBuffer(NetworkUpdate *cur)
|
|
|
|
{
|
|
|
|
size_t sz = 0;
|
|
|
|
size_t cur_sz;
|
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
cur_sz = ntohl(cur->size);
|
|
|
|
|
|
|
|
if (sz + cur_sz >= 4096)
|
|
|
|
break;
|
|
|
|
|
|
|
|
cnt++;
|
|
|
|
sz += cur_sz;
|
|
|
|
if (ntohs(cur->type) == STOP)
|
|
|
|
break;
|
|
|
|
cur = (NetworkUpdate*)((Uint8*)cur + cur_sz);
|
|
|
|
}
|
|
|
|
assert(sz <= 4096);
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
void Network::AddNetworkUpdate(NetworkUpdate *update)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
Uint8 *next = (Uint8*)this->cur_ud + update->size;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
this->cur_ud = (NetworkUpdate*)next;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
bool Network::MarshalData(NetworkUpdate *p)
|
|
|
|
{
|
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case DISPLAY_UPDATE_RAW:
|
|
|
|
case DISPLAY_UPDATE_RLE:
|
|
|
|
case DISPLAY_UPDATE_DIFF:
|
|
|
|
case JOYSTICK_UPDATE:
|
2009-01-29 19:04:31 +01:00
|
|
|
case DISCONNECT:
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONNECT_TO_PEER:
|
2009-04-13 08:31:12 +02:00
|
|
|
case TEXT_MESSAGE:
|
2009-01-26 22:00:23 +01:00
|
|
|
case STOP:
|
|
|
|
break;
|
2009-11-08 13:50:56 +01:00
|
|
|
case BANDWIDTH_PING:
|
|
|
|
case BANDWIDTH_ACK:
|
2009-03-28 16:18:50 +01:00
|
|
|
case PING:
|
|
|
|
case ACK:
|
|
|
|
{
|
|
|
|
NetworkUpdatePingAck *pa = (NetworkUpdatePingAck *)p->data;
|
|
|
|
pa->seq = htonl(pa->seq);
|
|
|
|
} break;
|
2009-02-28 19:45:26 +01:00
|
|
|
case SELECT_PEER:
|
|
|
|
{
|
|
|
|
NetworkUpdateSelectPeer *sp = (NetworkUpdateSelectPeer *)p->data;
|
2009-02-28 20:42:11 +01:00
|
|
|
sp->server_id = htonl(sp->server_id);
|
2009-02-28 19:45:26 +01:00
|
|
|
} break;
|
2010-02-07 10:37:21 +01:00
|
|
|
case REGISTER_DATA:
|
|
|
|
{
|
|
|
|
NetworkUpdateRegisterData *ds = (NetworkUpdateRegisterData *)p->data;
|
|
|
|
|
|
|
|
ds->key = htonl(ds->key);
|
|
|
|
ds->metadata = htonl(ds->metadata);
|
|
|
|
} break;
|
2009-02-07 12:08:50 +01:00
|
|
|
case LIST_PEERS:
|
|
|
|
{
|
|
|
|
NetworkUpdateListPeers *lp = (NetworkUpdateListPeers *)p->data;
|
2009-02-21 08:59:45 +01:00
|
|
|
for (unsigned int i = 0; i < lp->n_peers; i++)
|
2009-02-07 12:08:50 +01:00
|
|
|
{
|
|
|
|
NetworkUpdatePeerInfo *peer = &lp->peers[i];
|
|
|
|
|
|
|
|
peer->key = htons(peer->key);
|
|
|
|
peer->private_port = htons(peer->private_port);
|
|
|
|
peer->public_port = htons(peer->public_port);
|
|
|
|
peer->is_master = htons(peer->is_master);
|
2009-03-28 14:03:50 +01:00
|
|
|
peer->server_id = htonl(peer->server_id);
|
2009-04-04 09:40:17 +02:00
|
|
|
peer->version = htonl(peer->version);
|
2009-11-01 09:38:13 +01:00
|
|
|
peer->avatar = htonl(peer->avatar);
|
2010-02-07 10:37:21 +01:00
|
|
|
peer->screenshot_key = htonl(peer->screenshot_key);
|
2009-02-07 12:08:50 +01:00
|
|
|
}
|
|
|
|
lp->n_peers = htonl(lp->n_peers);
|
|
|
|
lp->your_port = htons(lp->your_port);
|
|
|
|
} break;
|
2009-02-28 19:45:26 +01:00
|
|
|
case CONNECT_TO_BROKER:
|
|
|
|
{
|
|
|
|
NetworkUpdatePeerInfo *pi = (NetworkUpdatePeerInfo *)p->data;
|
|
|
|
|
|
|
|
/* The rest is simply ignored */
|
|
|
|
pi->is_master = htons(pi->is_master);
|
|
|
|
pi->key = htons(pi->key);
|
2009-04-04 09:40:17 +02:00
|
|
|
pi->version = htonl(pi->version);
|
2009-02-28 19:45:26 +01:00
|
|
|
} break;
|
2009-11-06 07:31:06 +01:00
|
|
|
case SOUND_UPDATE:
|
|
|
|
{
|
|
|
|
NetworkUpdateSound *snd = (NetworkUpdateSound *)p->data;
|
|
|
|
NetworkUpdateSoundInfo *info = (NetworkUpdateSoundInfo *)snd->info;
|
2009-11-07 10:36:01 +01:00
|
|
|
int items = snd->n_items;
|
2009-11-06 07:31:06 +01:00
|
|
|
|
|
|
|
snd->flags = htons(snd->flags);
|
2009-11-07 09:52:46 +01:00
|
|
|
snd->n_items = htons(snd->n_items);
|
2009-11-07 10:36:01 +01:00
|
|
|
|
2010-02-06 15:00:12 +01:00
|
|
|
for (int i = 0; i < items; i++)
|
2009-11-06 07:31:06 +01:00
|
|
|
{
|
|
|
|
NetworkUpdateSoundInfo *cur = &info[i];
|
|
|
|
|
|
|
|
cur->delay_cycles = htons(cur->delay_cycles);
|
|
|
|
}
|
2009-11-06 18:16:35 +01:00
|
|
|
} break;
|
2009-01-26 22:00:23 +01:00
|
|
|
default:
|
|
|
|
/* Unknown data... */
|
|
|
|
fprintf(stderr, "Got unknown data %d while marshalling. Something is wrong\n",
|
|
|
|
p->type);
|
2009-02-28 19:45:26 +01:00
|
|
|
exit(0); // FIXME! TMP!!
|
2009-01-26 22:00:23 +01:00
|
|
|
return false;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
2009-01-26 22:00:23 +01:00
|
|
|
|
2009-02-03 19:10:09 +01:00
|
|
|
p->size = htonl(p->size);
|
|
|
|
p->magic = htons(p->magic);
|
|
|
|
p->type = htons(p->type);
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
return true;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
bool Network::MarshalAllData(NetworkUpdate *ud)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
NetworkUpdate *p = ud;
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
while (p->type != STOP)
|
2009-01-19 07:44:19 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
NetworkUpdate *nxt = this->GetNext(p);
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
if (this->MarshalData(p) == false)
|
|
|
|
return false;
|
|
|
|
p = nxt;
|
|
|
|
}
|
2009-01-19 07:44:19 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
/* The stop tag */
|
|
|
|
return this->MarshalData(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Network::DeMarshalData(NetworkUpdate *p)
|
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
p->size = ntohl(p->size);
|
|
|
|
p->magic = ntohs(p->magic);
|
|
|
|
p->type = ntohs(p->type);
|
|
|
|
|
|
|
|
if (p->magic != FRODO_NETWORK_MAGIC)
|
|
|
|
return false;
|
2009-01-26 22:00:23 +01:00
|
|
|
|
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case DISPLAY_UPDATE_RAW:
|
|
|
|
case DISPLAY_UPDATE_RLE:
|
|
|
|
case DISPLAY_UPDATE_DIFF:
|
|
|
|
case JOYSTICK_UPDATE:
|
2009-01-29 19:04:31 +01:00
|
|
|
case DISCONNECT:
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONNECT_TO_PEER:
|
2009-04-13 08:31:12 +02:00
|
|
|
case TEXT_MESSAGE:
|
2009-01-26 22:00:23 +01:00
|
|
|
case STOP:
|
|
|
|
/* Nothing to do, just bytes */
|
|
|
|
break;
|
2009-11-08 13:50:56 +01:00
|
|
|
case BANDWIDTH_PING:
|
|
|
|
case BANDWIDTH_ACK:
|
2009-03-28 16:18:50 +01:00
|
|
|
case PING:
|
|
|
|
case ACK:
|
|
|
|
{
|
|
|
|
NetworkUpdatePingAck *pa = (NetworkUpdatePingAck *)p->data;
|
|
|
|
pa->seq = ntohl(pa->seq);
|
|
|
|
} break;
|
2009-03-28 12:32:59 +01:00
|
|
|
case SELECT_PEER:
|
|
|
|
{
|
|
|
|
NetworkUpdateSelectPeer *sp = (NetworkUpdateSelectPeer *)p->data;
|
|
|
|
sp->server_id = ntohl(sp->server_id);
|
|
|
|
} break;
|
2010-02-07 10:37:21 +01:00
|
|
|
case REGISTER_DATA:
|
|
|
|
{
|
|
|
|
NetworkUpdateRegisterData *ds = (NetworkUpdateRegisterData *)p->data;
|
|
|
|
|
|
|
|
ds->key = ntohl(ds->key);
|
|
|
|
ds->metadata = ntohl(ds->metadata);
|
|
|
|
} break;
|
2009-02-07 12:08:50 +01:00
|
|
|
case LIST_PEERS:
|
|
|
|
{
|
|
|
|
NetworkUpdateListPeers *lp = (NetworkUpdateListPeers *)p->data;
|
|
|
|
|
|
|
|
lp->n_peers = ntohl(lp->n_peers);
|
2009-02-21 08:59:45 +01:00
|
|
|
for (unsigned int i = 0; i < lp->n_peers; i++)
|
2009-02-07 12:08:50 +01:00
|
|
|
{
|
|
|
|
NetworkUpdatePeerInfo *peer = &lp->peers[i];
|
|
|
|
|
|
|
|
peer->key = ntohs(peer->key);
|
|
|
|
peer->private_port = ntohs(peer->private_port);
|
|
|
|
peer->public_port = ntohs(peer->public_port);
|
|
|
|
peer->is_master = ntohs(peer->is_master);
|
2009-03-28 13:55:11 +01:00
|
|
|
peer->server_id = ntohl(peer->server_id);
|
2009-04-04 09:40:17 +02:00
|
|
|
peer->version = ntohl(peer->version);
|
2009-11-01 09:38:13 +01:00
|
|
|
peer->avatar = ntohl(peer->avatar);
|
2010-02-07 10:37:21 +01:00
|
|
|
peer->screenshot_key = ntohl(peer->screenshot_key);
|
2009-02-07 12:08:50 +01:00
|
|
|
}
|
|
|
|
lp->your_port = ntohs(lp->your_port);
|
|
|
|
} break;
|
2009-11-06 07:31:06 +01:00
|
|
|
case SOUND_UPDATE:
|
|
|
|
{
|
|
|
|
NetworkUpdateSound *snd = (NetworkUpdateSound *)p->data;
|
|
|
|
NetworkUpdateSoundInfo *info = (NetworkUpdateSoundInfo *)snd->info;
|
|
|
|
|
|
|
|
snd->flags = ntohs(snd->flags);
|
2009-11-07 09:52:46 +01:00
|
|
|
snd->n_items = ntohs(snd->n_items);
|
2009-11-06 07:31:06 +01:00
|
|
|
for (unsigned int i = 0; i < snd->n_items; i++)
|
|
|
|
{
|
|
|
|
NetworkUpdateSoundInfo *cur = &info[i];
|
|
|
|
|
|
|
|
cur->delay_cycles = ntohs(cur->delay_cycles);
|
|
|
|
}
|
2009-11-07 09:32:51 +01:00
|
|
|
} break;
|
2009-01-26 22:00:23 +01:00
|
|
|
default:
|
|
|
|
/* Unknown data... */
|
2009-02-28 19:45:26 +01:00
|
|
|
printf("Got unknown data: %d\n", p->type);
|
2009-01-26 22:00:23 +01:00
|
|
|
return false;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
2009-01-26 22:00:23 +01:00
|
|
|
|
|
|
|
return true;
|
2009-01-19 07:44:19 +01:00
|
|
|
}
|
|
|
|
|
2009-11-07 14:52:22 +01:00
|
|
|
bool Network::DeMarshalAllData(NetworkUpdate *ud, size_t max_size)
|
2009-02-07 19:24:50 +01:00
|
|
|
{
|
|
|
|
NetworkUpdate *p = ud;
|
2009-03-16 21:56:04 +01:00
|
|
|
int cnt = 0;
|
|
|
|
size_t sz = 0;
|
2009-02-07 19:24:50 +01:00
|
|
|
|
2009-03-16 21:56:04 +01:00
|
|
|
while (ntohs(p->type) != STOP &&
|
|
|
|
sz + ntohl(p->size) < max_size)
|
2009-02-07 19:24:50 +01:00
|
|
|
{
|
|
|
|
if (this->DeMarshalData(p) == false)
|
|
|
|
return false;
|
2009-03-16 21:56:04 +01:00
|
|
|
sz += p->size;
|
|
|
|
cnt++;
|
2009-02-07 19:24:50 +01:00
|
|
|
p = this->GetNext(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this->DeMarshalData(p);
|
|
|
|
}
|
|
|
|
|
2009-11-07 14:52:22 +01:00
|
|
|
bool Network::ScanDataForStop(NetworkUpdate *ud, size_t max_size)
|
|
|
|
{
|
|
|
|
NetworkUpdate *p = ud;
|
|
|
|
size_t sz = 0;
|
|
|
|
|
|
|
|
while (ntohs(p->type) != STOP &&
|
|
|
|
sz + ntohl(p->size) < max_size)
|
|
|
|
{
|
|
|
|
size_t cur_sz = ntohl(p->size);
|
|
|
|
|
|
|
|
sz += cur_sz;
|
|
|
|
p = (NetworkUpdate*)((Uint8*)p + cur_sz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The stop tag (maybe) */
|
|
|
|
return ntohs(p->type) == STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-13 08:31:12 +02:00
|
|
|
bool Network::DecodeUpdate(C64Display *display, uint8 *js, MOS6581 *dst)
|
2009-01-24 21:57:23 +01:00
|
|
|
{
|
2009-01-26 22:00:23 +01:00
|
|
|
NetworkUpdate *p = this->ud;
|
2009-01-29 19:04:31 +01:00
|
|
|
bool out = true;
|
2009-01-24 21:57:23 +01:00
|
|
|
|
2009-01-26 22:00:23 +01:00
|
|
|
while (p->type != STOP)
|
2009-01-24 21:57:23 +01:00
|
|
|
{
|
|
|
|
switch(p->type)
|
|
|
|
{
|
2009-11-04 18:12:20 +01:00
|
|
|
case SOUND_UPDATE:
|
2009-11-06 20:57:02 +01:00
|
|
|
{
|
2009-04-10 13:39:46 +02:00
|
|
|
/* No sound updates _to_ the master */
|
2009-11-04 18:43:05 +01:00
|
|
|
if (TheC64->network_connection_type == MASTER)
|
2009-04-10 13:39:46 +02:00
|
|
|
break;
|
2009-11-06 20:57:02 +01:00
|
|
|
NetworkUpdateSound *snd = (NetworkUpdateSound *)p->data;
|
|
|
|
NetworkUpdateSoundInfo *info = (NetworkUpdateSoundInfo *)snd->info;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < snd->n_items; i++)
|
|
|
|
{
|
|
|
|
NetworkUpdateSoundInfo *cur = &info[i];
|
|
|
|
|
|
|
|
this->EnqueueSound(cur->delay_cycles, cur->adr, cur->val);
|
|
|
|
}
|
|
|
|
} break;
|
2009-01-24 21:57:23 +01:00
|
|
|
case DISPLAY_UPDATE_RAW:
|
|
|
|
case DISPLAY_UPDATE_RLE:
|
2009-01-25 11:07:51 +01:00
|
|
|
case DISPLAY_UPDATE_DIFF:
|
2009-02-01 20:47:21 +01:00
|
|
|
/* No screen updates _to_ the master */
|
2009-11-04 18:43:05 +01:00
|
|
|
if (TheC64->network_connection_type == MASTER)
|
2009-01-29 21:48:58 +01:00
|
|
|
break;
|
2009-04-13 08:31:12 +02:00
|
|
|
if (this->DecodeDisplayUpdate(p) == false)
|
2009-01-29 19:04:31 +01:00
|
|
|
out = false;
|
|
|
|
break;
|
2009-01-29 22:11:04 +01:00
|
|
|
case JOYSTICK_UPDATE:
|
2009-02-01 20:47:21 +01:00
|
|
|
/* No joystick updates _from_ the master */
|
2009-11-04 18:43:05 +01:00
|
|
|
if (js && TheC64->network_connection_type == MASTER)
|
2009-02-03 19:10:09 +01:00
|
|
|
{
|
|
|
|
NetworkUpdateJoystick *j = (NetworkUpdateJoystick *)p->data;
|
|
|
|
*js = j->val;
|
|
|
|
}
|
2009-01-29 22:11:04 +01:00
|
|
|
break;
|
2009-04-13 09:49:19 +02:00
|
|
|
case TEXT_MESSAGE:
|
2009-04-13 12:10:08 +02:00
|
|
|
{
|
|
|
|
static char display_buf[80];
|
|
|
|
|
|
|
|
strncpy(display_buf, (char*)p->data, 80);
|
|
|
|
display->display_status_string(display_buf, 4);
|
|
|
|
} break;
|
2009-02-07 12:08:50 +01:00
|
|
|
case LIST_PEERS:
|
|
|
|
{
|
|
|
|
} break;
|
2010-02-07 10:37:21 +01:00
|
|
|
case REGISTER_DATA:
|
|
|
|
{
|
|
|
|
NetworkUpdateRegisterData *rd = (NetworkUpdateRegisterData *)p->data;
|
|
|
|
|
|
|
|
DataStore::ds->registerNetworkData(rd->key, rd->metadata, rd->data,
|
|
|
|
p->size - (sizeof(NetworkUpdateRegisterData) + sizeof(NetworkUpdate)));
|
|
|
|
} break;
|
2009-11-08 13:50:56 +01:00
|
|
|
case BANDWIDTH_PING:
|
2009-02-07 12:08:50 +01:00
|
|
|
case PING:
|
2009-11-08 13:50:56 +01:00
|
|
|
{
|
|
|
|
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 */
|
2009-02-07 12:08:50 +01:00
|
|
|
break;
|
2009-01-29 19:04:31 +01:00
|
|
|
case DISCONNECT:
|
|
|
|
out = false;
|
2009-01-24 21:57:23 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-01-26 22:00:23 +01:00
|
|
|
p = this->GetNext(p);
|
2009-01-24 21:57:23 +01:00
|
|
|
}
|
2009-01-29 19:04:31 +01:00
|
|
|
|
|
|
|
return out;
|
2009-01-24 21:57:23 +01:00
|
|
|
}
|
|
|
|
|
2010-02-06 15:00:12 +01:00
|
|
|
bool Network::AppendScreenshot(NetworkUpdatePeerInfo *pi)
|
|
|
|
{
|
2010-02-07 10:22:37 +01:00
|
|
|
NetworkUpdateRegisterData *dsu;
|
2010-02-06 15:00:12 +01:00
|
|
|
NetworkUpdate *ud;
|
|
|
|
SDL_Surface *scr;
|
|
|
|
void *png;
|
|
|
|
size_t sz;
|
|
|
|
bool out = NULL;
|
|
|
|
|
|
|
|
scr = TheC64->TheDisplay->SurfaceFromC64Display();
|
|
|
|
if (!scr)
|
|
|
|
goto out_none;
|
|
|
|
|
|
|
|
png = sdl_surface_to_png(scr, &sz);
|
|
|
|
if (!png)
|
|
|
|
goto out_scr;
|
|
|
|
|
2010-02-14 11:09:46 +01:00
|
|
|
ud = InitNetworkUpdate(this->cur_ud, REGISTER_DATA,
|
2010-02-07 10:22:37 +01:00
|
|
|
sizeof(NetworkUpdate) + sizeof(NetworkUpdateRegisterData) + sz);
|
|
|
|
dsu = (NetworkUpdateRegisterData *)ud->data;
|
2010-02-07 10:52:36 +01:00
|
|
|
dsu->key = DataStore::ds->getNextKey();
|
|
|
|
dsu->metadata = 0;
|
|
|
|
memcpy(dsu->data, png, sz);
|
2010-02-06 15:00:12 +01:00
|
|
|
this->AddNetworkUpdate(ud);
|
|
|
|
|
|
|
|
out = true;
|
|
|
|
free(png);
|
|
|
|
out_scr:
|
|
|
|
SDL_FreeSurface(scr);
|
|
|
|
out_none:
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-02-13 08:57:40 +01:00
|
|
|
bool Network::ConnectToBroker()
|
|
|
|
{
|
2010-02-14 11:09:46 +01:00
|
|
|
NetworkUpdate *ud = InitNetworkUpdate(this->cur_ud, CONNECT_TO_BROKER,
|
2010-02-06 15:00:12 +01:00
|
|
|
sizeof(NetworkUpdate) + sizeof(NetworkUpdatePeerInfo));
|
2009-02-13 08:57:40 +01:00
|
|
|
NetworkUpdatePeerInfo *pi = (NetworkUpdatePeerInfo *)ud->data;
|
|
|
|
bool out;
|
|
|
|
|
2010-02-12 07:35:24 +01:00
|
|
|
/* Reset peer selection */
|
2010-02-14 09:05:08 +01:00
|
|
|
this->peer_selected = -1;
|
2010-02-12 07:35:24 +01:00
|
|
|
|
2009-11-04 17:29:58 +01:00
|
|
|
pi->is_master = 0; /* Will be set later */
|
2009-03-01 16:47:50 +01:00
|
|
|
pi->key = ThePrefs.NetworkKey;
|
2009-04-04 09:40:17 +02:00
|
|
|
pi->version = FRODO_NETWORK_PROTOCOL_VERSION;
|
2009-11-01 09:38:13 +01:00
|
|
|
pi->avatar = ThePrefs.NetworkAvatar;
|
2010-02-06 15:00:12 +01:00
|
|
|
pi->screenshot_key = 0;
|
2009-11-01 09:38:13 +01:00
|
|
|
|
2009-03-01 09:27:53 +01:00
|
|
|
strcpy((char*)pi->name, ThePrefs.NetworkName);
|
2009-02-13 08:57:40 +01:00
|
|
|
this->AddNetworkUpdate(ud);
|
2010-02-06 15:00:12 +01:00
|
|
|
out = this->AppendScreenshot(pi);
|
|
|
|
if (out)
|
2010-02-14 09:57:55 +01:00
|
|
|
out = this->SendServerUpdate();
|
2009-02-13 08:57:40 +01:00
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:50:56 +01:00
|
|
|
void Network::SendPingAck(int seq, uint16 type, size_t size_to_send)
|
2009-03-28 16:18:50 +01:00
|
|
|
{
|
2009-11-08 13:50:56 +01:00
|
|
|
NetworkUpdate *ud = InitNetworkUpdate(this->ud, type, size_to_send);
|
2009-03-28 16:18:50 +01:00
|
|
|
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
|
|
|
|
|
|
|
|
p->seq = seq;
|
|
|
|
this->AddNetworkUpdate(ud);
|
|
|
|
}
|
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
network_connection_error_t Network::WaitForPeerAddress()
|
2009-02-13 08:57:40 +01:00
|
|
|
{
|
|
|
|
NetworkUpdateListPeers *pi;
|
|
|
|
|
|
|
|
this->ResetNetworkUpdate();
|
2009-04-04 10:23:51 +02:00
|
|
|
if (this->ReceiveUpdate() == false)
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-03-28 16:18:50 +01:00
|
|
|
if (this->ud->type == PING)
|
|
|
|
{
|
|
|
|
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
|
|
|
|
/* Send ack and go back to this state again */
|
2009-11-08 13:50:56 +01:00
|
|
|
this->SendPingAck(p->seq, ACK, ud->size);
|
2010-02-14 09:57:55 +01:00
|
|
|
this->SendServerUpdate();
|
2009-11-08 13:50:56 +01:00
|
|
|
this->ResetNetworkUpdate();
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-03-28 16:18:50 +01:00
|
|
|
}
|
|
|
|
if (this->ud->type != LIST_PEERS)
|
2009-04-04 09:40:17 +02:00
|
|
|
return SERVER_GARBAGE_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
|
|
|
|
pi = (NetworkUpdateListPeers *)this->ud->data;
|
|
|
|
if (pi->n_peers != 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "There is something wrong with the server: Got %d peers on master connect\n"
|
|
|
|
"Contact Simon Kagstrom and ask him to correct it\n",
|
|
|
|
pi->n_peers);
|
2009-04-04 09:40:17 +02:00
|
|
|
return SERVER_GARBAGE_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
}
|
2009-04-04 09:40:17 +02:00
|
|
|
if (pi->peers[0].version != FRODO_NETWORK_PROTOCOL_VERSION)
|
|
|
|
return VERSION_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
return OK;
|
2009-02-13 08:57:40 +01:00
|
|
|
}
|
|
|
|
|
2009-02-28 19:45:26 +01:00
|
|
|
bool Network::SelectPeer(uint32 id)
|
|
|
|
{
|
|
|
|
NetworkUpdate *ud = InitNetworkUpdate(this->ud, SELECT_PEER,
|
|
|
|
sizeof(NetworkUpdate) + sizeof(NetworkUpdateSelectPeer));
|
|
|
|
NetworkUpdateSelectPeer *p = (NetworkUpdateSelectPeer*)ud->data;
|
|
|
|
bool out;
|
|
|
|
|
|
|
|
p->server_id = id;
|
|
|
|
this->AddNetworkUpdate(ud);
|
2010-02-14 09:57:55 +01:00
|
|
|
out = this->SendServerUpdate();
|
2009-02-28 19:45:26 +01:00
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
network_connection_error_t Network::WaitForPeerList()
|
2009-01-24 16:48:43 +01:00
|
|
|
{
|
2009-02-13 08:57:40 +01:00
|
|
|
NetworkUpdateListPeers *pi;
|
2009-02-08 12:52:09 +01:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = 1;
|
|
|
|
tv.tv_usec = 0;
|
2009-02-11 21:07:28 +01:00
|
|
|
|
2009-02-13 08:57:40 +01:00
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
if (this->ReceiveUpdate(&tv) == false)
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-03-28 16:18:50 +01:00
|
|
|
if (this->ud->type == PING)
|
|
|
|
{
|
|
|
|
NetworkUpdatePingAck *p = (NetworkUpdatePingAck*)ud->data;
|
|
|
|
/* Send ack and go back to this state again */
|
2009-11-08 13:50:56 +01:00
|
|
|
this->SendPingAck(p->seq, ACK, ud->size);
|
2010-02-14 09:57:55 +01:00
|
|
|
this->SendServerUpdate();
|
2009-11-08 13:50:56 +01:00
|
|
|
this->ResetNetworkUpdate();
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-03-28 16:18:50 +01:00
|
|
|
}
|
2009-02-16 20:20:00 +01:00
|
|
|
if (ud->type != LIST_PEERS)
|
2009-04-04 09:40:17 +02:00
|
|
|
return SERVER_GARBAGE_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
|
|
|
|
pi = (NetworkUpdateListPeers *)this->ud->data;
|
2010-02-12 07:35:24 +01:00
|
|
|
for (unsigned i = 0; i < pi->n_peers; i++)
|
|
|
|
{
|
2009-04-04 09:40:17 +02:00
|
|
|
if (pi->peers[i].version != FRODO_NETWORK_PROTOCOL_VERSION)
|
|
|
|
return VERSION_ERROR;
|
2009-10-28 19:04:30 +01:00
|
|
|
}
|
2010-02-14 11:09:46 +01:00
|
|
|
if (pi->n_peers == 0)
|
|
|
|
return NO_PEERS_ERROR;
|
2010-02-12 07:35:24 +01:00
|
|
|
|
|
|
|
Gui::gui->nuv->setPeers(pi);
|
|
|
|
Gui::gui->activate();
|
|
|
|
Gui::gui->pushView(Gui::gui->nuv);
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Network::SelectPeer(const char *hostname, uint16_t port, uint32_t server_id)
|
|
|
|
{
|
2010-02-14 09:05:08 +01:00
|
|
|
if (!hostname)
|
|
|
|
{
|
|
|
|
this->peer_selected = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-12 07:35:24 +01:00
|
|
|
this->SelectPeer(server_id);
|
2010-02-14 09:28:40 +01:00
|
|
|
this->InitSockaddr(&this->peer_addr, hostname, port);
|
2010-02-14 09:05:08 +01:00
|
|
|
this->peer_selected = 1;
|
2010-02-12 07:35:24 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
network_connection_error_t Network::WaitForPeerSelection()
|
|
|
|
{
|
2010-02-14 09:05:08 +01:00
|
|
|
if (this->peer_selected == 1)
|
2010-02-12 07:35:24 +01:00
|
|
|
return AGAIN_ERROR;
|
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
return OK;
|
2009-02-13 08:57:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Network::WaitForPeerReply()
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = 3;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
if (this->ReceiveUpdate(&tv) == false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (this->ud->type != CONNECT_TO_PEER)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Network::ConnectToPeer()
|
|
|
|
{
|
|
|
|
NetworkUpdate *ud = InitNetworkUpdate(this->ud, CONNECT_TO_PEER,
|
2009-02-16 20:20:00 +01:00
|
|
|
sizeof(NetworkUpdate));
|
|
|
|
bool out;
|
2009-02-13 08:57:40 +01:00
|
|
|
|
|
|
|
this->AddNetworkUpdate(ud);
|
2010-02-14 09:57:55 +01:00
|
|
|
out = this->SendServerUpdate();
|
2009-02-13 08:57:40 +01:00
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:50:56 +01:00
|
|
|
network_connection_error_t Network::WaitForBandWidthReply()
|
|
|
|
{
|
2009-11-10 06:43:43 +01:00
|
|
|
unsigned cnt;
|
|
|
|
|
2009-11-08 13:50:56 +01:00
|
|
|
/* Wait until we've got an ack */
|
2009-11-10 06:43:43 +01:00
|
|
|
for (cnt = 0; cnt < 5; cnt++) {
|
2009-11-08 13:50:56 +01:00
|
|
|
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);
|
2010-02-14 09:57:55 +01:00
|
|
|
this->SendServerUpdate();
|
2009-11-08 13:50:56 +01:00
|
|
|
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;
|
2009-11-10 06:43:43 +01:00
|
|
|
cnt++;
|
2009-11-08 13:50:56 +01:00
|
|
|
}
|
2009-11-10 06:43:43 +01:00
|
|
|
if (cnt == 5) {
|
|
|
|
printf("Timeout. Setting default kbps (160)\n");
|
|
|
|
this->target_kbps = 160000;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:50:56 +01:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2009-11-08 13:52:54 +01:00
|
|
|
/* But force it to be within these limits */
|
2009-11-08 13:50:56 +01:00
|
|
|
if (this->target_kbps > 300000)
|
|
|
|
this->target_kbps = 300000;
|
2009-11-10 06:44:10 +01:00
|
|
|
if (this->target_kbps < 150000)
|
|
|
|
this->target_kbps = 150000;
|
2009-11-08 13:50:56 +01:00
|
|
|
|
2009-11-08 16:21:53 +01:00
|
|
|
printf("%d bytes in %d ms. Setting cap at %d\n", sz, ms_diff, this->target_kbps);
|
|
|
|
|
2009-11-08 13:50:56 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
network_connection_error_t Network::ConnectFSM()
|
2009-02-13 08:57:40 +01:00
|
|
|
{
|
2009-04-04 09:40:17 +02:00
|
|
|
network_connection_error_t err;
|
|
|
|
|
2009-02-11 21:07:28 +01:00
|
|
|
/* See http://www.brynosaurus.com/pub/net/p2pnat/ for how this works.
|
2009-02-13 08:57:40 +01:00
|
|
|
*
|
|
|
|
* For the client:
|
|
|
|
* 1. Send connect to the broker
|
|
|
|
* 2. Wait for the broker to return list of peers
|
|
|
|
* 3. Tell the broker who to connect to
|
2009-11-08 10:16:10 +01:00
|
|
|
* 4. Select peer
|
|
|
|
* 4.1 (master) Wait for broker to return peer address
|
|
|
|
* 4.2 (client) Connect to peer
|
2009-02-13 08:57:40 +01:00
|
|
|
* 5. Until connected:
|
|
|
|
* 5.1 Send connection message to peer
|
|
|
|
* 5.2 Wait for reply from peer
|
2009-11-08 16:59:18 +01:00
|
|
|
* 6. Test bandwidth
|
2009-02-11 21:07:28 +01:00
|
|
|
*/
|
2009-02-13 08:57:40 +01:00
|
|
|
switch(this->network_connection_state)
|
|
|
|
{
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_CONNECT_TO_BROKER:
|
2009-10-28 19:04:30 +01:00
|
|
|
{
|
2010-02-14 10:24:49 +01:00
|
|
|
Gui::gui->status_bar->queueMessage("Connecting to broker...");
|
2009-10-28 19:04:30 +01:00
|
|
|
if (this->ConnectToBroker())
|
|
|
|
this->network_connection_state = CONN_WAIT_FOR_PEER_LIST;
|
|
|
|
} break;
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_WAIT_FOR_PEER_ADDRESS:
|
2010-02-14 10:24:49 +01:00
|
|
|
Gui::gui->status_bar->queueMessage("Waiting for connection...");
|
2009-04-04 09:40:17 +02:00
|
|
|
err = this->WaitForPeerAddress();
|
|
|
|
if (err == OK)
|
|
|
|
this->network_connection_state = CONN_CONNECT_TO_PEER;
|
|
|
|
else
|
|
|
|
return err;
|
2009-02-13 08:57:40 +01:00
|
|
|
break;
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_WAIT_FOR_PEER_LIST:
|
2010-02-14 10:24:49 +01:00
|
|
|
Gui::gui->status_bar->queueMessage("Waiting for peer list...");
|
2009-02-28 19:45:26 +01:00
|
|
|
/* Also tells the broker that we want to connect */
|
2010-02-14 11:09:46 +01:00
|
|
|
err = this->WaitForPeerList();
|
|
|
|
if (err == NO_PEERS_ERROR)
|
|
|
|
{
|
|
|
|
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
|
|
|
|
this->is_master = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this->network_connection_state = CONN_WAIT_FOR_PEER_SELECT;
|
2010-02-12 07:35:24 +01:00
|
|
|
break;
|
|
|
|
case CONN_WAIT_FOR_PEER_SELECT:
|
|
|
|
err = this->WaitForPeerSelection();
|
2009-11-04 17:43:47 +01:00
|
|
|
if (err == OK) {
|
2009-04-04 09:40:17 +02:00
|
|
|
this->network_connection_state = CONN_CONNECT_TO_PEER;
|
2009-11-04 17:43:47 +01:00
|
|
|
this->is_master = false;
|
|
|
|
}
|
|
|
|
else if (err == NO_PEERS_ERROR) {
|
2009-10-28 19:04:30 +01:00
|
|
|
this->network_connection_state = CONN_WAIT_FOR_PEER_ADDRESS;
|
2009-11-04 17:43:47 +01:00
|
|
|
this->is_master = true;
|
|
|
|
}
|
2009-04-04 09:40:17 +02:00
|
|
|
else
|
|
|
|
return err;
|
2009-02-13 08:57:40 +01:00
|
|
|
break;
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_CONNECT_TO_PEER:
|
2010-02-14 10:24:49 +01:00
|
|
|
Gui::gui->status_bar->queueMessage("Connecting to peer...");
|
2009-02-13 08:57:40 +01:00
|
|
|
if (this->ConnectToPeer() == false)
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
/* Allow some transit time */
|
|
|
|
sleep(1);
|
2009-11-08 16:59:18 +01:00
|
|
|
if (this->ConnectToPeer() == false)
|
|
|
|
return AGAIN_ERROR;
|
2009-02-16 20:20:00 +01:00
|
|
|
this->network_connection_state = CONN_WAIT_FOR_PEER_REPLY;
|
2009-02-13 08:57:40 +01:00
|
|
|
break;
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_WAIT_FOR_PEER_REPLY:
|
2009-02-13 08:57:40 +01:00
|
|
|
/* Connect again in case the first sent was dropped on
|
|
|
|
* its way to the peer */
|
2009-04-04 09:40:17 +02:00
|
|
|
if (this->WaitForPeerReply() == true)
|
2009-11-08 13:50:56 +01:00
|
|
|
this->network_connection_state = CONN_BANDWIDTH_PING;
|
2009-04-04 09:40:17 +02:00
|
|
|
else
|
|
|
|
return AGAIN_ERROR;
|
2009-02-13 08:57:40 +01:00
|
|
|
break;
|
2009-11-08 13:50:56 +01:00
|
|
|
case CONN_BANDWIDTH_PING:
|
|
|
|
this->ResetNetworkUpdate();
|
2009-11-08 16:21:53 +01:00
|
|
|
this->SendPingAck(this->is_master, BANDWIDTH_PING, 1024);
|
2010-02-14 09:57:55 +01:00
|
|
|
this->SendServerUpdate();
|
2009-11-08 13:52:54 +01:00
|
|
|
this->bandwidth_ping_ms = SDL_GetTicks();
|
2009-11-08 13:50:56 +01:00
|
|
|
this->ResetNetworkUpdate();
|
|
|
|
this->network_connection_state = CONN_BANDWIDTH_REPLY;
|
|
|
|
break;
|
|
|
|
case CONN_BANDWIDTH_REPLY:
|
|
|
|
{
|
|
|
|
network_connection_error_t err = this->WaitForBandWidthReply();
|
2009-11-08 17:10:19 +01:00
|
|
|
if (err == OK) {
|
2009-11-08 13:50:56 +01:00
|
|
|
this->network_connection_state = CONN_CONNECTED;
|
2009-11-08 17:10:19 +01:00
|
|
|
return AGAIN_ERROR;
|
|
|
|
}
|
2009-11-08 13:50:56 +01:00
|
|
|
return err;
|
|
|
|
} break;
|
2009-02-16 20:20:00 +01:00
|
|
|
case CONN_CONNECTED:
|
2010-02-14 10:24:49 +01:00
|
|
|
Gui::gui->status_bar->queueMessage("Connected!");
|
2009-10-28 19:04:30 +01:00
|
|
|
/* The lowest number is the default master */
|
2009-02-13 08:57:40 +01:00
|
|
|
default:
|
2009-04-04 09:40:17 +02:00
|
|
|
return OK;
|
2009-02-13 08:57:40 +01:00
|
|
|
}
|
2009-02-08 12:52:09 +01:00
|
|
|
|
2009-04-04 09:40:17 +02:00
|
|
|
return AGAIN_ERROR;
|
2009-01-24 16:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-01 20:47:21 +01:00
|
|
|
void Network::Disconnect()
|
2009-01-29 19:04:31 +01:00
|
|
|
{
|
2009-02-03 19:10:09 +01:00
|
|
|
NetworkUpdate *disconnect = InitNetworkUpdate(this->cur_ud, DISCONNECT,
|
|
|
|
sizeof(NetworkUpdate));
|
2009-01-29 19:04:31 +01:00
|
|
|
|
|
|
|
/* Add a stop at the end of the update */
|
|
|
|
this->AddNetworkUpdate(disconnect);
|
2010-02-14 09:57:55 +01:00
|
|
|
this->SendServerUpdate();
|
|
|
|
if (this->network_connection_state)
|
|
|
|
this->SendPeerUpdate();
|
2009-01-29 19:04:31 +01:00
|
|
|
}
|
|
|
|
|
2009-04-26 08:02:57 +02:00
|
|
|
bool Network::networking_started = false;
|
2009-02-02 20:51:58 +01:00
|
|
|
|
2009-02-21 08:59:45 +01:00
|
|
|
#if defined(GEKKO)
|
2009-02-07 12:28:29 +01:00
|
|
|
#include "NetworkWii.h"
|
|
|
|
#else
|
2009-01-19 07:44:19 +01:00
|
|
|
#include "NetworkUnix.h"
|
2009-02-07 12:28:29 +01:00
|
|
|
#endif
|