Commit 5739065d authored by Pavel Vainerman's avatar Pavel Vainerman

(ModbusTCPSession): добавил обработку ситуации когда клиент уже закрыл соединение,

которое мы пытаемся создать. (modbustcpserver-echo): добавил возможность задать паузу после каждого ответа
parent 7173801a
......@@ -23,6 +23,11 @@ class MBTCPServer
replyVal = val;
}
inline timeout_t setAfterSendPause( timeout_t msec )
{
return sslot->setAfterSendPause(msec);
}
void execute(); /*!< основной цикл работы */
void setLog( std::shared_ptr<DebugStream>& dlog );
......@@ -89,8 +94,8 @@ class MBTCPServer
ModbusTCPServerSlot* sslot;
std::unordered_set<ModbusRTU::ModbusAddr> vaddr; /*!< адреса данного узла */
bool verbose;
long replyVal;
bool verbose = { false };
long replyVal = { -1 };
#if 0
typedef std::unordered_map<ModbusRTU::mbErrCode, unsigned int> ExchangeErrorMap;
ExchangeErrorMap errmap; /*!< статистика обмена */
......
......@@ -16,6 +16,7 @@ static struct option longopts[] =
{ "myaddr", required_argument, 0, 'a' },
{ "port", required_argument, 0, 'p' },
{ "const-reply", required_argument, 0, 'c' },
{ "after-send-pause", required_argument, 0, 's' },
{ NULL, 0, 0, 0 }
};
// --------------------------------------------------------------------------
......@@ -29,6 +30,7 @@ static void print_help()
printf(" myaddr=255 - Reply to all RTU-addresses.\n");
printf("[-p|--port] port - Server port. Default: 502.\n");
printf("[-c|--const-reply] val - Reply 'val' for all queries\n");
printf("[-s|--after-send-pause] msec - Pause after send request. Default: 0\n");
}
// --------------------------------------------------------------------------
int main( int argc, char** argv )
......@@ -42,6 +44,7 @@ int main( int argc, char** argv )
string myaddr("0x01");
auto dlog = make_shared<DebugStream>();
int replyVal = -1;
timeout_t afterpause = 0;
ost::Thread::setException(ost::Thread::throwException);
......@@ -49,7 +52,7 @@ int main( int argc, char** argv )
{
while(1)
{
opt = getopt_long(argc, argv, "hva:p:i:c:", longopts, &optindex);
opt = getopt_long(argc, argv, "hva:p:i:c:s:", longopts, &optindex);
if( opt == -1 )
break;
......@@ -80,6 +83,10 @@ int main( int argc, char** argv )
replyVal = uni_atoi(optarg);
break;
case 's':
afterpause = uni_atoi(optarg);
break;
case '?':
default:
printf("? argumnet\n");
......@@ -105,6 +112,7 @@ int main( int argc, char** argv )
MBTCPServer mbs(vaddr, iaddr, port, verb);
mbs.setLog(dlog);
mbs.setVerbose(verb);
mbs.setAfterSendPause(afterpause);
if( replyVal != -1 )
mbs.setReply(replyVal);
......
......@@ -55,8 +55,17 @@ ModbusTCPSession::ModbusTCPSession( int sfd, const std::unordered_set<ModbusAddr
ost::InetAddress iaddr = sock->getIPV4Peer(&p);
// resolve..
caddr = string( iaddr.getHostname() );
if( !iaddr.isInetAddress() )
{
ostringstream err;
err << "(ModbusTCPSession): unknonwn ip(0.0.0.0) client disconnected?!";
if( dlog->is_crit() )
dlog->crit() << err.str() << endl;
sock.reset();
throw SystemError(err.str());
}
caddr = string( iaddr.getHostname() );
ostringstream s;
s << iaddr << ":" << p;
peername = s.str();
......@@ -65,7 +74,10 @@ ModbusTCPSession::ModbusTCPSession( int sfd, const std::unordered_set<ModbusAddr
{
ostringstream err;
err << ex.what();
if( dlog->is_crit() )
dlog->crit() << "(ModbusTCPSession): err: " << err.str() << endl;
sock.reset();
throw SystemError(err.str());
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment