Skip to content

Commit

Permalink
[xssh][stream] better implementation (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
buglloc authored Dec 11, 2023
1 parent 9436acf commit e780037
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
65 changes: 41 additions & 24 deletions lib/xssh/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@

namespace XSSH
{

int ChanStream::available()
{
return 0;
}

int ChanStream::peek()
int ChanReader::read()
{
if (peekBuf != 0) {
return peekBuf;
if (ssh_channel_is_eof(chan)) {
return 0;
}

if (readBytes(&peekBuf, 1) == 1) {
return peekBuf;
char buf;
if (readBytes(&buf, 1) == 1) {
return buf;
}

return -1;
}

int ChanStream::read()
{
int out = peek();
peekBuf = 0;
return out;
}

size_t ChanStream::readBytes(char* buffer, size_t length)
size_t ChanReader::readBytes(char* buffer, size_t length)
{
if (ssh_channel_is_eof(chan)) {
return 0;
Expand All @@ -47,21 +35,50 @@ namespace XSSH
return rc < 0 ? 0 : rc;
}

size_t ChanPrinter::write(uint8_t b)
size_t ChanWriter::write(uint8_t b)
{
return write(&b, 1);
if (size + 1 >= BUF_SIZE && !flush()) {
return 0;
}

buffer[size++] = b;
return 1;
}

size_t ChanPrinter::write(const uint8_t *buffer, size_t length)
size_t ChanWriter::write(const uint8_t* bytes, size_t length)
{
if (!ssh_channel_is_open(chan)) {
return 0;
}

for (size_t i = 0; i < length; i++) {
if (write(bytes[i]) != 1) {
return i;
}
}

return length;
}

bool ChanWriter::flush() {
if (size == 0) {
return true;
}

if (!ssh_channel_is_open(chan)) {
return false;
}

int rc = 0;
do {
rc = ssh_channel_write(chan, buffer, length);
rc = ssh_channel_write(chan, buffer, size);
} while (rc == SSH_AGAIN);
return rc < 0 ? 0 : rc;

if (rc > 0) {
size = 0;
return true;
}

return false;
}
}
38 changes: 22 additions & 16 deletions lib/xssh/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,54 @@

#include <Arduino.h>

// fwd
typedef struct ssh_channel_struct* ssh_channel;

#define BUF_SIZE 512

namespace XSSH
{
class ChanStream: public Stream
class ChanReader
{
public:
explicit ChanStream(ssh_channel chan) : chan(chan) {};
explicit ChanReader(ssh_channel chan) : chan(chan) {};

void setTimeouts(int pollMs, int readMs)
{
pollTimeout = pollMs;
readTimeout = readMs;
};

virtual int available() override;
virtual int read() override;
virtual int peek() override;
virtual size_t readBytes(char* buffer, size_t length) override;
virtual size_t write(uint8_t b) override
{
return 0;
}
int read();
size_t readBytes(char* s, size_t length);

private:
ssh_channel chan;
char peekBuf;
int pollTimeout = 100;
int readTimeout = 5000;
};

class ChanPrinter: public Print
class ChanWriter
{
public:
explicit ChanPrinter(ssh_channel chan) : chan(chan) {};
explicit ChanWriter(ssh_channel chan) : chan(chan)
{
size = 0;
};

size_t write(uint8_t b);
size_t write(const uint8_t* bytes, size_t length);
bool flush();

virtual size_t write(uint8_t b) override;
virtual size_t write(const uint8_t *buffer, size_t length) override;
~ChanWriter()
{
flush();
}

using Print::write; // pull in write(str) and write(buf, size) from Print
private:
ssh_channel chan;
uint8_t buffer[BUF_SIZE];
size_t size;
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void doMainTask(void* params)
}

w.write('\n');
return ok;
return w.flush();
});
} while (1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ssh_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ struct TSshAuthInfo: public Printable
size_t printTo(Print& p) const override;
};

using TSshReader = XSSH::ChanStream;
using TSshWriter = XSSH::ChanPrinter;
using TSshReader = XSSH::ChanReader;
using TSshWriter = XSSH::ChanWriter;
using TSshAuthInfoHolder = std::unique_ptr<TSshAuthInfo>;
using TSshActionCallback = std::function<bool(const TSshAuthInfoHolder& sess, const String& cmd, TSshReader& r, TSshWriter& w)>;

Expand Down

0 comments on commit e780037

Please sign in to comment.