]> source.dussan.org Git - jgit.git/blob
59fe2fc246958b01073634598d5b95b283baf969
[jgit.git] /
1 /*
2  * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Distribution License v. 1.0 which is available at
6  * https://www.eclipse.org/org/documents/edl-v10.php.
7  *
8  * SPDX-License-Identifier: BSD-3-Clause
9  */
10 package org.eclipse.jgit.internal.transport.sshd.agent.connector;
11
12 import java.io.IOException;
13
14 import org.eclipse.jgit.transport.sshd.agent.AbstractConnector;
15
16 /**
17  * A connector using Pageant's shared memory IPC mechanism.
18  */
19 public class PageantConnector extends AbstractConnector {
20
21         private final PageantLibrary lib;
22
23         /**
24          * Creates a new {@link PageantConnector}.
25          */
26         public PageantConnector() {
27                 super(); // Use default maximum message size
28                 this.lib = new PageantLibrary();
29         }
30
31         @Override
32         public boolean connect() throws IOException {
33                 return lib.isPageantAvailable();
34         }
35
36         @Override
37         public void close() throws IOException {
38                 // Nothing to do
39         }
40
41         @Override
42         public byte[] rpc(byte command, byte[] message) throws IOException {
43                 try (PageantLibrary.Pipe pipe = lib
44                                 .createPipe(getClass().getSimpleName(),
45                                                 getMaximumMessageLength())) {
46                         prepareMessage(command, message);
47                         pipe.send(message);
48                         byte[] lengthBuf = new byte[4];
49                         pipe.receive(lengthBuf);
50                         int length = toLength(command, lengthBuf);
51                         byte[] payload = new byte[length];
52                         pipe.receive(payload);
53                         return payload;
54                 }
55         }
56 }