package com.discord.utilities.websocket; import androidx.core.view.PointerIconCompat; import d0.z.d.m; import f0.f0.k.h; import f0.x; import f0.z; 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.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, Unit> errorLogger; private Function1 onClosed = WebSocket$onClosed$1.INSTANCE; private Function1 onError = WebSocket$onError$1.INSTANCE; private Function2 onMessage = WebSocket$onMessage$1.INSTANCE; private Function1 onOpened = WebSocket$onOpened$1.INSTANCE; private RawMessageHandler rawMessageHandler; private final PublishSubject> 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 implements Action1> { 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 function0) { call((Function0) function0); } public final void call(Function0 function0) { function0.mo1invoke(); } } /* compiled from: WebSocket.kt */ /* renamed from: com.discord.utilities.websocket.WebSocket$2 reason: invalid class name */ public static final class AnonymousClass2 implements Action1 { 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, kotlin.Unit> */ /* JADX WARN: Multi-variable type inference failed */ public WebSocket(Scheduler scheduler, Function3, Unit> function3, SSLSocketFactory sSLSocketFactory) { m.checkNotNullParameter(function3, "errorLogger"); this.errorLogger = function3; this.socketFactory = sSLSocketFactory; PublishSubject> k0 = PublishSubject.k0(); this.schedulerSubject = k0; Observable> J = k0.J(); if (scheduler == null) { AtomicReference 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.f2769c; aVar.b(sSLSocketFactory, h.a.n()); } aVar.a(1, TimeUnit.MINUTES); x xVar = new x(aVar); z.a aVar3 = new z.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 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, kotlin.jvm.functions.Function1 */ public final Function1 getOnClosed() { return this.onClosed; } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1 */ public final Function1 getOnError() { return this.onError; } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function2, kotlin.jvm.functions.Function2 */ public final Function2 getOnMessage() { return this.onMessage; } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: kotlin.jvm.functions.Function1, kotlin.jvm.functions.Function1 */ public final Function1 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 function1) { m.checkNotNullParameter(function1, ""); this.onClosed = function1; } public final void setOnError(Function1 function1) { m.checkNotNullParameter(function1, ""); this.onError = function1; } public final void setOnMessage(Function2 function2) { m.checkNotNullParameter(function2, ""); this.onMessage = function2; } public final void setOnOpened(Function1 function1) { m.checkNotNullParameter(function1, ""); this.onOpened = function1; } public final void setRawMessageHandler(RawMessageHandler rawMessageHandler) { this.rawMessageHandler = rawMessageHandler; } }