package com.alterdekim.game.component.rtmp;

import com.alterdekim.game.message.SerializerUtils;
import com.alterdekim.game.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

@Slf4j
@Getter
@AllArgsConstructor
public abstract class ConnectionProcessor {
    private InputStream inputStream;
    private OutputStream outputStream;
    private Socket sock;

    public abstract ConnectionState getState();
    public abstract ConnectionState process() throws IOException;

    public void write(List<Byte> bytes) throws IOException {
        log.info("Packet sent: {}", StringUtils.bytesToHex(SerializerUtils.bytesToPrimitive(bytes)));
        this.outputStream.write(SerializerUtils.bytesToPrimitive(bytes));
        this.outputStream.flush();
    }

    public void close() {
        log.info("ConnectionProcessor.close()");
        try {
            this.sock.close();
        } catch (IOException e) {
            log.error("ConnectionProcessor.close()", e);
        }
    }
}
