Merged changes from 1.1 branch.

This commit is contained in:
Scott Ehlert 2008-12-05 15:57:49 -06:00
commit 7bdcea5814
9 changed files with 177 additions and 39 deletions

View File

@ -106,7 +106,7 @@
/** /**
* Sets the server to connect to for auotmatic gamedata updates. * Sets the server to connect to for auotmatic gamedata updates.
*/ */
"AutoUpdateServer" "hayate.alliedmods.net" "AutoUpdateServer" "smupdate.alliedmods.net"
/** /**
* Sets the port to connect to on the AutoUpdateServer server * Sets the port to connect to on the AutoUpdateServer server

View File

@ -802,11 +802,29 @@ static cell_t sm_ReadFileString(IPluginContext *pContext, const cell_t *params)
char *buffer; char *buffer;
pContext->LocalToString(params[2], &buffer); pContext->LocalToString(params[2], &buffer);
if (params[4] != -1)
{
if (size_t(params[4]) > size_t(params[3]))
{
return pContext->ThrowNativeError("read_count (%u) is greater than buffer size (%u)",
params[4],
params[3]);
}
num_read = (cell_t)fread(buffer, 1, params[4], pFile);
if (num_read != params[4] && ferror(pFile))
{
return -1;
}
return num_read;
}
char val; char val;
while (1) while (1)
{ {
/* If we're in stop mode, break as soon as the buffer is full. */ if (params[3] == 0 || num_read >= params[3] - 1)
if (params[4] && (params[3] == 0 || num_read >= params[3] - 1))
{ {
break; break;
} }

View File

@ -283,7 +283,7 @@
{ {
"GiveNamedItem" "GiveNamedItem"
{ {
"windows" "409" "windows" "406"
"linux" "410" "linux" "410"
} }
"RemovePlayerItem" "RemovePlayerItem"

View File

@ -176,11 +176,11 @@ native ReadFile(Handle:hndl, items[], num_items, size);
* @param hndl Handle to the file. * @param hndl Handle to the file.
* @param buffer Buffer to store the string. * @param buffer Buffer to store the string.
* @param max_size Maximum size of the string buffer. * @param max_size Maximum size of the string buffer.
* @param stop If true, reading will stop once max_size-1 bytes have * @param read_count If -1, reads until a null terminator is encountered in
* been read. If false, reading will stop once a NUL * the file. Otherwise, read_count bytes are read
* terminator is reached. The buffer will simply be * into the buffer provided. In this case the buffer
* terminated in either case, the difference is in how * is not explicitly null terminated, and the buffer
* the far the file position is changed. * will contain any null terminators read from the file.
* @return Number of characters written to the buffer, or -1 * @return Number of characters written to the buffer, or -1
* if an error was encountered. * if an error was encountered.
* @error Invalid Handle, or read_count > max_size. * @error Invalid Handle, or read_count > max_size.

View File

@ -58,7 +58,7 @@ stock FindTeamByName(const String:name[])
{ {
GetTeamName(i, team_name, sizeof(team_name)); GetTeamName(i, team_name, sizeof(team_name));
if (strncmp(team_name, name, name_len) == 0) if (strncmp(team_name, name, name_len, false) == 0)
{ {
if (found_team >= 0) if (found_team >= 0)
{ {

View File

@ -0,0 +1,62 @@
#include <sourcemod>
public Plugin:myinfo =
{
name = "File test",
author = "AlliedModders LLC",
description = "Tests file functions",
version = "1.0.0.0",
url = "http://www.sourcemod.net/"
};
public OnPluginStart()
{
RegServerCmd("test_fread1", Test_ReadBinStr);
}
Handle:OpenFile2(const String:path[], const String:mode[])
{
new Handle:file = OpenFile(path, mode);
if (file == INVALID_HANDLE)
PrintToServer("Failed to open file %s for %s", path, mode);
else
PrintToServer("Opened file handle %x: %s", file, path);
return file;
}
public Action:Test_ReadBinStr(args)
{
new items[] = {1, 3, 5, 7, 0, 92, 193, 26, 0, 84, 248, 2};
new Handle:of = OpenFile2("smbintest", "wb");
if (of == INVALID_HANDLE)
return Plugin_Handled;
WriteFile(of, items, sizeof(items), 1);
CloseHandle(of);
new Handle:inf = OpenFile2("smbintest", "rb");
new String:buffer[sizeof(items)];
ReadFileString(inf, buffer, sizeof(items), sizeof(items));
FileSeek(inf, 0, SEEK_SET);
new items2[sizeof(items)];
ReadFile(inf, items2, sizeof(items), 1);
CloseHandle(inf);
for (new i = 0; i < sizeof(items); i++)
{
if (buffer[i] != items[i])
{
PrintToServer("FAILED ON INDEX %d: %d != %d", i, buffer[i], items[i]);
return Plugin_Handled;
}
else if (items2[i] != items[i])
{
PrintToServer("FAILED ON INDEX %d: %d != %d", i, items2[i], items[i]);
return Plugin_Handled;
}
}
PrintToServer("Test passed!");
return Plugin_Handled;
}

View File

@ -18,7 +18,7 @@ C_GCC4_FLAGS = -fvisibility=hidden
CPP_GCC4_FLAGS = -fvisibility-inlines-hidden CPP_GCC4_FLAGS = -fvisibility-inlines-hidden
CPP = gcc-4.1 CPP = gcc-4.1
BINARY = daemon BINARY = smupdated
LINK += -lpthread -static-libgcc LINK += -lpthread -static-libgcc

View File

@ -1,3 +1,4 @@
#include <errno.h>
#include "smud.h" #include "smud.h"
#include "smud_threads.h" #include "smud_threads.h"
@ -29,7 +30,9 @@ int main(int argc, char **argv)
char filename[100]; char filename[100];
struct stat sbuf; struct stat sbuf;
printf("Loading Gamedata files into memory\n"); #if defined DEBUG
fprintf(stdout, "Loading Gamedata files into memory\n");
#endif
for (int i=0; i<NUM_FILES; i++) for (int i=0; i<NUM_FILES; i++)
{ {
@ -38,34 +41,44 @@ int main(int argc, char **argv)
if (!file) if (!file)
{ {
fprintf(stderr, "Could not find file: %s", filename);
return 1; return 1;
} }
if (stat(filename, &sbuf) == -1) if (stat(filename, &sbuf) == -1)
{ {
fprintf(stderr, "Could not stat file: %s (error: %s)", filename, strerror(errno));
return 1; return 1;
} }
if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_SHARED, file, 0)) == (caddr_t)(-1)) if ((fileLocations[i] = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, file, 0)) == MAP_FAILED)
{ {
fprintf(stderr, "Could not mmap file: %s (error: %s)", filename, strerror(errno));
return 1; return 1;
} }
fileLength[i] = sbuf.st_size; fileLength[i] = sbuf.st_size;
printf("Initialised file of %s of length %i\n", fileNames[i], fileLength[i]); #if defined DEBUG
fprintf(stdout, "Initialised file of %s of length %i\n", fileNames[i], fileLength[i]);
#endif
} }
printf("Initializing Thread Pool\n"); #if defined DEBUG
fprintf(stdout, "Initializing Thread Pool\n");
#endif
pool = new ThreadPool(); pool = new ThreadPool();
if (!pool->Start()) if (!pool->Start())
{ {
fprintf(stderr, "Could not initialize thread pool!\n");
return 1; return 1;
} }
#if defined DEBUG
printf("Create Server Socket\n"); printf("Create Server Socket\n");
#endif
memset(&serverAddress, 0, sizeof(serverAddress)); memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET; serverAddress.sin_family = AF_INET;
@ -76,6 +89,7 @@ int main(int argc, char **argv)
if (pProtocol == NULL) if (pProtocol == NULL)
{ {
fprintf(stderr, "Could not get tcp proto: %s", strerror(errno));
return 1; return 1;
} }
@ -83,6 +97,7 @@ int main(int argc, char **argv)
if (serverSocket < 0) if (serverSocket < 0)
{ {
fprintf(stderr, "Could not open socket: %s", strerror(errno));
return 1; return 1;
} }
@ -91,34 +106,45 @@ int main(int argc, char **argv)
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0)
{ {
fprintf(stderr, "Could not bind socket: %s", strerror(errno));
return 1; return 1;
} }
if (listen(serverSocket, LISTEN_QUEUE_LENGTH) < 0) if (listen(serverSocket, LISTEN_QUEUE_LENGTH) < 0)
{ {
fprintf(stderr, "Could not listen on socket: %s", strerror(errno));
return 1; return 1;
} }
printf("Entering Main Loop\n"); fprintf(stdout, "Server has started.\n");
while (1) while (1)
{ {
addressLen = sizeof(clientAddress); addressLen = sizeof(clientAddress);
if ( (clientSocket = accept(serverSocket, (struct sockaddr *)&clientAddress, (socklen_t *)&addressLen)) < 0) clientSocket = accept(serverSocket,
(struct sockaddr *)&clientAddress,
(socklen_t *)&addressLen);
if (clientSocket < 0)
{ {
fprintf(stderr, "Could not accept client: %s", strerror(errno));
continue; continue;
} }
opts = fcntl(clientSocket, F_GETFL, 0); opts = fcntl(clientSocket, F_GETFL, 0);
if (fcntl(clientSocket, F_SETFL, opts|O_NONBLOCK) < 0) if (fcntl(clientSocket, F_SETFL, opts|O_NONBLOCK) < 0)
{ {
fprintf(stderr, "Could not non-block client: %s", strerror(errno));
closesocket(clientSocket); closesocket(clientSocket);
continue; continue;
} }
#if defined DEBUG
printf("Connection Received!\n"); fprintf(stdout,
"Accepting connection from client (sock %d, ip %s)",
clientSocket,
inet_ntoa(&clientAddress));
#endif
pool->AddConnection(clientSocket); pool->AddConnection(clientSocket);
} }

View File

@ -1,3 +1,4 @@
#include <assert.h>
#include "smud_connections.h" #include "smud_connections.h"
#include "smud.h" #include "smud.h"
@ -19,8 +20,6 @@ void ConnectionPool::AddConnection( int fd )
pthread_mutex_lock(&m_AddLock); pthread_mutex_lock(&m_AddLock);
m_AddQueue.push_back(connection); m_AddQueue.push_back(connection);
pthread_mutex_unlock(&m_AddLock); pthread_mutex_unlock(&m_AddLock);
printf("New Connection Added\n");
} }
void ConnectionPool::Process( bool *terminate ) void ConnectionPool::Process( bool *terminate )
@ -48,9 +47,10 @@ void ConnectionPool::Process( bool *terminate )
/* Add all connections that want processing to the sets */ /* Add all connections that want processing to the sets */
while (iter != m_Links.end()) while (iter != m_Links.end())
{ {
con = (smud_connection *)*iter; con = *iter;
pollReturn = poll(&(con->pollData), 1, 0); pollReturn = poll(&(con->pollData), 1, 0);
assert(pollReturn <= 1);
if (pollReturn == -1) if (pollReturn == -1)
{ {
@ -66,11 +66,11 @@ void ConnectionPool::Process( bool *terminate )
if (result == QueryResult_Complete) if (result == QueryResult_Complete)
{ {
iter = m_Links.erase(iter); iter = m_Links.erase(iter);
#if defined DEBUG
fprintf(stdout, "Closing socket %d\n", con->fd);
#endif
closesocket(con->fd); closesocket(con->fd);
delete con; delete con;
printf("Connection Completed!\n");
continue; continue;
} }
@ -169,7 +169,9 @@ void ConnectionPool::ReadQueryHeader( smud_connection *con )
con->sentSums = data[10]; con->sentSums = data[10];
con->state = ConnectionState_ReadQueryData; con->state = ConnectionState_ReadQueryData;
printf("Query Header Read Complete, %i md5's expected\n", con->sentSums); #if defined DEBUG
fprintf(stdout, "Query Header Read Complete, %i md5's expected\n", con->sentSums);
#endif
} }
void ConnectionPool::ReplyQuery(smud_connection *con) void ConnectionPool::ReplyQuery(smud_connection *con)
@ -200,7 +202,9 @@ void ConnectionPool::ReplyQuery(smud_connection *con)
//Alternatively we could just send all at once here. Could make for a damn big query. 100k anyone? //Alternatively we could just send all at once here. Could make for a damn big query. 100k anyone?
con->state = ConnectionState_SendingFiles; con->state = ConnectionState_SendingFiles;
#if defined DEBUG
printf("Query Reply Header Complete\n"); printf("Query Reply Header Complete\n");
#endif
} }
void ConnectionPool::ReadQueryContent( smud_connection *con ) void ConnectionPool::ReadQueryContent( smud_connection *con )
@ -229,7 +233,9 @@ void ConnectionPool::ReadQueryContent( smud_connection *con )
if (con->shouldSend[i] == MD5Status_NeedsUpdate) if (con->shouldSend[i] == MD5Status_NeedsUpdate)
{ {
printf("File %i needs updating\n", i); #if defined DEBUG
fprintf(stdout, "File %i needs updating\n", i);
#endif
con->sendCount++; con->sendCount++;
con->headerSent[i] = false; con->headerSent[i] = false;
continue; continue;
@ -237,7 +243,9 @@ void ConnectionPool::ReadQueryContent( smud_connection *con )
if (con->shouldSend[i] == MD5Status_Unknown) if (con->shouldSend[i] == MD5Status_Unknown)
{ {
printf("File %i is unknown\n", i); #if defined DEBUG
fprintf(stdout, "File %i is unknown\n", i);
#endif
con->unknownCount++; con->unknownCount++;
} }
} }
@ -245,7 +253,9 @@ void ConnectionPool::ReadQueryContent( smud_connection *con )
con->state = ConnectionState_ReplyQuery; con->state = ConnectionState_ReplyQuery;
con->pollData.events = POLLOUT; con->pollData.events = POLLOUT;
delete [] data; delete [] data;
printf("Query Data Read Complete\n"); #if defined DEBUG
fprintf(stdout, "Query Data Read Complete\n");
#endif
} }
MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection *con, int fileNum) MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection *con, int fileNum)
@ -263,7 +273,9 @@ MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection
strcat(path, md5String); strcat(path, md5String);
printf("checking for file \"%s\"\n", path); #if defined DEBUG
fprintf(stdout, "checking for file \"%s\"\n", path);
#endif
FILE *file = fopen(path, "r"); FILE *file = fopen(path, "r");
@ -275,7 +287,9 @@ MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection
char latestMD5[33]; char latestMD5[33];
fgets(latestMD5, 33, file); fgets(latestMD5, 33, file);
printf("Latest md5 is: %s\n", latestMD5); #if defined DEBUG
fprintf(stdout, "Latest md5 is: %s\n", latestMD5);
#endif
if (strcmp(latestMD5, md5String) == 0) if (strcmp(latestMD5, md5String) == 0)
{ {
@ -298,7 +312,9 @@ MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection
fclose(file); fclose(file);
printf("Filename is %s\n", filename); #if defined DEBUG
fprintf(stdout, "Filename is %s\n", filename);
#endif
//We now need to match this filename with one of our mmap'd files in memory and store it until send gets called. //We now need to match this filename with one of our mmap'd files in memory and store it until send gets called.
for (int i=0; i<NUM_FILES; i++) for (int i=0; i<NUM_FILES; i++)
@ -306,7 +322,9 @@ MD5Status ConnectionPool::GetMD5UpdateStatus( const char *md5 , smud_connection
if (strcmp(fileNames[i], filename) == 0) if (strcmp(fileNames[i], filename) == 0)
{ {
con->fileLocation[fileNum] = i; con->fileLocation[fileNum] = i;
printf("File %i mapped to local file %i\n", fileNum, i); #if defined DEBUG
fprintf(stdout, "File %i mapped to local file %i\n", fileNum, i);
#endif
return MD5Status_NeedsUpdate; return MD5Status_NeedsUpdate;
} }
} }
@ -326,7 +344,9 @@ void ConnectionPool::SendFile( smud_connection *con )
//All files have been sent. //All files have been sent.
if (con->currentFile >= con->sentSums) if (con->currentFile >= con->sentSums)
{ {
printf("All files sent!\n"); #if defined DEBUG
fprintf(stdout, "All files sent!\n");
#endif
con->state = ConnectionState_SendUnknownList; con->state = ConnectionState_SendUnknownList;
return; return;
} }
@ -334,8 +354,13 @@ void ConnectionPool::SendFile( smud_connection *con )
void *file = fileLocations[con->fileLocation[con->currentFile]]; void *file = fileLocations[con->fileLocation[con->currentFile]];
int filelength = fileLength[con->fileLocation[con->currentFile]]; int filelength = fileLength[con->fileLocation[con->currentFile]];
printf("Sending file of length %i\n", filelength); #if defined DEBUG
printf("Current file index is: %i, maps to file index: %i\n", con->currentFile, con->fileLocation[con->currentFile]); fprintf(stdout, "Sending file of length %i\n", filelength);
fprintf(stdout,
"Current file index is: %i, maps to file index: %i\n",
con->currentFile,
con->fileLocation[con->currentFile]);
#endif
if (!con->headerSent[con->currentFile]) if (!con->headerSent[con->currentFile])
{ {
@ -367,7 +392,9 @@ void ConnectionPool::SendFile( smud_connection *con )
} }
con->currentFile++; con->currentFile++;
printf("Sent a file!: %s\n", fileNames[con->fileLocation[con->currentFile-1]]); #if defined DEBUG
fprintf(stdout, "Sent a file!: %s\n", fileNames[con->fileLocation[con->currentFile-1]]);
#endif
} }
void ConnectionPool::SendUnknownList( smud_connection *con ) void ConnectionPool::SendUnknownList( smud_connection *con )
@ -377,7 +404,9 @@ void ConnectionPool::SendUnknownList( smud_connection *con )
packet[0] = con->unknownCount; packet[0] = con->unknownCount;
printf("%i Files are unknown\n", con->unknownCount); #if defined DEBUG
fprintf(stdout, "%i Files are unknown\n", con->unknownCount);
#endif
int i=1; int i=1;
@ -401,5 +430,8 @@ void ConnectionPool::SendUnknownList( smud_connection *con )
} }
con->state = ConnectionState_Complete; con->state = ConnectionState_Complete;
printf("Unknown's Sent\n"); #if defined DEBUG
fprintf(stdout, "Unknowns Sent\n");
#endif
} }