Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03caacc1 authored by cketti's avatar cketti
Browse files

Make RealImapConnection (somewhat) thread-safe

parent 00be66b1
Loading
Loading
Loading
Loading
+15 −11
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ class RealImapConnection implements ImapConnection {
    }

    @Override
    public void open() throws IOException, MessagingException {
    public synchronized void open() throws IOException, MessagingException {
        if (open) {
            return;
        } else if (stacktraceForClose != null) {
@@ -190,7 +190,7 @@ class RealImapConnection implements ImapConnection {
    }

    @Override
    public boolean isConnected() {
    public synchronized boolean isConnected() {
        return inputStream != null && outputStream != null && socket != null &&
                socket.isConnected() && !socket.isClosed();
    }
@@ -260,9 +260,11 @@ class RealImapConnection implements ImapConnection {
    }

    @Override
    public void setSocketReadTimeout(int timeout) throws SocketException {
    public synchronized void setSocketReadTimeout(int timeout) throws SocketException {
        if (socket != null) {
            socket.setSoTimeout(timeout);
        }
    }

    private void setUpStreamsAndParserFromSocket() throws IOException {
        setUpStreamsAndParser(socket.getInputStream(), socket.getOutputStream());
@@ -731,7 +733,7 @@ class RealImapConnection implements ImapConnection {
    }

    @Override
    public void close() {
    public synchronized void close() {
        if (!open) {
            return;
        }
@@ -750,7 +752,7 @@ class RealImapConnection implements ImapConnection {

    @Override
    @NotNull
    public OutputStream getOutputStream() {
    public synchronized OutputStream getOutputStream() {
        return outputStream;
    }

@@ -762,7 +764,8 @@ class RealImapConnection implements ImapConnection {

    @Override
    @NotNull
    public List<ImapResponse> executeSimpleCommand(@NotNull String command) throws IOException, MessagingException {
    public synchronized List<ImapResponse> executeSimpleCommand(@NotNull String command)
            throws IOException, MessagingException {
        return executeSimpleCommand(command, false);
    }

@@ -786,8 +789,8 @@ class RealImapConnection implements ImapConnection {

    @Override
    @NotNull
    public List<ImapResponse> executeCommandWithIdSet(@NotNull String commandPrefix, @NotNull String commandSuffix,
            @NotNull Set<Long> ids) throws IOException, MessagingException {
    public synchronized List<ImapResponse> executeCommandWithIdSet(@NotNull String commandPrefix,
            @NotNull String commandSuffix, @NotNull Set<Long> ids) throws IOException, MessagingException {

        GroupedIds groupedIds = IdGrouper.groupIds(ids);
        List<String> splitCommands = ImapCommandSplitter.splitCommand(
@@ -828,7 +831,8 @@ class RealImapConnection implements ImapConnection {

    @Override
    @NotNull
    public String sendCommand(@NotNull String command, boolean sensitive) throws MessagingException, IOException {
    public synchronized String sendCommand(@NotNull String command, boolean sensitive)
            throws MessagingException, IOException {
        try {
            open();

@@ -853,7 +857,7 @@ class RealImapConnection implements ImapConnection {
    }

    @Override
    public void sendContinuation(@NotNull String continuation) throws IOException {
    public synchronized void sendContinuation(@NotNull String continuation) throws IOException {
        outputStream.write(continuation.getBytes());
        outputStream.write('\r');
        outputStream.write('\n');
+7 −4
Original line number Diff line number Diff line
@@ -153,12 +153,15 @@ internal class RealImapFolderIdler(
    private fun sendDone() {
        val folder = folder ?: return
        val connection = connectionProvider.getConnection(folder) ?: return

        synchronized(connection) {
            if (connection.isConnected) {
                doneSent = true
                connection.setSocketDefaultReadTimeout()
                connection.sendContinuation("DONE")
            }
        }
    }

    private fun ImapConnection.setSocketIdleReadTimeout() {
        setSocketReadTimeout((idleRefreshTimeoutProvider.idleRefreshTimeoutMs + SOCKET_EXTRA_TIMEOUT_MS).toInt())