Simplifly data registry and reserve 0...1000 as client range

This commit is contained in:
simon.kagstrom 2010-02-07 09:52:36 +00:00
parent 6b4014dfca
commit 56c4742f75
4 changed files with 22 additions and 31 deletions

View File

@ -976,7 +976,6 @@ bool Network::AppendScreenshot(NetworkUpdatePeerInfo *pi)
NetworkUpdateRegisterData *dsu; NetworkUpdateRegisterData *dsu;
NetworkUpdate *ud; NetworkUpdate *ud;
SDL_Surface *scr; SDL_Surface *scr;
struct ds_data *data;
void *png; void *png;
size_t sz; size_t sz;
bool out = NULL; bool out = NULL;
@ -989,20 +988,15 @@ bool Network::AppendScreenshot(NetworkUpdatePeerInfo *pi)
if (!png) if (!png)
goto out_scr; goto out_scr;
data = DataStore::ds->embedData(png, sz);
if (!data)
goto out_png;
ud = InitNetworkUpdate(this->ud, REGISTER_DATA, ud = InitNetworkUpdate(this->ud, REGISTER_DATA,
sizeof(NetworkUpdate) + sizeof(NetworkUpdateRegisterData) + sz); sizeof(NetworkUpdate) + sizeof(NetworkUpdateRegisterData) + sz);
dsu = (NetworkUpdateRegisterData *)ud->data; dsu = (NetworkUpdateRegisterData *)ud->data;
dsu->key = data->key; dsu->key = DataStore::ds->getNextKey();
dsu->metadata = data->metadata; dsu->metadata = 0;
memcpy(dsu->data, data->data, sz); memcpy(dsu->data, png, sz);
this->AddNetworkUpdate(ud); this->AddNetworkUpdate(ud);
out = true; out = true;
free(data);
out_png:
free(png); free(png);
out_scr: out_scr:
SDL_FreeSurface(scr); SDL_FreeSurface(scr);

View File

@ -68,20 +68,14 @@ struct ds_data *DataStore::registerData(uint32_t key,
return NULL; return NULL;
} }
struct ds_data *DataStore::embedData(void *data, size_t sz) uint32_t DataStore::getNextKey()
{ {
struct ds_data *out; uint32_t out = this->key_counter;
out = (struct ds_data *)xmalloc(sizeof(struct ds_data) + sz); this->key_counter += 2;
out->key = this->key_counter; if (this->key_counter > DATA_KEY_RANGE)
out->metadata = 0; /* Setup by the embedder */ this->key_counter = 1;
memcpy(out->data, data, sz);
panic_if(this->registerData(out->key, out) != NULL,
"Registering new data with key %u was non-NULL\n",
out->key);
out->key += 2;
return out; return out;
} }

View File

@ -3,6 +3,8 @@
#include <stdint.h> #include <stdint.h>
#define DATA_KEY_RANGE 1000
struct ds_data struct ds_data
{ {
uint32_t key; uint32_t key;
@ -20,28 +22,24 @@ public:
~DataStore(); ~DataStore();
/** /**
* Register a new datum. * Register a new datum from the network.
* *
* @param key The key to register with * @param key The key to register with
* @param metadata The metadata
* @param data The data to register * @param data The data to register
* @param data_sz The size of the data
* *
* @return the old datum with that key, or NULL * @return the old datum with that key, or NULL
*/ */
struct ds_data *registerData(uint32_t key, struct ds_data *data);
struct ds_data *registerNetworkData(uint32_t key, uint32_t metadata, struct ds_data *registerNetworkData(uint32_t key, uint32_t metadata,
void *data, size_t data_sz); void *data, size_t data_sz);
/** /**
* Embed existing data into a data store. The new data is reallocated, * Get the next key for registered data
* but the old is not freed
* *
* @param data The data to embed. * @return a valid key
* @param sz the size of the data
*
* @return the new data store data.
*/ */
struct ds_data *embedData(void *data, size_t sz); uint32_t getNextKey();
/** /**
* Unregister a datum. * Unregister a datum.
@ -57,6 +55,8 @@ public:
static DataStore *ds; static DataStore *ds;
private: private:
struct ds_data *registerData(uint32_t key, struct ds_data *data);
struct ds_data **registeredData; struct ds_data **registeredData;
int n_registeredData; int n_registeredData;

View File

@ -467,7 +467,7 @@ class Broker(SocketServer.UDPServer):
self.stat_data = stat_data self.stat_data = stat_data
self.image_dir = image_dir self.image_dir = image_dir
self.data_key = 0 self.data_key = DATA_KEY_RANGE
stats.load(self.stat_data) stats.load(self.stat_data)
try: try:
@ -479,6 +479,9 @@ class Broker(SocketServer.UDPServer):
def get_data_key(self): def get_data_key(self):
out = self.data_key out = self.data_key
self.data_key = (self.data_key + DATA_KEY_RANGE) & 0xffffffff self.data_key = (self.data_key + DATA_KEY_RANGE) & 0xffffffff
# Start at DATA_KEY_RANGE
if self.data_key < DATA_KEY_RANGE:
self.data_key = self.data_key + DATA_KEY_RANGE
return out return out