void CSerialSocket::Close(void)
{
- SocketClose(m_socket);
+ if (IsOpen())
+ SocketClose(m_socket);
}
void CSerialSocket::Shutdown(void)
{
- SocketClose(m_socket);
+ if (IsOpen())
+ SocketClose(m_socket);
}
ssize_t CSerialSocket::Write(void* data, size_t len)
{
- return SocketWrite(m_socket, &m_iError, data, len);
+ return IsOpen() ? SocketWrite(m_socket, &m_iError, data, len) : -1;
}
ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
{
- return SocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
+ return IsOpen() ? SocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
}
//setting all this stuff up is a pain in the ass
bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
{
iTimeoutMs = 0;
- CLockObject lock(m_mutex);
+ if (IsOpen())
+ return false;
if (m_iDatabits != SERIAL_DATA_BITS_FIVE && m_iDatabits != SERIAL_DATA_BITS_SIX &&
m_iDatabits != SERIAL_DATA_BITS_SEVEN && m_iDatabits != SERIAL_DATA_BITS_EIGHT)
void CSerialSocket::Close(void)
{
- SerialSocketClose(m_socket);
+ if (IsOpen())
+ SerialSocketClose(m_socket);
}
void CSerialSocket::Shutdown(void)
{
- SerialSocketClose(m_socket);
+ if (IsOpen())
+ SerialSocketClose(m_socket);
}
ssize_t CSerialSocket::Write(void* data, size_t len)
{
- return SerialSocketWrite(m_socket, &m_iError, data, len);
+ return IsOpen() ? SerialSocketWrite(m_socket, &m_iError, data, len) : -1;
}
ssize_t CSerialSocket::Read(void* data, size_t len, uint64_t iTimeoutMs /* = 0 */)
{
- return SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs);
+ return IsOpen() ? SerialSocketRead(m_socket, &m_iError, data, len, iTimeoutMs) : -1;
}
bool CSerialSocket::Open(uint64_t iTimeoutMs /* = 0 */)
{
iTimeoutMs = 0;
+ if (IsOpen())
+ return false;
+
CStdString strComPath = "\\\\.\\" + m_strName;
CLockObject lock(m_mutex);
m_socket = CreateFile(strComPath.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);