git-svn-id: svn://db.shs.com.ru/pip@236 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -188,7 +188,7 @@ public:
|
||||
llong logSize() const {return file.size();}
|
||||
|
||||
//! Return true, if position at the end of BinLog file
|
||||
bool isEnd() const {if (!opened_) return true; return file.isEnd();}
|
||||
bool isEnd() const {if (isClosed()) return true; return file.isEnd();}
|
||||
|
||||
//! Returns if BinLog file is empty
|
||||
bool isEmpty() const {return (file.size() <= PIBINARYLOG_SIGNATURE_SIZE + 1);}
|
||||
|
||||
@@ -123,7 +123,8 @@ PIEthernet::PIEthernet(int sock_, PIString ip_port): PIIODevice("", ReadWrite) {
|
||||
construct();
|
||||
parseAddress(ip_port, &ip_s, &port_s);
|
||||
sock = sock_;
|
||||
init_ = opened_ = connected_ = true;
|
||||
opened_ = connected_ = true;
|
||||
init();
|
||||
setParameters(PIEthernet::ReuseAddress | PIEthernet::MulticastLoop);
|
||||
setType(TCP_Client, false);
|
||||
setPath(ip_port);
|
||||
@@ -157,6 +158,7 @@ void PIEthernet::construct() {
|
||||
|
||||
|
||||
bool PIEthernet::init() {
|
||||
if (isOpened()) return true;
|
||||
//cout << "init " << type_ << endl;
|
||||
if (sock_s == sock)
|
||||
sock_s = -1;
|
||||
@@ -348,7 +350,7 @@ bool PIEthernet::joinMulticastGroup(const PIString & group) {
|
||||
piCoutObj << "Only UDP sockets can join multicast groups";
|
||||
return false;
|
||||
}
|
||||
if (!opened_) {
|
||||
if (isClosed()) {
|
||||
if (mcast_queue.contains(group))
|
||||
return false;
|
||||
mcast_queue.enqueue(group);
|
||||
|
||||
@@ -155,22 +155,23 @@ bool PIFile::openDevice() {
|
||||
}
|
||||
}
|
||||
fd = _fopen_call_(p.data(), strType(mode_).data());
|
||||
opened_ = (fd != 0);
|
||||
if (opened_) {
|
||||
bool opened = (fd != 0);
|
||||
if (opened) {
|
||||
fdi = fileno(fd);
|
||||
#ifndef WINDOWS
|
||||
fcntl(fdi, F_SETFL, O_NONBLOCK);
|
||||
#endif
|
||||
seekToBegin();
|
||||
_fseek_call_(fd, 0, SEEK_SET);
|
||||
clearerr(fd);
|
||||
}
|
||||
//piCout << "open file" << fd << opened_;
|
||||
return opened_;
|
||||
return opened;
|
||||
}
|
||||
|
||||
|
||||
bool PIFile::closeDevice() {
|
||||
//piCout << "close file" << fd << opened_;
|
||||
if (!opened_ || fd == 0) return true;
|
||||
if (isClosed() || fd == 0) return true;
|
||||
bool cs = (fclose(fd) == 0);
|
||||
if (cs) fd = 0;
|
||||
fdi = -1;
|
||||
@@ -181,7 +182,7 @@ bool PIFile::closeDevice() {
|
||||
|
||||
PIString PIFile::readLine() {
|
||||
PIByteArray str;
|
||||
if (!opened_) return str;
|
||||
if (isClosed()) return str;
|
||||
int cc;
|
||||
while (!isEnd()) {
|
||||
cc = fgetc(fd);
|
||||
@@ -231,7 +232,7 @@ PIByteArray PIFile::readAll(bool forceRead) {
|
||||
|
||||
|
||||
llong PIFile::size() const {
|
||||
if (!opened_) return -1;
|
||||
if (isClosed()) return -1;
|
||||
llong s, cp = pos();
|
||||
_fseek_call_(fd, 0, SEEK_END); clearerr(fd);
|
||||
s = pos();
|
||||
@@ -298,33 +299,33 @@ PIString PIFile::strType(const PIIODevice::DeviceMode type) {
|
||||
|
||||
|
||||
void PIFile::flush() {
|
||||
if (opened_) fflush(fd);
|
||||
if (isOpened()) fflush(fd);
|
||||
}
|
||||
|
||||
|
||||
void PIFile::seek(llong position) {
|
||||
if (!opened_) return;
|
||||
if (isClosed()) return;
|
||||
_fseek_call_(fd, position, SEEK_SET);
|
||||
clearerr(fd);
|
||||
}
|
||||
|
||||
|
||||
void PIFile::seekToBegin() {
|
||||
if (!opened_) return;
|
||||
if (isClosed()) return;
|
||||
_fseek_call_(fd, 0, SEEK_SET);
|
||||
clearerr(fd);
|
||||
}
|
||||
|
||||
|
||||
void PIFile::seekToEnd() {
|
||||
if (!opened_) return;
|
||||
if (isClosed()) return;
|
||||
_fseek_call_(fd, 0, SEEK_END);
|
||||
clearerr(fd);
|
||||
}
|
||||
|
||||
|
||||
void PIFile::seekToLine(llong line) {
|
||||
if (!opened_) return;
|
||||
if (isClosed()) return;
|
||||
seekToBegin();
|
||||
piForTimes(line) readLine();
|
||||
clearerr(fd);
|
||||
@@ -338,18 +339,18 @@ char PIFile::readChar() {
|
||||
|
||||
void PIFile::setPath(const PIString & path) {
|
||||
PIIODevice::setPath(path);
|
||||
if (opened_) openDevice();
|
||||
if (isOpened()) openDevice();
|
||||
}
|
||||
|
||||
|
||||
llong PIFile::pos() const {
|
||||
if (!opened_) return -1;
|
||||
if (isClosed()) return -1;
|
||||
return _ftell_call_(fd);
|
||||
}
|
||||
|
||||
|
||||
bool PIFile::isEnd() const {
|
||||
if (!opened_) return true;
|
||||
if (isClosed()) return true;
|
||||
return (feof(fd) || ferror(fd));
|
||||
}
|
||||
|
||||
|
||||
@@ -183,7 +183,7 @@ void PIIODevice::write_func() {
|
||||
void PIIODevice::terminate() {
|
||||
timer.stop();
|
||||
thread_started_ = false;
|
||||
if (!isInitialized()) return;
|
||||
if (!init_) return;
|
||||
if (isRunning()) {
|
||||
stop();
|
||||
PIThread::terminate();
|
||||
|
||||
@@ -82,8 +82,6 @@ public:
|
||||
//! Return \b true if mode is WriteOnly or ReadWrite
|
||||
bool isWriteable() const {return (mode_ & WriteOnly);}
|
||||
|
||||
bool isInitialized() const {return init_;}
|
||||
|
||||
//! Return \b true if device is successfully opened
|
||||
bool isOpened() const {return opened_;}
|
||||
|
||||
@@ -215,7 +213,6 @@ public:
|
||||
EVENT_HANDLER1(bool, open, const DeviceMode &, _mode) {mode_ = _mode; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER2(bool, open, const PIString &, _path, const DeviceMode &, _mode) {setPath(_path); mode_ = _mode; if (!init_) init(); opened_ = openDevice(); if (opened_) opened(); return opened_;}
|
||||
EVENT_HANDLER(bool, close) {opened_ = !closeDevice(); if (!opened_) closed(); return !opened_;}
|
||||
EVENT_HANDLER(bool, initialize) {init_ = init(); return init_;}
|
||||
|
||||
EVENT_VHANDLER(void, flush) {;}
|
||||
|
||||
@@ -242,9 +239,6 @@ public:
|
||||
//! \fn bool close()
|
||||
//! \brief Close device
|
||||
|
||||
//! \fn bool initialize()
|
||||
//! \brief Initialize device
|
||||
|
||||
//! \}
|
||||
//! \vhandlers
|
||||
//! \{
|
||||
@@ -310,7 +304,7 @@ protected:
|
||||
|
||||
DeviceMode mode_;
|
||||
ReadRetFunc ret_func_;
|
||||
bool init_, opened_, thread_started_, raise_threaded_read_;
|
||||
bool opened_;
|
||||
void * ret_data_;
|
||||
|
||||
private:
|
||||
@@ -330,6 +324,7 @@ private:
|
||||
PIQueue<PIPair<PIByteArray, ullong> > write_queue;
|
||||
ullong tri;
|
||||
int readed_;
|
||||
bool init_, thread_started_, raise_threaded_read_;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -84,13 +84,13 @@ REGISTER_DEVICE(PIPacketExtractor);
|
||||
|
||||
|
||||
PIPacketExtractor::PIPacketExtractor(PIIODevice * device_, PIPacketExtractor::SplitMode mode) {
|
||||
init_();
|
||||
construct();
|
||||
setDevice(device_);
|
||||
setSplitMode(mode);
|
||||
}
|
||||
|
||||
|
||||
void PIPacketExtractor::init_() {
|
||||
void PIPacketExtractor::construct() {
|
||||
ret_func_header = ret_func_footer = 0;
|
||||
setPayloadSize(0);
|
||||
setTimeout(100);
|
||||
|
||||
@@ -165,7 +165,7 @@ protected:
|
||||
virtual bool validatePayload(uchar * rec, int size) {if (ret_func_ != 0) return ret_func_(ret_data_, rec, size); return true;}
|
||||
|
||||
private:
|
||||
void init_();
|
||||
void construct();
|
||||
void propertyChanged(const PIString & );
|
||||
bool threadedRead(uchar * readed, int size);
|
||||
PIString fullPathPrefix() const {return "pckext";}
|
||||
|
||||
@@ -140,12 +140,12 @@ PRIVATE_DEFINITION_END(PISerial)
|
||||
|
||||
|
||||
PISerial::PISerial(): PIIODevice("", ReadWrite) {
|
||||
_init();
|
||||
construct();
|
||||
}
|
||||
|
||||
|
||||
PISerial::PISerial(const PIString & device_, PISerial::Speed speed_, PIFlags<PISerial::Parameters> params_): PIIODevice(device_, ReadWrite) {
|
||||
_init();
|
||||
construct();
|
||||
setPath(device_);
|
||||
setSpeed(speed_);
|
||||
setParameters(params_);
|
||||
@@ -157,7 +157,7 @@ PISerial::~PISerial() {
|
||||
}
|
||||
|
||||
|
||||
void PISerial::_init() {
|
||||
void PISerial::construct() {
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->hCom = 0;
|
||||
#endif
|
||||
@@ -169,7 +169,6 @@ void PISerial::_init() {
|
||||
setParameters(0);
|
||||
setSpeed(S115200);
|
||||
setDataBitsCount(8);
|
||||
//init();
|
||||
}
|
||||
|
||||
|
||||
@@ -289,7 +288,6 @@ void PISerial::flush() {
|
||||
|
||||
|
||||
bool PISerial::closeDevice() {
|
||||
if (!isInitialized()) return true;
|
||||
if (isRunning()) {
|
||||
stop();
|
||||
PIThread::terminate();
|
||||
@@ -298,7 +296,7 @@ bool PISerial::closeDevice() {
|
||||
#ifdef WINDOWS
|
||||
SetCommState(PRIVATE->hCom, &PRIVATE->sdesc);
|
||||
SetCommMask(PRIVATE->hCom, PRIVATE->mask);
|
||||
CloseHandle(PRIVATE->hCom);
|
||||
// piCoutObj << "close" << CloseHandle(PRIVATE->hCom);
|
||||
PRIVATE->hCom = 0;
|
||||
#else
|
||||
tcsetattr(fd, TCSANOW, &PRIVATE->sdesc);
|
||||
|
||||
@@ -223,7 +223,7 @@ protected:
|
||||
//! Executes when any read function was successful. Default implementation does nothing
|
||||
virtual void received(const void * data, int size) {;}
|
||||
|
||||
void _init();
|
||||
void construct();
|
||||
void applySettings();
|
||||
int convertSpeed(PISerial::Speed speed);
|
||||
bool setBit(int bit, bool on, const PIString & bname);
|
||||
|
||||
@@ -298,7 +298,7 @@ bool PIUSB::closeDevice() {
|
||||
|
||||
int PIUSB::read(void * read_to, int max_size) {
|
||||
#ifdef PIP_USB
|
||||
if (!opened_ || ep_read.isNull()) return -1;
|
||||
if (isClosed() || ep_read.isNull()) return -1;
|
||||
switch (ep_read.transfer_type) {
|
||||
case Endpoint::Bulk: /*piCoutObj << "bulk read" << max_size;*/ return usb_bulk_read(hdev, ep_read.address, (char * )read_to, max_size, timeout_r); break;
|
||||
case Endpoint::Interrupt: return usb_interrupt_read(hdev, ep_read.address, (char * )read_to, max_size, timeout_r); break;
|
||||
@@ -313,7 +313,7 @@ int PIUSB::read(void * read_to, int max_size) {
|
||||
|
||||
int PIUSB::write(const void * data, int max_size) {
|
||||
#ifdef PIP_USB
|
||||
if (!opened_ || ep_write.isNull()) return -1;
|
||||
if (isClosed() || ep_write.isNull()) return -1;
|
||||
switch (ep_read.transfer_type) {
|
||||
case Endpoint::Bulk: /*piCoutObj << "bulk write" << max_size;*/ return usb_bulk_write(hdev, ep_write.address, (char * )const_cast<void * >(data), max_size, timeout_w); break;
|
||||
case Endpoint::Interrupt: return usb_interrupt_write(hdev, ep_read.address, (char * )data, max_size, timeout_w); break;
|
||||
@@ -328,7 +328,7 @@ int PIUSB::write(const void * data, int max_size) {
|
||||
|
||||
int PIUSB::controlWrite(const void * data, int max_size) {
|
||||
#ifdef PIP_USB
|
||||
if (!opened_) return -1;
|
||||
if (isClosed()) return -1;
|
||||
//return usb_control_msg(hdev, );
|
||||
return -1;
|
||||
#else
|
||||
@@ -339,7 +339,7 @@ int PIUSB::controlWrite(const void * data, int max_size) {
|
||||
|
||||
void PIUSB::flush() {
|
||||
#ifdef PIP_USB
|
||||
if (!opened_) return;
|
||||
if (isClosed()) return;
|
||||
if (!ep_read.isNull()) usb_resetep(hdev, ep_read.address);
|
||||
if (!ep_write.isNull()) usb_resetep(hdev, ep_write.address);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user