326 lines
12 KiB
Java
326 lines
12 KiB
Java
package com.discord.utilities.websocket;
|
|
|
|
import androidx.core.view.PointerIconCompat;
|
|
import d0.z.d.m;
|
|
import f0.e0.k.h;
|
|
import f0.x;
|
|
import j0.p.a;
|
|
import java.io.InputStreamReader;
|
|
import java.util.Map;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
import kotlin.Unit;
|
|
import kotlin.jvm.functions.Function0;
|
|
import kotlin.jvm.functions.Function1;
|
|
import kotlin.jvm.functions.Function2;
|
|
import kotlin.jvm.functions.Function3;
|
|
import kotlin.jvm.internal.DefaultConstructorMarker;
|
|
import okhttp3.Request;
|
|
import okhttp3.Response;
|
|
import okhttp3.WebSocketListener;
|
|
import rx.Observable;
|
|
import rx.Scheduler;
|
|
import rx.functions.Action1;
|
|
import rx.subjects.PublishSubject;
|
|
/* compiled from: WebSocket.kt */
|
|
public final class WebSocket {
|
|
private static final int CLOSE_NORMAL = 1000;
|
|
public static final Companion Companion = new Companion(null);
|
|
private okhttp3.WebSocket client;
|
|
private final Function3<String, Exception, Map<String, String>, Unit> errorLogger;
|
|
private Function1<? super Closed, Unit> onClosed = WebSocket$onClosed$1.INSTANCE;
|
|
private Function1<? super Error, Unit> onError = WebSocket$onError$1.INSTANCE;
|
|
private Function2<? super InputStreamReader, ? super Integer, Unit> onMessage = WebSocket$onMessage$1.INSTANCE;
|
|
private Function1<? super Opened, Unit> onOpened = WebSocket$onOpened$1.INSTANCE;
|
|
private RawMessageHandler rawMessageHandler;
|
|
private final PublishSubject<Function0<Unit>> schedulerSubject;
|
|
private final SSLSocketFactory socketFactory;
|
|
private State state = State.NOT_YET_CONNECTED;
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
/* renamed from: com.discord.utilities.websocket.WebSocket$1 reason: invalid class name */
|
|
public static final class AnonymousClass1<T> implements Action1<Function0<? extends Unit>> {
|
|
public static final AnonymousClass1 INSTANCE = new AnonymousClass1();
|
|
|
|
/* JADX DEBUG: Method arguments types fixed to match base method, original types: [java.lang.Object] */
|
|
@Override // rx.functions.Action1
|
|
public /* bridge */ /* synthetic */ void call(Function0<? extends Unit> function0) {
|
|
call((Function0<Unit>) function0);
|
|
}
|
|
|
|
public final void call(Function0<Unit> function0) {
|
|
function0.mo1invoke();
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
/* renamed from: com.discord.utilities.websocket.WebSocket$2 reason: invalid class name */
|
|
public static final class AnonymousClass2<T> implements Action1<Throwable> {
|
|
public final /* synthetic */ WebSocket this$0;
|
|
|
|
public AnonymousClass2(WebSocket webSocket) {
|
|
this.this$0 = webSocket;
|
|
}
|
|
|
|
public final void call(Throwable th) {
|
|
WebSocket webSocket = this.this$0;
|
|
m.checkNotNullExpressionValue(th, "it");
|
|
WebSocket.access$handleOnFailure(webSocket, th, null, this.this$0.getState() == State.CLOSED);
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
public static final class Closed {
|
|
private final int code;
|
|
private final String reason;
|
|
|
|
public Closed(int i, String str) {
|
|
this.code = i;
|
|
this.reason = str;
|
|
}
|
|
|
|
public final int getCode() {
|
|
return this.code;
|
|
}
|
|
|
|
public final String getReason() {
|
|
return this.reason;
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
public static final class Companion {
|
|
private Companion() {
|
|
}
|
|
|
|
public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) {
|
|
this();
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
public static final class Error {
|
|
private final Response response;
|
|
private final Throwable throwable;
|
|
|
|
public Error(Throwable th, Response response) {
|
|
m.checkNotNullParameter(th, "throwable");
|
|
this.throwable = th;
|
|
this.response = response;
|
|
}
|
|
|
|
public final Response getResponse() {
|
|
return this.response;
|
|
}
|
|
|
|
public final Throwable getThrowable() {
|
|
return this.throwable;
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
public static final class Opened {
|
|
private final Response response;
|
|
|
|
public Opened(Response response) {
|
|
this.response = response;
|
|
}
|
|
|
|
public final Response getResponse() {
|
|
return this.response;
|
|
}
|
|
}
|
|
|
|
/* compiled from: WebSocket.kt */
|
|
public enum State {
|
|
NOT_YET_CONNECTED,
|
|
CONNECTING,
|
|
CONNECTED,
|
|
CLOSING,
|
|
CLOSED
|
|
}
|
|
|
|
/* JADX DEBUG: Multi-variable search result rejected for r3v0, resolved type: kotlin.jvm.functions.Function3<? super java.lang.String, ? super java.lang.Exception, ? super java.util.Map<java.lang.String, java.lang.String>, kotlin.Unit> */
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public WebSocket(Scheduler scheduler, Function3<? super String, ? super Exception, ? super Map<String, String>, Unit> function3, SSLSocketFactory sSLSocketFactory) {
|
|
m.checkNotNullParameter(function3, "errorLogger");
|
|
this.errorLogger = function3;
|
|
this.socketFactory = sSLSocketFactory;
|
|
PublishSubject<Function0<Unit>> k0 = PublishSubject.k0();
|
|
this.schedulerSubject = k0;
|
|
Observable<Function0<Unit>> J = k0.J();
|
|
if (scheduler == null) {
|
|
AtomicReference<a> atomicReference = a.a;
|
|
scheduler = j0.l.c.m.a;
|
|
}
|
|
J.I(scheduler).W(AnonymousClass1.INSTANCE, new AnonymousClass2(this));
|
|
}
|
|
|
|
public static final /* synthetic */ okhttp3.WebSocket access$getClient$p(WebSocket webSocket) {
|
|
return webSocket.client;
|
|
}
|
|
|
|
public static final /* synthetic */ Function3 access$getErrorLogger$p(WebSocket webSocket) {
|
|
return webSocket.errorLogger;
|
|
}
|
|
|
|
public static final /* synthetic */ State access$getState$p(WebSocket webSocket) {
|
|
return webSocket.state;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$handleOnFailure(WebSocket webSocket, Throwable th, Response response, boolean z2) {
|
|
webSocket.handleOnFailure(th, response, z2);
|
|
}
|
|
|
|
public static final /* synthetic */ void access$schedule(WebSocket webSocket, Function0 function0) {
|
|
webSocket.schedule(function0);
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setClient$p(WebSocket webSocket, okhttp3.WebSocket webSocket2) {
|
|
webSocket.client = webSocket2;
|
|
}
|
|
|
|
public static final /* synthetic */ void access$setState$p(WebSocket webSocket, State state) {
|
|
webSocket.state = state;
|
|
}
|
|
|
|
private final okhttp3.WebSocket createWebSocket(String str, SSLSocketFactory sSLSocketFactory, WebSocketListener webSocketListener) {
|
|
x.a aVar = new x.a();
|
|
if (sSLSocketFactory != null) {
|
|
h.a aVar2 = h.f2831c;
|
|
aVar.b(sSLSocketFactory, h.a.n());
|
|
}
|
|
aVar.a(1, TimeUnit.MINUTES);
|
|
x xVar = new x(aVar);
|
|
Request.a aVar3 = new Request.a();
|
|
aVar3.f(str);
|
|
return xVar.g(aVar3.a(), webSocketListener);
|
|
}
|
|
|
|
public static /* synthetic */ void disconnect$default(WebSocket webSocket, int i, String str, int i2, Object obj) {
|
|
if ((i2 & 1) != 0) {
|
|
i = 1000;
|
|
}
|
|
if ((i2 & 2) != 0) {
|
|
str = null;
|
|
}
|
|
webSocket.disconnect(i, str);
|
|
}
|
|
|
|
private final void handleOnFailure(Throwable th, Response response, boolean z2) {
|
|
if (!z2) {
|
|
disconnect(PointerIconCompat.TYPE_COPY, "Closing due to failure " + th + ", " + response);
|
|
} else {
|
|
this.state = State.CLOSED;
|
|
}
|
|
this.onError.invoke(new Error(th, response));
|
|
}
|
|
|
|
private final void schedule(Function0<Unit> function0) {
|
|
this.schedulerSubject.j.onNext(function0);
|
|
}
|
|
|
|
public final void connect(String str) {
|
|
m.checkNotNullParameter(str, "url");
|
|
disconnect(1000, "Closing existing connection.");
|
|
this.state = State.CONNECTING;
|
|
this.client = createWebSocket(str, this.socketFactory, new ZLibWebSocketListener(new WebSocket$connect$1(this)));
|
|
}
|
|
|
|
public final void disconnect() {
|
|
disconnect$default(this, 0, null, 3, null);
|
|
}
|
|
|
|
public final void disconnect(int i) {
|
|
disconnect$default(this, i, null, 2, null);
|
|
}
|
|
|
|
public final void disconnect(int i, String str) {
|
|
okhttp3.WebSocket webSocket = this.client;
|
|
if (webSocket != null) {
|
|
this.state = State.CLOSING;
|
|
try {
|
|
webSocket.e(i, str);
|
|
} catch (Exception e) {
|
|
handleOnFailure(e, null, true);
|
|
} catch (Throwable th) {
|
|
this.client = null;
|
|
throw th;
|
|
}
|
|
this.client = null;
|
|
}
|
|
}
|
|
|
|
/* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function1<? super com.discord.utilities.websocket.WebSocket$Closed, kotlin.Unit>, kotlin.jvm.functions.Function1<com.discord.utilities.websocket.WebSocket$Closed, kotlin.Unit> */
|
|
public final Function1<Closed, Unit> getOnClosed() {
|
|
return this.onClosed;
|
|
}
|
|
|
|
/* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function1<? super com.discord.utilities.websocket.WebSocket$Error, kotlin.Unit>, kotlin.jvm.functions.Function1<com.discord.utilities.websocket.WebSocket$Error, kotlin.Unit> */
|
|
public final Function1<Error, Unit> getOnError() {
|
|
return this.onError;
|
|
}
|
|
|
|
/* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function2<? super java.io.InputStreamReader, ? super java.lang.Integer, kotlin.Unit>, kotlin.jvm.functions.Function2<java.io.InputStreamReader, java.lang.Integer, kotlin.Unit> */
|
|
public final Function2<InputStreamReader, Integer, Unit> getOnMessage() {
|
|
return this.onMessage;
|
|
}
|
|
|
|
/* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function1<? super com.discord.utilities.websocket.WebSocket$Opened, kotlin.Unit>, kotlin.jvm.functions.Function1<com.discord.utilities.websocket.WebSocket$Opened, kotlin.Unit> */
|
|
public final Function1<Opened, Unit> getOnOpened() {
|
|
return this.onOpened;
|
|
}
|
|
|
|
public final RawMessageHandler getRawMessageHandler() {
|
|
return this.rawMessageHandler;
|
|
}
|
|
|
|
public final State getState() {
|
|
return this.state;
|
|
}
|
|
|
|
public final void message(String str) {
|
|
m.checkNotNullParameter(str, "message");
|
|
okhttp3.WebSocket webSocket = this.client;
|
|
if (webSocket != null && this.state == State.CONNECTED) {
|
|
try {
|
|
webSocket.a(str);
|
|
} catch (IllegalStateException e) {
|
|
handleOnFailure(e, null, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public final void resetListeners() {
|
|
this.onOpened = WebSocket$resetListeners$1.INSTANCE;
|
|
this.onClosed = WebSocket$resetListeners$2.INSTANCE;
|
|
this.onMessage = WebSocket$resetListeners$3.INSTANCE;
|
|
this.rawMessageHandler = null;
|
|
this.onError = WebSocket$resetListeners$4.INSTANCE;
|
|
}
|
|
|
|
public final void setOnClosed(Function1<? super Closed, Unit> function1) {
|
|
m.checkNotNullParameter(function1, "<set-?>");
|
|
this.onClosed = function1;
|
|
}
|
|
|
|
public final void setOnError(Function1<? super Error, Unit> function1) {
|
|
m.checkNotNullParameter(function1, "<set-?>");
|
|
this.onError = function1;
|
|
}
|
|
|
|
public final void setOnMessage(Function2<? super InputStreamReader, ? super Integer, Unit> function2) {
|
|
m.checkNotNullParameter(function2, "<set-?>");
|
|
this.onMessage = function2;
|
|
}
|
|
|
|
public final void setOnOpened(Function1<? super Opened, Unit> function1) {
|
|
m.checkNotNullParameter(function1, "<set-?>");
|
|
this.onOpened = function1;
|
|
}
|
|
|
|
public final void setRawMessageHandler(RawMessageHandler rawMessageHandler) {
|
|
this.rawMessageHandler = rawMessageHandler;
|
|
}
|
|
}
|