1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Network: Remove code for compatibility with unsupported platforms

This commit is contained in:
reuk 2024-06-12 19:30:35 +01:00
parent 225f1526ee
commit ae75e27948
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -162,246 +162,6 @@ private:
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (URLConnectionStateBase)
};
#if JUCE_MAC
// This version is only used for backwards-compatibility with older OSX targets,
// so we'll turn off deprecation warnings. This code will be removed at some point
// in the future.
JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated")
class URLConnectionStatePreYosemite final : public URLConnectionStateBase
{
public:
URLConnectionStatePreYosemite (NSURLRequest* req, const int maxRedirects)
: URLConnectionStateBase (req, maxRedirects)
{
static DelegateClass cls;
delegate = [cls.createInstance() init];
DelegateClass::setState (delegate, this);
}
~URLConnectionStatePreYosemite() override
{
stop();
[connection release];
[request release];
[headers release];
[delegate release];
[data release];
}
bool start (WebInputStream& inputStream, WebInputStream::Listener* listener) override
{
startThread();
while (isThreadRunning() && ! initialised)
{
if (listener != nullptr)
if (! listener->postDataSendProgress (inputStream, (int) latestTotalBytes, (int) [[request HTTPBody] length]))
return false;
Thread::sleep (1);
}
return connection != nil && ! hasFailed;
}
void stop()
{
{
const ScopedLock dLock (dataLock);
const ScopedLock connectionLock (createConnectionLock);
hasBeenCancelled = true;
if (connection != nil)
[connection cancel];
}
stopThread (10000);
}
void cancel() override
{
hasFinished = hasFailed = true;
stop();
}
int read (char* dest, int numBytes) override
{
int numDone = 0;
while (numBytes > 0)
{
const ScopedLock sl (dataLock);
auto available = jmin (numBytes, (int) [data length]);
if (available > 0)
{
[data getBytes: dest length: (NSUInteger) available];
[data replaceBytesInRange: NSMakeRange (0, (NSUInteger) available) withBytes: nil length: 0];
numDone += available;
numBytes -= available;
dest += available;
}
else
{
if (hasFailed || hasFinished)
break;
const ScopedUnlock sul (dataLock);
Thread::sleep (1);
}
}
return numDone;
}
void didReceiveResponse (NSURLResponse* response)
{
{
const ScopedLock sl (dataLock);
[data setLength: 0];
}
contentLength = [response expectedContentLength];
[headers release];
headers = nil;
if ([response isKindOfClass: [NSHTTPURLResponse class]])
{
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*) response;
headers = [[httpResponse allHeaderFields] retain];
statusCode = (int) [httpResponse statusCode];
}
initialised = true;
}
NSURLRequest* willSendRequest (NSURLRequest* newRequest, NSURLResponse* redirectResponse)
{
if (redirectResponse != nullptr)
{
if (numRedirects >= numRedirectsToFollow)
return nil; // Cancel redirect and allow connection to continue
++numRedirects;
}
return newRequest;
}
void didFailWithError ([[maybe_unused]] NSError* error)
{
DBG (nsStringToJuce ([error description]));
nsUrlErrorCode = [error code];
hasFailed = true;
initialised = true;
signalThreadShouldExit();
}
void didReceiveData (NSData* newData)
{
const ScopedLock sl (dataLock);
[data appendData: newData];
initialised = true;
}
void didSendBodyData (NSInteger totalBytesWritten, NSInteger /*totalBytesExpected*/)
{
latestTotalBytes = static_cast<int> (totalBytesWritten);
}
void finishedLoading()
{
hasFinished = true;
initialised = true;
signalThreadShouldExit();
}
void run() override
{
{
const ScopedLock lock (createConnectionLock);
if (hasBeenCancelled)
return;
connection = [[NSURLConnection alloc] initWithRequest: request
delegate: delegate];
}
while (! threadShouldExit())
{
JUCE_AUTORELEASEPOOL
{
[[NSRunLoop currentRunLoop] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.01]];
}
}
}
private:
//==============================================================================
struct DelegateClass final : public ObjCClass<NSObject>
{
DelegateClass() : ObjCClass<NSObject> ("JUCENetworkDelegate_")
{
addIvar<URLConnectionStatePreYosemite*> ("state");
addMethod (@selector (connection:didReceiveResponse:), didReceiveResponse);
addMethod (@selector (connection:didFailWithError:), didFailWithError);
addMethod (@selector (connection:didReceiveData:), didReceiveData);
addMethod (@selector (connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:),
connectionDidSendBodyData);
addMethod (@selector (connectionDidFinishLoading:), connectionDidFinishLoading);
addMethod (@selector (connection:willSendRequest:redirectResponse:), willSendRequest);
registerClass();
}
static void setState (id self, URLConnectionStatePreYosemite* state) { object_setInstanceVariable (self, "state", state); }
static URLConnectionStatePreYosemite* getState (id self) { return getIvar<URLConnectionStatePreYosemite*> (self, "state"); }
private:
static void didReceiveResponse (id self, SEL, NSURLConnection*, NSURLResponse* response)
{
getState (self)->didReceiveResponse (response);
}
static void didFailWithError (id self, SEL, NSURLConnection*, NSError* error)
{
getState (self)->didFailWithError (error);
}
static void didReceiveData (id self, SEL, NSURLConnection*, NSData* newData)
{
getState (self)->didReceiveData (newData);
}
static NSURLRequest* willSendRequest (id self, SEL, NSURLConnection*, NSURLRequest* request, NSURLResponse* response)
{
return getState (self)->willSendRequest (request, response);
}
static void connectionDidSendBodyData (id self, SEL, NSURLConnection*, NSInteger, NSInteger totalBytesWritten, NSInteger totalBytesExpected)
{
getState (self)->didSendBodyData (totalBytesWritten, totalBytesExpected);
}
static void connectionDidFinishLoading (id self, SEL, NSURLConnection*)
{
getState (self)->finishedLoading();
}
};
NSURLConnection* connection = nil;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (URLConnectionStatePreYosemite)
};
JUCE_END_IGNORE_WARNINGS_GCC_LIKE
#endif
//==============================================================================
class API_AVAILABLE (macos (10.9)) URLConnectionState final : public URLConnectionStateBase
{
@ -982,17 +742,7 @@ public:
if (! connection->start (owner, webInputListener))
{
const auto errorCode = connection->getErrorCode();
connection.reset();
if (@available (macOS 10.10, *))
return false;
// Workaround for macOS versions below 10.10 where HTTPS POST requests with keep-alive
// fail with the NSURLErrorNetworkConnectionLost error code.
if (numRetries == 0 && errorCode == NSURLErrorNetworkConnectionLost)
return connect (webInputListener, ++numRetries);
return false;
}
@ -1157,12 +907,7 @@ private:
// Workaround for an Apple bug. See https://github.com/AFNetworking/AFNetworking/issues/2334
[req HTTPBody];
if (@available (macOS 10.10, *))
connection = std::make_unique<URLConnectionState> (req, numRedirectsToFollow);
#if JUCE_MAC
else
connection = std::make_unique<URLConnectionStatePreYosemite> (req, numRedirectsToFollow);
#endif
connection = std::make_unique<URLConnectionState> (req, numRedirectsToFollow);
}
}
}