Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
chivstyle committed Mar 3, 2023
1 parent adf7041 commit f19d13d
Show file tree
Hide file tree
Showing 24 changed files with 251 additions and 115 deletions.
3 changes: 2 additions & 1 deletion comxd/CodecTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ void CodecTool::Genereate()
int code = mOutputFmt.GetKey(mOutputFmt.GetIndex());
Codec* codec = CodecFactory::Inst()->CreateInst((String)mOutputEncoding.GetData());
if (codec) {
std::string bit = codec->TranscodeFromUTF8((const unsigned char*)input.data(), input.length());
size_t ep;
std::string bit = codec->TranscodeFromUTF8((const unsigned char*)input.data(), input.length(), &ep);
switch (code) {
case OutputFormat::FC_C:
mOutput.SetData(Generate_C(bit));
Expand Down
6 changes: 6 additions & 0 deletions comxd/ioimpls/PipeClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,10 @@ bool NamedPipeClient::Start()
return true;
}

bool NamedPipeClient::Reconnect()
{
Stop();
return Start();
}

#endif
1 change: 1 addition & 0 deletions comxd/ioimpls/PipeClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class NamedPipeClient : public SerialIo {

bool Start();
void Stop();
bool Reconnect();
int Available() const;
size_t Read(unsigned char* buf, size_t sz);
size_t Write(const unsigned char* buf, size_t sz);
Expand Down
6 changes: 6 additions & 0 deletions comxd/ioimpls/PipeServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,10 @@ bool NamedPipeServer::Start()
return true;
}

bool NamedPipeServer::Reconnect()
{
Stop();
return Start();
}

#endif
1 change: 1 addition & 0 deletions comxd/ioimpls/PipeServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class NamedPipeServer : public SerialIo {

bool Start();
void Stop();
bool Reconnect();
int Available() const;
size_t Read(unsigned char* buf, size_t sz);
size_t Write(const unsigned char* buf, size_t sz);
Expand Down
31 changes: 18 additions & 13 deletions comxd/ioimpls/SSHPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ using namespace Upp;
const char* SSHPort::kDeviceType = "SSH";

SSHPort::SSHPort(std::shared_ptr<Upp::SshSession> session, Upp::String host, int port, Upp::String user, Upp::String term)
: mSession(session)
: mShell(nullptr)
, mSession(session)
, mHost(host)
, mPort(port)
, mUser(user)
, mTerm(term)
, mShouldExit(false)
, mRunning(false)
{
mShell = CreateShell();
//
WhenUsrBar = [=](Bar& bar) {
bar.Add("Upload", comxd::upload(), [=]() {
Upload();
Expand Down Expand Up @@ -80,21 +79,27 @@ void SSHPort::Stop()
}
}

bool SSHPort::Reconnect()
{
Stop();
//
SSHDevsDialog d;
if (d.Reconnect(this)) {
return Start();
}
}

bool SSHPort::Start()
{
if (!mJob.joinable()) {
mShouldExit = false;
if (!mShell) {
SSHDevsDialog d;
if (!d.Reconnect(this)) {
return false;
}
}
}
if (mShell) {
return true;
}
// before start the job, set the running to true
mRunning = true;
// start the job
// create a shell
mShell = CreateShell();
// create the job
mShouldExit = false;
mJob = std::thread([=]() {
mShell->Run(mTerm, 80, 34, Null);
mRunning = false;
Expand Down
1 change: 1 addition & 0 deletions comxd/ioimpls/SSHPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class SSHPort : public SerialIo {
//
bool Start();
void Stop();
bool Reconnect();
//
Upp::SshShell* CreateShell();
//
Expand Down
24 changes: 19 additions & 5 deletions comxd/ioimpls/SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ const char* SerialPort::kDeviceType = "Serial";

bool SerialPort::Start()
{
if (!mSerial->isOpen()) {
SerialDevsDialog d;
return d.Reconnect(this);
}
return true;
try {
if (!mSerial->isOpen()) {
mSerial->open();
}
return true;
} catch (const std::exception& ex) {
(void)ex;
}
return false;
}

void SerialPort::Stop()
{
mSerial->close();
}

bool SerialPort::Reconnect()
{
Stop();
//
SerialDevsDialog d;
if (d.Reconnect(this)) {
return Start();
}
}
1 change: 1 addition & 0 deletions comxd/ioimpls/SerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class SerialPort : public SerialIo {
//
bool Start() override;
void Stop() override;
bool Reconnect() override;
//
int Available() const override
{
Expand Down
15 changes: 12 additions & 3 deletions comxd/ioimpls/TcpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,22 @@ size_t TcpClient::Write(const unsigned char* buf, size_t sz)

bool TcpClient::Start()
{
if (!mTcp->IsOpen()) {
TcpClientDialog d;
d.Reconnect(this);
if (mTcp->IsOpen()) {
return true;
}
mTcp->Timeout(0); // NON BLOCK
mShouldStop = false;
mRx = std::thread([=]() { RxProc(); });

return true;
}

bool TcpClient::Reconnect()
{
Stop();
//
TcpClientDialog d;
if (d.Reconnect(this)) {
return Start();
}
}
2 changes: 2 additions & 0 deletions comxd/ioimpls/TcpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class TcpClient : public SerialIo {

bool Start();
void Stop();
bool Reconnect();
//
int Available() const;
size_t Read(unsigned char* buf, size_t sz);
size_t Write(const unsigned char* buf, size_t sz);
Expand Down
9 changes: 5 additions & 4 deletions comxd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ class MainWindow : public WithMainWindow<TopWindow> {
if (mDevsTab.GetCount()) {
auto conn = dynamic_cast<SerialConn*>(mDevsTab.GetItem(mDevsTab.Get()).GetSlave());
if (conn) {
TabCtrl::Item* item = FindConnItem(conn);
bar.Add(t_("Reconnect"), comxd::reconnect(), [=]() {
this->Disable();
conn->Stop();
conn->GetIo()->Stop();
if (conn->GetIo()->Start()) {
// restart the I/O
if (conn->GetIo()->Reconnect()) {
conn->Start();
TabCtrl::Item* item = FindConnItem(conn);
if (item) {
auto& conns = ConnCreateFactory::Inst()->GetSupportedConnIntroductions();
String type = conn->GetIo()->DeviceType();
Expand All @@ -197,7 +197,8 @@ class MainWindow : public WithMainWindow<TopWindow> {
}
}
} else {
// the user close the I/O, do nothing here
// failed to reconnect the I/O device, report error
item->SetImage(comxd::exception);
}
this->Enable();
}).Help(t_("Reconnect to the I/O device"));
Expand Down
14 changes: 7 additions & 7 deletions xterminal/Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,18 @@ class Codec {
//
const std::string& GetName() const { return mName; }
void SetName(const char* name) { mName = name; }
// Raw to UTF-32
virtual std::vector<uint32_t> TranscodeToUTF32(const unsigned char* data, size_t sz, size_t& ep) = 0;
// Raw to UTF-8
virtual std::string TranscodeToUTF8(const unsigned char* data, size_t sz) = 0;
// UTF-32 To this
// current to UTF-32
virtual std::vector<uint32_t> TranscodeToUTF32(const unsigned char* data, size_t sz, size_t* ep) = 0;
// current to UTF-8
virtual std::string TranscodeToUTF8(const unsigned char* data, size_t sz, size_t* ep) = 0;
// UTF-32 To current
virtual std::string TranscodeFromUTF32(const uint32_t* data, size_t sz) = 0;
inline std::string TranscodeFromUTF32(const uint32_t& cc)
{
return TranscodeFromUTF32(&cc, 1);
}
//
virtual std::string TranscodeFromUTF8(const unsigned char* data, size_t sz) = 0;
// UTF-8 to current
virtual std::string TranscodeFromUTF8(const unsigned char* data, size_t sz, size_t* ep) = 0;
//
private:
std::string mName;
Expand Down
1 change: 1 addition & 0 deletions xterminal/SerialIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SerialIo {
//
virtual bool Start() { return true; }
virtual void Stop() { }
virtual bool Reconnect() = 0;
/// Was data available?
/// @return
/// - <0 Error, device was corrupted
Expand Down
Loading

0 comments on commit f19d13d

Please sign in to comment.