📄 updatemanager.cpp
字号:
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->GetPrefBoolean(kUseProxyPref, &useProxy);
length = sizeof(proxyname);
m_context->prefs->GetPrefString(kProxyHostPref, 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\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
"User-Agent: FreeAmp/%s\r\n"
"\r\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);
if(!buffer)
{
result = kError_OutOfMemory;
break;
}
}
count = recv(s, buffer + total, bufferSize - total - 1, 0);
if(count > 0)
total += count;
else
{
result = kError_IOError;
}
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;
}
}while(!IsHTTPHeaderComplete(buffer, total) && IsntError(result));
}
// parse header
if(IsntError(result))
{
uint32 returnCode = atoi(buffer+9);
buffer[total] = 0x00;
//cout << buffer << endl;
//cout << returnCode << endl;
switch(buffer[9])
{
// 1xx: Informational - Request received, continuing process
case '1':
{
// not sure what to do here... continue receiving???
}
// 2xx: Success - The action was successfully received,
// understood, and accepted
case '2':
{
result = kError_UnknownErr;
//cout << destPath << endl;
int32 fileSize = GetContentLengthFromHeader(buffer);
int openFlags = O_BINARY|O_CREAT|O_RDWR|O_TRUNC;
int fd = open(destPath, openFlags, S_IREAD | S_IWRITE);
if(fd >= 0)
{
result = kError_NoErr;
char* cp = strstr(buffer, "\n\n");
if(cp)
cp += 2;
else
{
cp = strstr(buffer, "\r\n\r\n");
if(cp)
cp += 4;
}
if(cp)
{
if(cp - buffer < (int)total)
{
write(fd, cp, total - (cp - buffer));
total -= (cp - buffer);
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Progress;
event.data.progressData.item = item;
event.data.progressData.position = total;
event.data.progressData.total = fileSize;
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
}
}
do
{
count = recv(s, buffer, bufferSize, 0);
if(count > 0)
{
write(fd, buffer, count);
total += count;
}
if(count < 0)
result = kError_IOError;
if(function)
{
UMEvent event;
memset(&event, 0x00, sizeof(UMEvent));
event.type = kUMEvent_Progress;
event.data.progressData.item = item;
event.data.progressData.position = total;
event.data.progressData.total = fileSize;
bool ok = function(&event, cookie);
if(!ok)
result = kError_UserCancel;
}
//cout << "bytes recvd:" << count << endl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -