status_t CDDBServer::_SendCommand(const BString& command, BString& output) { if (!fConnected) return B_ERROR; // Assemble full command string. BString fullCommand; fullCommand << command << "&hello=" << fLocalUserName << " " << fLocalHostName << " cddb_lookup 1.0&proto=6"; // Replace spaces by + signs. fullCommand.ReplaceAll(" ", "+"); // And now add command header and footer. fullCommand.Prepend("GET /~cddb/cddb.cgi?cmd="); fullCommand << " HTTP 1.0\n\n"; int32 result = fConnection.Send((void*)fullCommand.String(), fullCommand.Length()); if (result == fullCommand.Length()) { BNetBuffer netBuffer; while (fConnection.Receive(netBuffer, 1024) != 0) { // Do nothing. Data is automatically appended to the NetBuffer. } // AppendString automatically adds the terminating \0. netBuffer.AppendString(""); output.SetTo((char*)netBuffer.Data(), netBuffer.Size()); return B_OK; } return B_ERROR; }
int32 WatchNetEndpoint::Receive(BNetBuffer &buffer, size_t size, int flags = 0) { int32 returnVal = BNetEndpoint::Receive(buffer, size, flags); if(returnVal > 0) { BString recStr; if (buffer.Size() > 0) { recStr.SetTo((const char*)buffer.Data(), buffer.Size()); } BMessage recMsg('ADMS'); recMsg.AddInt8("Type", DATA_RECEIVED_EVENT); recMsg.AddString("Text", recStr); watchWin->PostMessage(&recMsg); } return returnVal; }
int32 BNetEndpoint::SendTo(BNetBuffer& buffer, const BNetAddress& address, int flags) { return SendTo(buffer.Data(), buffer.Size(), address, flags); }
int32 BNetEndpoint::Send(BNetBuffer& buffer, int flags) { return Send(buffer.Data(), buffer.Size(), flags); }
int32 WatchNetEndpoint::Send(BNetBuffer &pack, int flags) { return Send(pack.Data(),pack.Size(),flags); }
int32 DownloadLoop(void *pDummy) { char *pDetails, *pFile, *pIP, *pPort, *pUser , *pTemp, *pLength, *pLeaf, *pFilenameEnd; uint32 iIPAddress, iFileLength, iDataLength; int32 iPort, iBytesReceived; status_t stReturnCode; BNetEndpoint bneSong; BNetAddress bnaSong; BNetBuffer bnbSong; BFile *bfSong; Preferences *myPrefs; DownloadView *dlView; DownloadWindow *myDownloadWindow; myPrefs = (Preferences *)pDummy; pDetails = myPrefs->pDownloadDetails; pUser = pDetails; pTemp = strchr(pDetails, ' '); *pTemp = '\0'; pTemp++; pIP = pTemp; pTemp = strchr(pTemp, ' '); *pTemp = '\0'; iIPAddress = strtoul(pIP, NULL, 10); pTemp++; pPort = pTemp; pTemp = strchr(pTemp, ' '); *pTemp = '\0'; iPort = atol(pPort); pTemp++; pTemp = strchr(pTemp, '"'); pFile = pTemp; pTemp++; pTemp = strchr(pTemp, '"') + 1; *pTemp = '\0'; pFilenameEnd = pTemp - 1; bnbSong.AppendData(myPrefs->GetUser(), strlen(myPrefs->GetUser())); bnbSong.AppendData(" ", 1); bnbSong.AppendData(pFile, strlen(pFile)); bnbSong.AppendData(" ", 1); bnbSong.AppendData("0", 1); for(pLeaf = pTemp; *pLeaf != '\\' && *pLeaf != '/'; pLeaf-- ); pLeaf++; *pFilenameEnd = '\0'; BPath Path(myPrefs->GetDownloadPath(), NULL, true); if(Path.InitCheck() == B_OK) { // Making sure we have a valid path. Path.Append(pLeaf); bfSong = new BFile(Path.Path(), B_WRITE_ONLY|B_CREATE_FILE|B_OPEN_AT_END); } else // No valid path, use default (current directory) bfSong = new BFile(pLeaf, B_WRITE_ONLY|B_CREATE_FILE|B_OPEN_AT_END); if(bfSong->InitCheck() == B_OK) { stReturnCode = bnaSong.SetTo(iIPAddress, iPort); stReturnCode = bneSong.Connect(bnaSong); // now we need to recieve the mysterious '1'; char *pReceiveBuffer = (char *)malloc(4096); iBytesReceived = bneSong.Receive(pReceiveBuffer, 1); if(iBytesReceived == 1) { pLength = (char *)malloc(50); // more than enough to hold a big number :) printf("sending get command\n"); bneSong.Send("GET",3); pTemp = (char *)bnbSong.Data(); bneSong.Send(pTemp, strlen(pTemp)); pTemp = pLength - 1; // the other client now sends a length followed by the mp3 // according to the unoffical protocol you can not tell how long // the length is. but the mp3 starts // with 0xFF while(isdigit((unsigned char)*pReceiveBuffer) != 0) { pTemp++; iBytesReceived = bneSong.Receive(pReceiveBuffer, 1); *pTemp = *pReceiveBuffer; } *pTemp = '\0'; iFileLength = strtoul(pLength, NULL, 10); //create the download window now we know all the details myDownloadWindow = new DownloadWindow(BRect(100,100, 400, 200), "BeNapster Download", B_DOCUMENT_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, B_NOT_RESIZABLE, B_CURRENT_WORKSPACE); myDownloadWindow->Show(); myDownloadWindow->Lock(); dlView = myDownloadWindow->AddTransfer(pFile, iFileLength); myDownloadWindow->Unlock(); // The 0xFF is part of the mp3 so we need to write it out bfSong->Write(pReceiveBuffer, iBytesReceived); iDataLength = iBytesReceived; myDownloadWindow->Lock(); dlView->AddBytesReceived(iBytesReceived); myDownloadWindow->Unlock(); iBytesReceived = bneSong.Receive(pReceiveBuffer, 4096); bfSong->Write(pReceiveBuffer, iBytesReceived); iDataLength += iBytesReceived; myDownloadWindow->Lock(); dlView->AddBytesReceived(iBytesReceived); myDownloadWindow->Unlock(); while(iDataLength != iFileLength) { iBytesReceived = bneSong.Receive(pReceiveBuffer, 4096); if (iBytesReceived < 0) { break; } bfSong->Write(pReceiveBuffer, iBytesReceived); iDataLength += iBytesReceived; myDownloadWindow->Lock(); dlView->AddBytesReceived(iBytesReceived); myDownloadWindow->Unlock(); } bfSong->Unset(); delete (bfSong); myDownloadWindow->PostMessage(B_QUIT_REQUESTED); } } return(0); }