package com.seewo.remote.c;

import android.util.Log;
import com.cvte.vman.instance.InvokeCmd;
import com.seewo.remote.c.c;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import java.util.Arrays;

/* JADX INFO: compiled from: RemoteConnector.java */
/* JADX INFO: loaded from: classes.dex */
class f implements c {
    private Bootstrap b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private EventLoopGroup f24c;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private String f27f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private int f28g;
    private c a = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private Channel f25d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private g f26e = null;

    f(String str, int i2) {
        this.b = null;
        this.f24c = null;
        this.f27f = str;
        this.f28g = i2;
        this.f24c = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        this.b = bootstrap;
        bootstrap.remoteAddress(this.f27f, this.f28g);
        this.b.group(this.f24c).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, Boolean.TRUE).option(ChannelOption.TCP_NODELAY, Boolean.TRUE).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, Integer.valueOf(InvokeCmd.CMD_PMSCTRL_SET_PMS_WAKEUP_SOURCES));
        c();
    }

    private void c() {
        if (this.f26e == null) {
            this.f26e = new g(this);
        }
        Bootstrap bootstrap = this.b;
        if (bootstrap != null) {
            bootstrap.handler(this.f26e);
        }
    }

    static /* synthetic */ void e(Future future) throws Exception {
        Log.d("RemoteConnector", "operationComplete:" + future.isSuccess());
        Log.w("RemoteConnector", "cause:" + future.cause());
    }

    private void f() {
        this.f26e = null;
    }

    @Override // com.seewo.remote.c.c
    public void a(c.a aVar, ChannelHandlerContext channelHandlerContext) {
        if (aVar == c.a.CONNECTED) {
            if (this.f25d != channelHandlerContext.channel()) {
                this.f25d = channelHandlerContext.channel();
            }
            e.c.d.y.e.f("RemoteConnector", "Remote connected");
        } else {
            e.c.d.y.e.f("RemoteConnector", "Remote disconnected");
        }
        c cVar = this.a;
        if (cVar != null) {
            cVar.a(aVar, channelHandlerContext);
        }
    }

    @Override // com.seewo.remote.c.c
    public void b(byte[] bArr) {
        e.c.d.y.e.g("RemoteConnector", "Received from remote: " + Arrays.toString(bArr));
        c cVar = this.a;
        if (cVar != null) {
            cVar.b(bArr);
        }
    }

    public boolean d() {
        Channel channel = this.f25d;
        return channel != null && channel.isActive();
    }

    public ChannelFuture g(byte[] bArr) {
        if (d()) {
            e.c.d.y.e.g("RemoteConnector", "Send to remote: " + Arrays.toString(bArr));
            return this.f25d.writeAndFlush(bArr);
        }
        e.c.d.y.e.b("RemoteConnector", "Remote not connected, unsent data: " + Arrays.toString(bArr));
        return null;
    }

    public void h(c cVar) {
        this.a = cVar;
    }

    public synchronized void i() {
        long jCurrentTimeMillis = System.currentTimeMillis();
        ChannelFuture channelFutureConnect = this.b.connect();
        channelFutureConnect.addListener((GenericFutureListener<? extends Future<? super Void>>) new GenericFutureListener() { // from class: com.seewo.remote.c.b
            @Override // io.netty.util.concurrent.GenericFutureListener
            public final void operationComplete(Future future) throws Exception {
                f.e(future);
            }
        });
        this.f25d = channelFutureConnect.channel();
        e.c.d.y.e.f("RemoteConnector", "Connect AndroidRemote: " + this.f27f + ":" + this.f28g);
        e.c.d.y.e.f("RemoteConnector", "connect time:" + getClass() + " time:" + (System.currentTimeMillis() - jCurrentTimeMillis));
    }

    public synchronized void j() {
        f();
        if (this.f25d != null) {
            this.f25d.close();
            this.f25d = null;
        }
        if (this.f24c != null) {
            this.f24c.shutdownGracefully();
            this.f24c = null;
        }
        this.a = null;
        this.b = null;
    }
}
