updatemanager.cpp
字号:
result = kError_CantFindHost;
else
{
hostByIP.h_length = sizeof(uint32);
hostByIP.h_addrtype = AF_INET;
hostByIP.h_addr_list = (char**)&addr_ptr;
hostByName = &hostByIP;
}
}
if(IsntError(result))
{
memcpy(&host, hostByName, sizeof(struct hostent));
}
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Found server address...";
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
// open socket
if(IsntError(result))
{
memset(&addr, 0x00, sizeof(struct sockaddr_in));
memcpy(&addr.sin_addr, host.h_addr, host.h_length);
addr.sin_family= host.h_addrtype;
addr.sin_port= htons(port);
s = socket(host.h_addrtype, SOCK_STREAM, 0);
if(s < 0)
result = kError_CantCreateSocket;
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Connecting to the server...";
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
// connect and send request
if(IsntError(result))
{
if(connect(s,(const struct sockaddr*)&addr, sizeof(struct sockaddr)))
result = kError_CannotBind;
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Requesting latest version info...";
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
if(IsntError(result))
{
gethostname(localname, kMaxHostNameLen);
char* query = new char[ strlen(kUpdateRequest) +
strlen(file) +
strlen(localname) +
strlen(FREEAMP_VERSION) + 1];
sprintf(query, kUpdateRequest, file, localname, FREEAMP_VERSION);
int count;
count = send(s, query, strlen(query), 0);
if(count != (int)strlen(query))
{
result = kError_IOError;
}
delete [] query;
}
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Receiving latest version info...";
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
// receive response
if(IsntError(result))
{
uint32 bufferSize = 2048;
char* buffer = NULL;
int count;
uint32 total = 0;
buffer = (char*)malloc(bufferSize + 1);
result = kError_OutOfMemory;
if(buffer)
{
result = kError_NoErr;
do
{
count = recv(s, buffer, bufferSize, 0);
if(count > 0)
{
buffer[count] = 0x00;
info += buffer;
total += count;
}
if(count < 0)
result = kError_IOError;
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Receiving latest version info...";
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
}while(count > 0 && IsntError(result));
free(buffer);
}
}
if(s > 0)
closesocket(s);
return result;
}
static bool IsHTTPHeaderComplete(char* buffer, uint32 length)
{
bool result = false;
//if(length >= 4)
//{
//if( (buffer[0] == 'H' && buffer[1] == 'T'
// && buffer[2] == 'T' && buffer[3] == 'P'))
//{
//cout << "buffer is HTTP" << endl;
for(char* cp = buffer; cp < buffer + length; cp++)
{
if(!strncmp(cp, "\n\n", 2) || !strncmp(cp, "\r\n\r\n", 4))
{
result = true;
break;
}
}
//}
//}
return result;
}
static int32 GetContentLengthFromHeader(const char* buffer)
{
int32 result = -1;
char* cp = strstr(buffer, "Content-Length:");
if(cp)
{
cp += strlen("Content-Length:") + 1;
result = atoi(cp);
}
return result;
}
Error UpdateManager::DownloadItem(UpdateItem* item,
UMCallBackFunction function,
void* cookie)
{
Error result = kError_InvalidParam;
assert(item);
if(item)
{
cout << "Downloading " << item->GetLocalFileName() << endl;
char hostname[kMaxHostNameLen + 1];
char localname[kMaxHostNameLen + 1];
char proxyname[kMaxHostNameLen + 1];
unsigned short port;
struct sockaddr_in addr;
struct hostent host;
SOCKET s = -1;
char* file = NULL;
char* destPath = NULL;
char* srcPath = NULL;
bool useProxy;
// make sure there is a place to put it on this machine
destPath = new char[_MAX_PATH];
srcPath = new char[_MAX_PATH];
uint32 length = _MAX_PATH;
m_context->prefs->GetPrefString(kInstallDirPref, destPath, &length);
strcat(destPath, "\\update");
// move past platform dir
char* cp = strchr(item->GetCurrentFileLocation().c_str(), '/');
// move past arch dir
if(cp)
cp = strchr(cp + 1, '/');
if(cp)
{
strcat(destPath, "\\");
strcat(destPath, cp + 1);
}
// make sure all the directory separators are correct
for (int32 index = strlen(destPath) - 1; index >=0; index--)
{
if(destPath[index] == '\\' && DIR_MARKER == '/')
destPath[index] = DIR_MARKER;
else if(destPath[index] == '/' && DIR_MARKER == '\\')
destPath[index] = DIR_MARKER;
}
cp = strrchr(destPath, DIR_MARKER);
if(cp)
*cp = 0x00;
CreateDirectoryPath(destPath);
if(cp)
*cp = DIR_MARKER;
result = kError_ProtocolNotSupported;
// where should we connect to?
if(!strncasecmp(item->GetCurrentFileURL().c_str(), "http://", 7))
{
int32 numFields;
uint32 length;
result = kError_NoErr;
m_context->prefs->GetUseProxyServer(&useProxy);
length = sizeof(proxyname);
m_context->prefs->GetProxyServerAddress(proxyname, &length);
if(useProxy)
{
numFields = sscanf(proxyname,
"http://%[^:/]:%hu", hostname, &port);
strcpy(proxyname, item->GetCurrentFileURL().c_str());
file = proxyname;
}
else
{
numFields = sscanf(item->GetCurrentFileURL().c_str(),
"http://%[^:/]:%hu", hostname, &port);
file = strchr(item->GetCurrentFileURL().c_str() + 7, '/');
}
if(numFields < 1)
{
result = kError_InvalidURL;
}
if(numFields < 2)
{
port = kHttpPort;
}
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Downloading ";
event.eventString += item->GetLocalFileName();
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
// get hostname
if(IsntError(result))
{
struct hostent* hostByName;
struct hostent hostByIP;
hostByName = gethostbyname(hostname);
// On some stacks a numeric IP address
// will not parse with gethostbyname.
// If that didn't work try to convert it as a
// numeric address before giving up.
if(!hostByName)
{
static unsigned long ip;
static char *addr_ptr[2] = {(char*)&ip, NULL};
if((ip = inet_addr(hostname)) < 0)
result = kError_CantFindHost;
else
{
hostByIP.h_length = sizeof(uint32);
hostByIP.h_addrtype = AF_INET;
hostByIP.h_addr_list = (char**)&addr_ptr;
hostByName = &hostByIP;
}
}
if(IsntError(result))
{
memcpy(&host, hostByName, sizeof(struct hostent));
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Downloading ";
event.eventString += item->GetLocalFileName();
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
}
// open socket
if(IsntError(result))
{
memset(&addr, 0x00, sizeof(struct sockaddr_in));
memcpy(&addr.sin_addr, host.h_addr, host.h_length);
addr.sin_family= host.h_addrtype;
addr.sin_port= htons(port);
s = socket(host.h_addrtype, SOCK_STREAM, 0);
if(s < 0)
result = kError_CantCreateSocket;
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Downloading ";
event.eventString += item->GetLocalFileName();
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
}
// connect and send request
if(IsntError(result))
{
if(connect(s,(const struct sockaddr*)&addr, sizeof(struct sockaddr)))
result = kError_CannotBind;
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Downloading ";
event.eventString += item->GetLocalFileName();
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
if(IsntError(result))
{
gethostname(localname, kMaxHostNameLen);
const char* kHTTPQuery = "GET %s HTTP/1.0\n"
"Host: %s\n"
"Accept: */*\n"
"User-Agent: FreeAmp/%s\n"
"\n";
char* query = new char[ strlen(kHTTPQuery) +
strlen(file) +
strlen(localname) +
strlen(FREEAMP_VERSION) + 1];
sprintf(query, kHTTPQuery, file, localname, FREEAMP_VERSION);
int count;
count = send(s, query, strlen(query), 0);
if(count != (int)strlen(query))
{
result = kError_IOError;
}
delete [] query;
}
}
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Status;
event.eventString = "Downloading ";
event.eventString += item->GetLocalFileName();
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
// receive response
if(IsntError(result))
{
uint32 bufferSize = 2048;
char* buffer = NULL;
int count;
uint32 total = 0;
buffer = (char*)malloc(bufferSize);
result = kError_OutOfMemory;
if(buffer)
{
result = kError_NoErr;
do
{
if(total >= bufferSize - 1)
{
bufferSize *= 2;
buffer = (char*) realloc(buffer, bufferSize);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -