mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-15 15:41:24 +01:00
replace string_printf() with new class derived from std::string with
printf() method.
This commit is contained in:
parent
23bdec0ea1
commit
8da0b73744
13 changed files with 213 additions and 153 deletions
|
@ -31,6 +31,7 @@ SRC = \
|
|||
lstnrmgr.cpp \
|
||||
permid.cpp \
|
||||
states.cpp \
|
||||
strwpf.cpp \
|
||||
timermgr.cpp \
|
||||
tpool.cpp \
|
||||
udpack.cpp \
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
#include "cidlock.h"
|
||||
#include "mlock.h"
|
||||
#include "strwpf.h"
|
||||
|
||||
// #define CIDLOCK_DEBUG
|
||||
|
||||
|
@ -70,8 +71,8 @@ void
|
|||
CidLock::print_claimed( const char* caller )
|
||||
{
|
||||
int unclaimed = 0;
|
||||
string str;
|
||||
string_printf( str, "after %s: ", caller );
|
||||
StrWPF str;
|
||||
str.printf( "after %s: ", caller );
|
||||
// Assume we have the mutex!!!!
|
||||
map< CookieID, CidInfo*>::const_iterator iter;
|
||||
for ( iter = m_infos.begin(); iter != m_infos.end(); ++iter ) {
|
||||
|
@ -79,10 +80,10 @@ CidLock::print_claimed( const char* caller )
|
|||
if ( 0 == info->GetOwner() ) {
|
||||
++unclaimed;
|
||||
} else {
|
||||
string_printf( str, "%d,", info->GetCid() );
|
||||
str. printf( "%d,", info->GetCid() );
|
||||
}
|
||||
}
|
||||
string_printf( str, " (plus %d unclaimed.)", unclaimed );
|
||||
str.printf( " (plus %d unclaimed.)", unclaimed );
|
||||
logf( XW_LOGINFO, "%s: claimed: %s", __func__, str.c_str() );
|
||||
}
|
||||
#else
|
||||
|
|
|
@ -50,6 +50,7 @@
|
|||
#include "tpool.h"
|
||||
#include "devmgr.h"
|
||||
#include "udpack.h"
|
||||
#include "strwpf.h"
|
||||
|
||||
/* this is *only* for testing. Don't abuse!!!! */
|
||||
extern pthread_rwlock_t gCookieMapRWLock;
|
||||
|
@ -555,7 +556,7 @@ static bool
|
|||
cmd_acks( int socket, const char* cmd, int argc, gchar** argv )
|
||||
{
|
||||
bool found = false;
|
||||
string result;
|
||||
StrWPF result;
|
||||
|
||||
if ( 1 >= argc ) {
|
||||
/* missing param; let help print */
|
||||
|
@ -578,9 +579,9 @@ cmd_acks( int socket, const char* cmd, int argc, gchar** argv )
|
|||
"* %s list\n"
|
||||
,"* %s nack all\n"
|
||||
};
|
||||
string help;
|
||||
StrWPF help;
|
||||
for ( size_t ii = 0; ii < VSIZE(strs); ++ii ) {
|
||||
string_printf( help, strs[ii], cmd );
|
||||
help.printf( strs[ii], cmd );
|
||||
}
|
||||
send( socket, help.c_str(), help.size(), 0 );
|
||||
}
|
||||
|
@ -591,7 +592,7 @@ static bool
|
|||
cmd_devs( int socket, const char* cmd, int argc, gchar** argv )
|
||||
{
|
||||
bool found = false;
|
||||
string result;
|
||||
StrWPF result;
|
||||
if ( 1 >= argc ) {
|
||||
/* missing param; let help print */
|
||||
} else {
|
||||
|
@ -619,7 +620,7 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** argv )
|
|||
DevMgr::Get()->printDevices( result, devids );
|
||||
} else {
|
||||
int deleted = DevMgr::Get()->forgetDevices( devids );
|
||||
string_printf( result, "Deleted %d devices\n", deleted );
|
||||
result.printf( "Deleted %d devices\n", deleted );
|
||||
}
|
||||
}
|
||||
} else if ( 0 == strcmp( "ping", arg1 ) ) {
|
||||
|
@ -644,9 +645,9 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** argv )
|
|||
if ( 0 != devid ) {
|
||||
if ( post_message( devid, unesc, onAckProc,
|
||||
(void*)socket ) ) {
|
||||
string_printf( result, "posted message: %s\n", unesc );
|
||||
result.printf( "posted message: %s\n", unesc );
|
||||
} else {
|
||||
string_printf( result, "unable to post; does "
|
||||
result.printf( "unable to post; does "
|
||||
"dev %d exist\n", devid );
|
||||
}
|
||||
}
|
||||
|
@ -671,9 +672,9 @@ cmd_devs( int socket, const char* cmd, int argc, gchar** argv )
|
|||
"* %s rm [all | <id>+]\n"
|
||||
};
|
||||
|
||||
string help;
|
||||
StrWPF help;
|
||||
for ( size_t ii = 0; ii < VSIZE(strs); ++ii ) {
|
||||
string_printf( help, strs[ii], cmd );
|
||||
help.printf( strs[ii], cmd );
|
||||
}
|
||||
send( socket, help.c_str(), help.size(), 0 );
|
||||
}
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include <glib.h>
|
||||
|
||||
#include "dbmgr.h"
|
||||
#include "strwpf.h"
|
||||
#include "mlock.h"
|
||||
#include "configs.h"
|
||||
#include "xwrelay_priv.h"
|
||||
|
@ -138,8 +139,8 @@ DBMgr::FindGameFor( const char* connName, char* cookieBuf, int bufLen,
|
|||
GAMES_TABLE " WHERE connName = '%s' AND nTotal = %d "
|
||||
"AND %d = seeds[%d] AND 'A' = ack[%d] "
|
||||
;
|
||||
string query;
|
||||
string_printf( query, fmt, connName, nPlayersS, seed, hid, hid );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName, nPlayersS, seed, hid, hid );
|
||||
logf( XW_LOGINFO, "query: %s", query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -167,8 +168,8 @@ DBMgr::FindGame( const char* connName, char* cookieBuf, int bufLen,
|
|||
GAMES_TABLE " WHERE connName = '%s'"
|
||||
// " LIMIT 1"
|
||||
;
|
||||
string query;
|
||||
string_printf( query, fmt, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName );
|
||||
logf( XW_LOGINFO, "query: %s", query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -195,8 +196,8 @@ DBMgr::FindPlayer( DevIDRelay relayID, AddrInfo::ClientToken token,
|
|||
|
||||
const char* fmt =
|
||||
"SELECT connName FROM %s WHERE %d = ANY(devids) AND %d = ANY(tokens)";
|
||||
string query;
|
||||
string_printf( query, fmt, GAMES_TABLE, relayID, token );
|
||||
StrWPF query;
|
||||
query.printf( fmt, GAMES_TABLE, relayID, token );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
int nTuples = PQntuples( result );
|
||||
|
@ -213,9 +214,9 @@ DBMgr::FindPlayer( DevIDRelay relayID, AddrInfo::ClientToken token,
|
|||
for ( HostID hid = 1; hid <= MAX_NUM_PLAYERS; ++hid ) {
|
||||
fmt = "SELECT seeds[%d] FROM %s WHERE connname = '%s' "
|
||||
"AND devids[%d] = %d AND tokens[%d] = %d";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, GAMES_TABLE, name,
|
||||
hid, relayID, hid, token );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, GAMES_TABLE, name,
|
||||
hid, relayID, hid, token );
|
||||
result = PQexec( getThreadConn(), query.c_str() );
|
||||
int nTuples2 = PQntuples( result );
|
||||
for ( int jj = 0; jj < nTuples2; ++jj ) {
|
||||
|
@ -323,8 +324,8 @@ DBMgr::AllDevsAckd( const char* const connName )
|
|||
{
|
||||
const char* cmd = "SELECT ntotal=sum_array(nperdevice) AND 'A'=ALL(ack) from " GAMES_TABLE
|
||||
" WHERE connName='%s'";
|
||||
string query;
|
||||
string_printf( query, cmd, connName );
|
||||
StrWPF query;
|
||||
query.printf( cmd, connName );
|
||||
logf( XW_LOGINFO, "query: %s", query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -405,22 +406,21 @@ DBMgr::UpdateDevice( DevIDRelay relayID, int clientVersion,
|
|||
{
|
||||
bool exists = !check;
|
||||
if ( !exists ) {
|
||||
string test;
|
||||
string_printf( test, "id = %d", relayID );
|
||||
StrWPF test;
|
||||
test.printf( "id = %d", relayID );
|
||||
exists = 1 == getCountWhere( DEVICES_TABLE, test );
|
||||
}
|
||||
|
||||
if ( exists ) {
|
||||
string query;
|
||||
string_printf( query, "UPDATE " DEVICES_TABLE " SET mtime='now'" );
|
||||
StrWPF query;
|
||||
query.printf( "UPDATE " DEVICES_TABLE " SET mtime='now'" );
|
||||
if ( NULL != desc && '\0' != desc[0] ) {
|
||||
string_printf( query, ", clntVers=%d, versDesc='%s'",
|
||||
clientVersion, desc );
|
||||
query.printf( ", clntVers=%d, versDesc='%s'", clientVersion, desc );
|
||||
}
|
||||
if ( NULL != model && '\0' != model[0] ) {
|
||||
string_printf( query, ", model='%s'", model );
|
||||
query.printf( ", model='%s'", model );
|
||||
}
|
||||
string_printf( query, " WHERE id = %d", relayID );
|
||||
query.printf( " WHERE id = %d", relayID );
|
||||
execSql( query );
|
||||
}
|
||||
return exists;
|
||||
|
@ -463,22 +463,22 @@ DBMgr::AddDevice( const char* connName, HostID curID, int clientVersion,
|
|||
}
|
||||
assert( newID <= 4 );
|
||||
|
||||
string query;
|
||||
string_printf( query, "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = %d,"
|
||||
StrWPF query;
|
||||
query.printf( "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = %d,"
|
||||
" clntVers[%d] = %d, seeds[%d] = %d, addrs[%d] = \'%s\', ",
|
||||
newID, nToAdd, newID, clientVersion, newID, seed, newID,
|
||||
inet_ntoa( addr->sin_addr() ) );
|
||||
if ( DEVID_NONE != devID ) {
|
||||
string_printf( query, "devids[%d] = %d, ", newID, devID );
|
||||
query.printf( "devids[%d] = %d, ", newID, devID );
|
||||
}
|
||||
string_printf( query, " tokens[%d] = %d, mtimes[%d]='now', ack[%d]=\'%c\'"
|
||||
" WHERE connName = '%s'", newID, addr->clientToken(),
|
||||
newID, newID, ackd?'A':'a', connName );
|
||||
query.printf( " tokens[%d] = %d, mtimes[%d]='now', ack[%d]=\'%c\'"
|
||||
" WHERE connName = '%s'", newID, addr->clientToken(),
|
||||
newID, newID, ackd?'A':'a', connName );
|
||||
|
||||
// Update the devices table too. Eventually the clntVers field of the
|
||||
// games table should go away.
|
||||
if ( DEVID_NONE != devID ) {
|
||||
string_printf( query, "; UPDATE " DEVICES_TABLE " SET clntVers = %d"
|
||||
query.printf( "; UPDATE " DEVICES_TABLE " SET clntVers = %d"
|
||||
" WHERE id = %d", clientVersion, devID );
|
||||
}
|
||||
|
||||
|
@ -493,8 +493,8 @@ DBMgr::NoteAckd( const char* const connName, HostID id )
|
|||
{
|
||||
const char* fmt = "UPDATE " GAMES_TABLE " SET ack[%d]='A'"
|
||||
" WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, id, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, id, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
execSql( query );
|
||||
|
@ -505,8 +505,8 @@ DBMgr::RmDeviceByHid( const char* connName, HostID hid )
|
|||
{
|
||||
const char* fmt = "UPDATE " GAMES_TABLE " SET nPerDevice[%d] = 0, "
|
||||
"seeds[%d] = 0, ack[%d]='-', mtimes[%d]='now' WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, hid, hid, hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, hid, hid, hid, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
return execSql( query );
|
||||
|
@ -520,8 +520,8 @@ DBMgr::HIDForSeed( const char* const connName, unsigned short seed )
|
|||
const char* fmt = "SELECT seeds FROM " GAMES_TABLE
|
||||
" WHERE connName = '%s'"
|
||||
" AND %d = ANY(seeds)";
|
||||
string query;
|
||||
string_printf( query, fmt, connName, seed );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName, seed );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
if ( 1 == PQntuples( result ) ) {
|
||||
|
@ -567,8 +567,8 @@ DBMgr::HaveDevice( const char* connName, HostID hid, int seed )
|
|||
bool found = false;
|
||||
const char* fmt = "SELECT * from " GAMES_TABLE
|
||||
" WHERE connName = '%s' AND seeds[%d] = %d";
|
||||
string query;
|
||||
string_printf( query, fmt, connName, hid, seed );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName, hid, seed );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
found = 1 == PQntuples( result );
|
||||
|
@ -581,8 +581,8 @@ DBMgr::AddCID( const char* const connName, CookieID cid )
|
|||
{
|
||||
const char* fmt = "UPDATE " GAMES_TABLE " SET cid = %d "
|
||||
" WHERE connName = '%s' AND cid IS NULL";
|
||||
string query;
|
||||
string_printf( query, fmt, cid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, cid, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
bool result = execSql( query );
|
||||
|
@ -595,8 +595,8 @@ DBMgr::ClearCID( const char* connName )
|
|||
{
|
||||
const char* fmt = "UPDATE " GAMES_TABLE " SET cid = null "
|
||||
"WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
execSql( query );
|
||||
|
@ -609,8 +609,8 @@ DBMgr::RecordSent( const char* const connName, HostID hid, int nBytes )
|
|||
const char* fmt = "UPDATE " GAMES_TABLE " SET"
|
||||
" nsents[%d] = nsents[%d] + %d, mtimes[%d] = 'now'"
|
||||
" WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, hid, nBytes, hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, hid, nBytes, hid, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
execSql( query );
|
||||
|
@ -620,10 +620,11 @@ void
|
|||
DBMgr::RecordSent( const int* msgIDs, int nMsgIDs )
|
||||
{
|
||||
if ( nMsgIDs > 0 ) {
|
||||
string query( "SELECT connname,hid,sum(msglen)"
|
||||
StrWPF query;
|
||||
query.printf( "SELECT connname,hid,sum(msglen)"
|
||||
" FROM " MSGS_TABLE " WHERE id IN (" );
|
||||
for ( int ii = 0; ; ) {
|
||||
string_printf( query, "%d", msgIDs[ii] );
|
||||
query.printf( "%d", msgIDs[ii] );
|
||||
if ( ++ii == nMsgIDs ) {
|
||||
break;
|
||||
} else {
|
||||
|
@ -652,9 +653,9 @@ DBMgr::RecordAddress( const char* const connName, HostID hid,
|
|||
assert( hid >= 0 && hid <= 4 );
|
||||
const char* fmt = "UPDATE " GAMES_TABLE " SET addrs[%d] = \'%s\'"
|
||||
" WHERE connName = '%s'";
|
||||
string query;
|
||||
StrWPF query;
|
||||
char* ntoa = inet_ntoa( addr->sin_addr() );
|
||||
string_printf( query, fmt, hid, ntoa, connName );
|
||||
query.printf( fmt, hid, ntoa, connName );
|
||||
logf( XW_LOGVERBOSE0, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
execSql( query );
|
||||
|
@ -665,8 +666,8 @@ DBMgr::GetPlayerCounts( const char* const connName, int* nTotal, int* nHere )
|
|||
{
|
||||
const char* fmt = "SELECT ntotal, sum_array(nperdevice) FROM " GAMES_TABLE
|
||||
" WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -682,8 +683,8 @@ DBMgr::KillGame( const char* const connName, int hid )
|
|||
const char* fmt = "UPDATE " GAMES_TABLE " SET dead = TRUE,"
|
||||
" nperdevice[%d] = - nperdevice[%d]"
|
||||
" WHERE connName = '%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, hid, connName );
|
||||
execSql( query );
|
||||
}
|
||||
|
||||
|
@ -705,8 +706,8 @@ DBMgr::PublicRooms( int lang, int nPlayers, int* nNames, string& names )
|
|||
" AND nTotal>sum_array(nPerDevice)"
|
||||
" AND nTotal = %d";
|
||||
|
||||
string query;
|
||||
string_printf( query, fmt, lang, nPlayers );
|
||||
StrWPF query;
|
||||
query.printf( fmt, lang, nPlayers );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -730,8 +731,8 @@ DBMgr::TokenFor( const char* const connName, int hid, DevIDRelay* devid,
|
|||
bool found = false;
|
||||
const char* fmt = "SELECT tokens[%d], devids[%d] FROM " GAMES_TABLE
|
||||
" WHERE connName='%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, hid, connName );
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
if ( 1 == PQntuples( result ) ) {
|
||||
AddrInfo::ClientToken token_tmp = atoi( PQgetvalue( result, 0, 0 ) );
|
||||
|
@ -785,8 +786,8 @@ DBMgr::readArray( const char* const connName, const char* column, int arr[] ) /
|
|||
{
|
||||
const char* fmt = "SELECT %s FROM " GAMES_TABLE " WHERE connName='%s'";
|
||||
|
||||
string query;
|
||||
string_printf( query, fmt, column, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, column, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -802,8 +803,8 @@ DBMgr::getDevID( const char* connName, int hid )
|
|||
{
|
||||
DevIDRelay devID = DEVID_NONE;
|
||||
const char* fmt = "SELECT devids[%d] FROM " GAMES_TABLE " WHERE connName='%s'";
|
||||
string query;
|
||||
string_printf( query, fmt, hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( fmt, hid, connName );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -819,19 +820,19 @@ DBMgr::getDevID( const DevID* devID )
|
|||
{
|
||||
DevIDRelay rDevID = DEVID_NONE;
|
||||
DevIDType devIDType = devID->m_devIDType;
|
||||
string query;
|
||||
StrWPF query;
|
||||
assert( ID_TYPE_NONE < devIDType );
|
||||
if ( ID_TYPE_RELAY == devIDType ) {
|
||||
// confirm it's there
|
||||
DevIDRelay cur = devID->asRelayID();
|
||||
if ( DEVID_NONE != cur ) {
|
||||
const char* fmt = "SELECT id FROM " DEVICES_TABLE " WHERE id=%d";
|
||||
string_printf( query, fmt, cur );
|
||||
query.printf( fmt, cur );
|
||||
}
|
||||
} else if ( 0 < devID->m_devIDString.size() ) {
|
||||
const char* fmt = "SELECT id FROM " DEVICES_TABLE
|
||||
" WHERE devtype=%d and devid = '%s'";
|
||||
string_printf( query, fmt, devIDType, devID->m_devIDString.c_str() );
|
||||
query.printf( fmt, devIDType, devID->m_devIDString.c_str() );
|
||||
}
|
||||
|
||||
if ( 0 < query.size() ) {
|
||||
|
@ -860,13 +861,13 @@ DBMgr::getDevID( const DevID* devID )
|
|||
int
|
||||
DBMgr::CountStoredMessages( const char* const connName, int hid )
|
||||
{
|
||||
string test;
|
||||
string_printf( test, "connname = '%s'", connName );
|
||||
StrWPF test;
|
||||
test.printf( "connname = '%s'", connName );
|
||||
#ifdef HAVE_STIME
|
||||
string_printf( test, " AND stime IS NULL" );
|
||||
test.printf( " AND stime IS NULL" );
|
||||
#endif
|
||||
if ( hid != -1 ) {
|
||||
string_printf( test, " AND hid = %d", hid );
|
||||
test.printf( " AND hid = %d", hid );
|
||||
}
|
||||
|
||||
return getCountWhere( MSGS_TABLE, test );
|
||||
|
@ -881,10 +882,10 @@ DBMgr::CountStoredMessages( const char* const connName )
|
|||
int
|
||||
DBMgr::CountStoredMessages( DevIDRelay relayID )
|
||||
{
|
||||
string test;
|
||||
string_printf( test, "devid = %d", relayID );
|
||||
StrWPF test;
|
||||
test.printf( "devid = %d", relayID );
|
||||
#ifdef HAVE_STIME
|
||||
string_printf( test, "AND stime IS NULL" );
|
||||
test.printf( "AND stime IS NULL" );
|
||||
#endif
|
||||
|
||||
return getCountWhere( MSGS_TABLE, test );
|
||||
|
@ -900,16 +901,16 @@ DBMgr::StoreMessage( DevIDRelay devID, const uint8_t* const buf,
|
|||
const char* fmt = "INSERT INTO " MSGS_TABLE " "
|
||||
"(devid, %s, msglen) VALUES(%d, %s'%s', %d)";
|
||||
|
||||
string query;
|
||||
StrWPF query;
|
||||
if ( m_useB64 ) {
|
||||
gchar* b64 = g_base64_encode( buf, len );
|
||||
string_printf( query, fmt, "msg64", devID, "", b64, len );
|
||||
query.printf( fmt, "msg64", devID, "", b64, len );
|
||||
g_free( b64 );
|
||||
} else {
|
||||
uint8_t* bytes = PQescapeByteaConn( getThreadConn(), buf,
|
||||
len, &newLen );
|
||||
assert( NULL != bytes );
|
||||
string_printf( query, fmt, "msg", devID, "E", bytes, len );
|
||||
query.printf( fmt, "msg", devID, "E", bytes, len );
|
||||
PQfreemem( bytes );
|
||||
}
|
||||
|
||||
|
@ -936,19 +937,19 @@ DBMgr::StoreMessage( const char* const connName, int hid,
|
|||
"(SELECT tokens[%d] from " GAMES_TABLE " where connname='%s'), "
|
||||
"%s'%s', %d)";
|
||||
|
||||
string query;
|
||||
StrWPF query;
|
||||
if ( m_useB64 ) {
|
||||
gchar* b64 = g_base64_encode( buf, len );
|
||||
string_printf( query, fmt, "msg64", connName, hid, devID, hid, connName,
|
||||
"", b64, len );
|
||||
query.printf( fmt, "msg64", connName, hid, devID, hid, connName,
|
||||
"", b64, len );
|
||||
g_free( b64 );
|
||||
} else {
|
||||
uint8_t* bytes = PQescapeByteaConn( getThreadConn(), buf,
|
||||
len, &newLen );
|
||||
assert( NULL != bytes );
|
||||
|
||||
string_printf( query, fmt, "msg", connName, hid, devID, hid, connName,
|
||||
"E", bytes, len );
|
||||
query.printf( fmt, "msg", connName, hid, devID, hid, connName,
|
||||
"E", bytes, len );
|
||||
PQfreemem( bytes );
|
||||
}
|
||||
|
||||
|
@ -991,9 +992,9 @@ void
|
|||
DBMgr::storedMessagesImpl( string test, vector<DBMgr::MsgInfo>& msgs,
|
||||
bool nullConnnameOK )
|
||||
{
|
||||
string query;
|
||||
string_printf( query, "SELECT id, msg64, msg, msglen, token, connname FROM "
|
||||
MSGS_TABLE " WHERE %s "
|
||||
StrWPF query;
|
||||
query.printf( "SELECT id, msg64, msg, msglen, token, connname FROM "
|
||||
MSGS_TABLE " WHERE %s "
|
||||
#ifdef HAVE_STIME
|
||||
" AND stime IS NULL "
|
||||
#endif
|
||||
|
@ -1001,10 +1002,9 @@ DBMgr::storedMessagesImpl( string test, vector<DBMgr::MsgInfo>& msgs,
|
|||
" WHERE NOT " GAMES_TABLE ".dead)", test.c_str() );
|
||||
|
||||
if ( nullConnnameOK ) {
|
||||
string_printf( query, " OR connname IS NULL ");
|
||||
query.printf( " OR connname IS NULL ");
|
||||
}
|
||||
string_printf( query, ") ORDER BY id" );
|
||||
|
||||
query.printf( ") ORDER BY id" );
|
||||
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
|
@ -1032,8 +1032,8 @@ void
|
|||
DBMgr::GetStoredMessages( DevIDRelay relayID, vector<MsgInfo>& msgs )
|
||||
{
|
||||
if ( !hasNoMessages( relayID ) ) {
|
||||
string query;
|
||||
string_printf( query, "devid=%d", relayID );
|
||||
StrWPF query;
|
||||
query.printf( "devid=%d", relayID );
|
||||
storedMessagesImpl( query, msgs, true );
|
||||
|
||||
if ( 0 == msgs.size() ) {
|
||||
|
@ -1046,8 +1046,8 @@ void
|
|||
DBMgr::GetStoredMessages( const char* const connName, HostID hid,
|
||||
vector<DBMgr::MsgInfo>& msgs )
|
||||
{
|
||||
string query;
|
||||
string_printf( query, "hid = %d AND connname = '%s'", hid, connName );
|
||||
StrWPF query;
|
||||
query.printf( "hid = %d AND connname = '%s'", hid, connName );
|
||||
storedMessagesImpl( query, msgs, false );
|
||||
}
|
||||
|
||||
|
@ -1061,8 +1061,8 @@ DBMgr::RemoveStoredMessages( string& msgids )
|
|||
"DELETE FROM " MSGS_TABLE
|
||||
#endif
|
||||
" WHERE id IN (%s)";
|
||||
string query;
|
||||
string_printf( query, fmt, msgids.c_str() );
|
||||
StrWPF query;
|
||||
query.printf( fmt, msgids.c_str() );
|
||||
logf( XW_LOGINFO, "%s: query: %s", __func__, query.c_str() );
|
||||
execSql( query );
|
||||
}
|
||||
|
@ -1071,11 +1071,11 @@ void
|
|||
DBMgr::RemoveStoredMessages( const int* msgIDs, int nMsgIDs )
|
||||
{
|
||||
if ( nMsgIDs > 0 ) {
|
||||
string ids;
|
||||
StrWPF ids;
|
||||
size_t len = 0;
|
||||
int ii;
|
||||
for ( ii = 0; ; ) {
|
||||
string_printf( ids, "%d", msgIDs[ii] );
|
||||
ids.printf( "%d", msgIDs[ii] );
|
||||
assert( len < sizeof(ids) );
|
||||
if ( ++ii == nMsgIDs ) {
|
||||
break;
|
||||
|
@ -1091,14 +1091,14 @@ void
|
|||
DBMgr::RemoveStoredMessages( vector<int>& idv )
|
||||
{
|
||||
if ( 0 < idv.size() ) {
|
||||
string ids;
|
||||
StrWPF ids;
|
||||
vector<int>::const_iterator iter = idv.begin();
|
||||
for ( ; ; ) {
|
||||
string_printf( ids, "%d", *iter );
|
||||
ids.printf( "%d", *iter );
|
||||
if ( ++iter == idv.end() ) {
|
||||
break;
|
||||
}
|
||||
string_printf( ids, "," );
|
||||
ids.printf( "," );
|
||||
}
|
||||
RemoveStoredMessages( ids );
|
||||
}
|
||||
|
@ -1113,8 +1113,8 @@ DBMgr::RemoveStoredMessage( const int msgID )
|
|||
int
|
||||
DBMgr::getCountWhere( const char* table, string& test )
|
||||
{
|
||||
string query;
|
||||
string_printf( query, "SELECT count(*) FROM %s WHERE %s", table, test.c_str() );
|
||||
StrWPF query;
|
||||
query.printf( "SELECT count(*) FROM %s WHERE %s", table, test.c_str() );
|
||||
|
||||
PGresult* result = PQexec( getThreadConn(), query.c_str() );
|
||||
assert( 1 == PQntuples( result ) );
|
||||
|
@ -1219,9 +1219,9 @@ DBMgr::getThreadConn( void )
|
|||
if ( !RelayConfigs::GetConfigs()->GetValueFor( "DB_PORT", &port ) ) {
|
||||
assert( 0 );
|
||||
}
|
||||
string params;
|
||||
string_printf( params, "dbname = %s ", buf );
|
||||
string_printf( params, "port = %d ", port );
|
||||
StrWPF params;
|
||||
params.printf( "dbname = %s ", buf );
|
||||
params.printf( "port = %d ", port );
|
||||
|
||||
conn = PQconnectdb( params.c_str() );
|
||||
pthread_setspecific( m_conn_key, conn );
|
||||
|
|
|
@ -139,7 +139,7 @@ DevMgr::getKnownDevices( vector<DevIDRelay>& devids )
|
|||
// it, so that as much work as possible is done on the calling thread without
|
||||
// holding up more important stuff.
|
||||
void
|
||||
DevMgr::printDevices( string& str, vector<DevIDRelay> devids )
|
||||
DevMgr::printDevices( StrWPF& str, vector<DevIDRelay> devids )
|
||||
{
|
||||
map<uint32_t, DevIDRelay> agedDevs;
|
||||
{
|
||||
|
@ -177,8 +177,8 @@ DevMgr::printDevices( string& str, vector<DevIDRelay> devids )
|
|||
uint32_t age = *keysIter;
|
||||
DevIDRelay devid = agedDevs.find( age )->second;
|
||||
age = now - age;
|
||||
string_printf( str, "%.3d: devid: % 10d; age: %.3d seconds\n", ++row,
|
||||
devid, age );
|
||||
str.printf( "%.3d: devid: % 10d; age: %.3d seconds\n", ++row,
|
||||
devid, age );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include "xwrelay_priv.h"
|
||||
#include "addrinfo.h"
|
||||
#include "strwpf.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -35,7 +36,8 @@ class DevMgr {
|
|||
const AddrInfo::AddrUnion* get( DevIDRelay devid );
|
||||
|
||||
/* Called from ctrl port */
|
||||
void printDevices( string& str, vector<DevIDRelay> devids /* empty means all */ );
|
||||
void printDevices( StrWPF& str,
|
||||
vector<DevIDRelay> devids /* empty means all */ );
|
||||
int forgetDevices( vector<DevIDRelay>& devids );
|
||||
void getKnownDevices( vector<DevIDRelay>& devids );
|
||||
|
||||
|
|
52
xwords4/relay/strwpf.cpp
Normal file
52
xwords4/relay/strwpf.cpp
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* -*- compile-command: "make -k -j3"; -*- */
|
||||
|
||||
/*
|
||||
* Copyright 2013 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||
*
|
||||
* 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 <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "strwpf.h"
|
||||
|
||||
/* From stack overflow: snprintf with an expanding buffer.
|
||||
*/
|
||||
|
||||
void
|
||||
StrWPF::printf( const char* fmt, ... )
|
||||
{
|
||||
const int origsiz = size();
|
||||
int addsiz = 100;
|
||||
va_list ap;
|
||||
for ( ; ; ) {
|
||||
resize( origsiz + addsiz );
|
||||
|
||||
va_start( ap, fmt );
|
||||
int len = vsnprintf( (char *)c_str() + origsiz, addsiz, fmt, ap );
|
||||
va_end( ap );
|
||||
|
||||
if ( len >= addsiz ) { // needs more space
|
||||
addsiz = len + 1;
|
||||
} else if ( -1 == len ) {
|
||||
assert(0); // should be impossible
|
||||
} else {
|
||||
resize( origsiz + len );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
30
xwords4/relay/strwpf.h
Normal file
30
xwords4/relay/strwpf.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/* -*-mode: C; fill-column: 78; c-basic-offset: 4; -*- */
|
||||
/*
|
||||
* Copyright 2013 by Eric House (xwords@eehouse.org). All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef _STRWPF_H_
|
||||
#define _STRWPF_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
class StrWPF : public std::string {
|
||||
public:
|
||||
void printf( const char* fmt, ... );
|
||||
};
|
||||
|
||||
#endif
|
|
@ -36,6 +36,7 @@
|
|||
#include "xwrelay.h"
|
||||
#include "timermgr.h"
|
||||
#include "mlock.h"
|
||||
#include "strwpf.h"
|
||||
|
||||
XWThreadPool* XWThreadPool::g_instance = NULL;
|
||||
|
||||
|
@ -492,12 +493,12 @@ XWThreadPool::release_socket_locked( int socket )
|
|||
void
|
||||
XWThreadPool::print_in_use( void )
|
||||
{
|
||||
string str;
|
||||
StrWPF str;
|
||||
set<int>::iterator iter;
|
||||
|
||||
for ( iter = m_sockets_in_use.begin();
|
||||
iter != m_sockets_in_use.end(); ++iter ) {
|
||||
string_printf( str, "%d ", *iter );
|
||||
str.printf( "%d ", *iter );
|
||||
}
|
||||
if ( 0 < str.size() ) {
|
||||
logf( XW_LOGINFO, "Sockets in use: %s", str.c_str() );
|
||||
|
|
|
@ -54,7 +54,7 @@ UDPAckTrack::setOnAck( OnAckProc proc, uint32_t packetID, void* data )
|
|||
}
|
||||
|
||||
/* static */ void
|
||||
UDPAckTrack::printAcks( string& out )
|
||||
UDPAckTrack::printAcks( StrWPF& out )
|
||||
{
|
||||
get()->printAcksImpl( out );
|
||||
}
|
||||
|
@ -142,15 +142,15 @@ UDPAckTrack::setOnAckImpl( OnAckProc proc, uint32_t packetID, void* data )
|
|||
}
|
||||
|
||||
void
|
||||
UDPAckTrack::printAcksImpl( string& out )
|
||||
UDPAckTrack::printAcksImpl( StrWPF& out )
|
||||
{
|
||||
time_t now = time( NULL );
|
||||
time_t limit = ackLimit();
|
||||
MutexLock ml( &m_mutex );
|
||||
map<uint32_t, AckRecord>::const_iterator iter;
|
||||
for ( iter = m_pendings.begin(); m_pendings.end() != iter; ++iter ) {
|
||||
string_printf( out, "id: % 8d; stl: %04d\n", iter->first,
|
||||
(iter->second.m_createTime + limit) - now );
|
||||
out.printf( "id: % 8d; stl: %04d\n", iter->first,
|
||||
(iter->second.m_createTime + limit) - now );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,14 +212,14 @@ UDPAckTrack::threadProc()
|
|||
}
|
||||
}
|
||||
if ( 0 < older.size() ) {
|
||||
string leaked;
|
||||
StrWPF leaked;
|
||||
vector<uint32_t>::const_iterator iter = older.begin();
|
||||
for ( ; ; ) {
|
||||
string_printf( leaked, "%d", *iter );
|
||||
leaked.printf( "%d", *iter );
|
||||
if ( ++iter == older.end() ) {
|
||||
break;
|
||||
}
|
||||
string_printf( leaked, ", " );
|
||||
leaked.printf( ", " );
|
||||
}
|
||||
logf( XW_LOGERROR, "%s: these packets leaked (were not ack'd "
|
||||
"within %d seconds): %s", __func__,
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include "xwrelay_priv.h"
|
||||
#include "xwrelay.h"
|
||||
#include "strwpf.h"
|
||||
|
||||
typedef void (*OnAckProc)( bool acked, uint32_t packetID, void* data );
|
||||
|
||||
|
@ -42,7 +43,7 @@ class UDPAckTrack {
|
|||
static bool setOnAck( OnAckProc proc, uint32_t packetID, void* data );
|
||||
static bool shouldAck( XWRelayReg cmd );
|
||||
/* called from ctrl port */
|
||||
static void printAcks( string& out );
|
||||
static void printAcks( StrWPF& out );
|
||||
static void doNack( vector<uint32_t> ids );
|
||||
|
||||
private:
|
||||
|
@ -54,7 +55,7 @@ class UDPAckTrack {
|
|||
void recordAckImpl( uint32_t packetID );
|
||||
bool setOnAckImpl( OnAckProc proc, uint32_t packetID, void* data );
|
||||
void callProc( const map<uint32_t, AckRecord>::iterator iter, bool acked );
|
||||
void printAcksImpl( string& out );
|
||||
void printAcksImpl( StrWPF& out );
|
||||
void doNackImpl( vector<uint32_t>& ids );
|
||||
void* threadProc();
|
||||
|
||||
|
|
|
@ -1732,33 +1732,6 @@ read_udp_packet( int udpsock )
|
|||
}
|
||||
}
|
||||
|
||||
/* From stack overflow, toward a snprintf with an expanding buffer.
|
||||
*/
|
||||
string&
|
||||
string_printf( string& str, const char* fmt, ... )
|
||||
{
|
||||
const int origsiz = str.size();
|
||||
int addsiz = 100;
|
||||
va_list ap;
|
||||
for ( ; ; ) {
|
||||
str.resize( origsiz + addsiz );
|
||||
|
||||
va_start( ap, fmt );
|
||||
int len = vsnprintf( (char *)str.c_str() + origsiz, addsiz, fmt, ap );
|
||||
va_end( ap );
|
||||
|
||||
if ( len >= addsiz ) { // needs more space
|
||||
addsiz = len + 1;
|
||||
} else if ( -1 == len ) {
|
||||
assert(0); // should be impossible
|
||||
} else {
|
||||
str.resize( origsiz + len );
|
||||
break;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
// Going with non-blocking instead
|
||||
#if 0
|
||||
static void
|
||||
|
|
|
@ -63,8 +63,6 @@ int GetNSpawns(void);
|
|||
|
||||
int make_socket( unsigned long addr, unsigned short port );
|
||||
|
||||
std::string& string_printf( std::string& str, const char* fmt, ... );
|
||||
|
||||
int read_packet( int sock, uint8_t* buf, int buflen );
|
||||
|
||||
void onMsgAcked( bool acked, uint32_t packetID, void* data );
|
||||
|
|
Loading…
Reference in a new issue