📄 cnet.cpp
字号:
linger lg;
lg.l_onoff = 1;
lg.l_linger = 10;
setsockopt (sock, SOL_SOCKET, SO_LINGER, reinterpret_cast <const char*> (&lg), sizeof (linger));
hRecvThread = CreateThread (NULL,
0,
(LPTHREAD_START_ROUTINE) RecvThread,
this,
0,
(LPDWORD) &dwRecvThreadID);
if (hRecvThread == NULL)
{
CConnection::SetLastError (1002);
closesocket (sock);
return false;
}
m_sync.Enter ();
m_socket = sock;
m_addr = addr;
m_sync.Leave ();
return true;
}
void CConnection::Disconnect ()
{
if (!IsConnected ())
{
CConnection::SetLastError (WSAENOTCONN);
return;
}
int status;
fd_set readfds;
timeval timeout;
// set the event to non-signaled
ResetEvent (m_event);
m_sync.Enter ();
SOCKET s = m_socket;
m_socket = INVALID_SOCKET;
m_sync.Leave ();
shutdown (s, 0x01);
FD_ZERO (&readfds);
FD_SET (s, &readfds);
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
status = select (1, &readfds, NULL, NULL, &timeout);
if (status == SOCKET_ERROR)
{
int err = WSAGetLastError();
CConnection::SetLastError (err);
}
// now wait until receive-thread is done (5 seconds max!)
Sleep (10);
WaitForSingleObject (hRecvThread, 5000);
closesocket (s);
DWORD dwExitCode;
GetExitCodeThread (hRecvThread, &dwExitCode);
if (dwExitCode == STILL_ACTIVE)
{
// wait for the event to be set and then reset
WaitForSingleObject (m_event, 2000);
ResetEvent (m_event);
// if still active, we might have a dead-lock
// might be caused by the callback fct
TerminateThread (hRecvThread, 0);
}
hRecvThread = NULL;
}
bool CConnection::PeerInfo (char* host, int host_len, unsigned int* port)
{
if (!IsConnected ())
{
CConnection::SetLastError (WSAENOTCONN);
return false;
}
sockaddr_in addr;
m_sync.Enter ();
addr = m_addr;
m_sync.Leave ();
if (port != NULL)
*port = (unsigned int) ntohs (addr.sin_port);
if (host != NULL)
{
char* ip = inet_ntoa (addr.sin_addr);
memset (host, 0, host_len);
int uselen = strlen (ip) + 1;
if (host_len < uselen) uselen = host_len;
memcpy (host, ip, host_len);
}
return true;
}
int CConnection::Send (const char* buffer, int bufferlen)
{
if (!IsConnected ())
{
CConnection::SetLastError (WSAENOTCONN);
return 0;
}
m_sync.Enter ();
SOCKET sock = m_socket;
m_sync.Leave ();
return send (sock, buffer, bufferlen, 0);
}
int CConnection::Receive (char* buffer, int bufferlen)
{
int iRecvd;
m_sync.Enter ();
iRecvd = m_data.Remove (buffer, bufferlen);
m_sync.Leave ();
return iRecvd;
}
void CConnection::SetCloseFunc (CALLBACKFUNC hFunc)
{
m_sync.Enter ();
hCloseFunc = hFunc;
m_sync.Leave ();
}
void CConnection::SetCloseEvent (HANDLE hEvent)
{
m_sync.Enter ();
hCloseEvent = hEvent;
m_sync.Leave ();
}
void CConnection::SetReceiveFunc (CALLBACKFUNC hFunc)
{
m_sync.Enter ();
hRecvFunc = hFunc;
m_sync.Leave ();
}
void CConnection::SetReceiveEvent (HANDLE hEvent)
{
m_sync.Enter ();
hRecvEvent = hEvent;
m_sync.Leave ();
}
bool CConnection::IsConnected ()
{
bool bConnd;
m_sync.Enter ();
bConnd = m_socket != INVALID_SOCKET;
m_sync.Leave ();
return bConnd;
}
int CConnection::HasReceived ()
{
int iRecvd;
m_sync.Enter ();
iRecvd = m_data.Length ();
m_sync.Leave ();
return iRecvd;
}
int CConnection::RecvWait ()
{
const int MSG_CHUNK = 512;
int status = 0;
char buffer[MSG_CHUNK];
SOCKET sock;
while (true)
{
m_sync.Enter ();
sock = m_socket;
m_sync.Leave ();
if (sock == INVALID_SOCKET)
{
SetEvent (m_event);
return 0;
}
// By default sockets are created in blocking mode.
// Just keep reading until process destroyed.
memset (buffer, 0, MSG_CHUNK);
status = recv (sock, buffer, MSG_CHUNK, 0);
if (status == SOCKET_ERROR)
{
int err = WSAGetLastError();
CConnection::SetLastError (err);
if (hCloseEvent) SetEvent (hCloseEvent); // if an event was given, pulse the event
if (hCloseFunc) (hCloseFunc) ((DWORD) this); // if a function ptr was given, call the function
Disconnect ();
SetEvent (m_event);
return 0;
}
if (status > 0)
{
m_sync.Enter ();
m_data.Append (buffer, status);
HANDLE hEvent = hRecvEvent;
CALLBACKFUNC fncCallback = hRecvFunc;
m_sync.Leave ();
if (hEvent) SetEvent (hEvent); // if an event was given, pulse the event
if (fncCallback) (fncCallback) ((DWORD) this); // if a function ptr was given, call the function
}
else
{
// socket was "gracefully" closed - thus, no error
if (hCloseEvent) SetEvent (hCloseEvent); // if an event was given, pulse the event
if (hCloseFunc) (hCloseFunc) ((DWORD) this); // if a function ptr was given, call the function
Disconnect ();
SetEvent (m_event);
return 0;
}
}
return 0;
}
int CConnection::RecvThread (void* pThis)
{
return ((CConnection*) (pThis))->RecvWait ();
}
CNetworking::CConnectionList::CConnectionList ()
{
m_first = NULL;
m_last = NULL;
m_length = 0;
}
CNetworking::CConnectionList::~CConnectionList ()
{
if (m_length == 0) return;
Node* next = m_first->m_next;
do
{
delete m_first;
m_first = next;
if (m_first)
next = m_first->m_next;
}
while (m_first);
}
// add a connection to the end of the list
void CNetworking::CConnectionList::Add (CConnection* con)
{
if (m_length == 0)
{
m_last = m_first = new Node ();
}
else
{
m_last->m_next = new Node ();
m_last = m_last->m_next;
}
m_last->m_con = con;
m_length ++;
}
// remove a connection from the beginning of the list
CConnection* CNetworking::CConnectionList::Remove ()
{
if (m_length == 0) return NULL;
CConnection* ret = m_first->m_con;
Node* newfirst = m_first->m_next;
delete m_first;
m_first = newfirst;
m_length --;
return ret;
}
// remove a connection at index i
CConnection* CNetworking::CConnectionList::Remove (int i)
{
if (m_length == 0) return NULL;
if (i == 0) return Remove ();
Node* ret = m_first;
for (int j = 0; j < i - 1; j++)
ret = ret->m_next;
Node* d = ret->m_next;
CConnection* cret = d->m_con;
ret->m_next = d->m_next;
delete d;
m_length --;
return cret;
}
CConnection* CNetworking::CConnectionList::Item (int i)
{
return operator[] (i);
}
CConnection* CNetworking::CConnectionList::operator [] (int i)
{
if (m_length == 0) return NULL;
Node* ret = m_first;
for (int j = 0; j < i; j++)
ret = ret->m_next;
return ret->m_con;
}
long CNetworking::CConnectionList::Length ()
{
return m_length;
}
CNetworking::CConnectionList::Node::Node ()
{
m_con = NULL;
m_next = NULL;
}
CConnection::CDataStack::CDataStack ()
{
m_buffer = NULL;
m_length = 0;
}
CConnection::CDataStack::~CDataStack ()
{
delete[] m_buffer;
}
void CConnection::CDataStack::Append (const char* data, int len)
{
if (data == NULL || len <= 0) return;
m_sync.Enter ();
if (m_length == 0)
{
m_buffer = new char[len];
memcpy(m_buffer, data, len);
m_length = len;
}
else
{
// backup old buffer (temporarily)
char* oldbuff = new char[m_length];
memcpy(oldbuff, m_buffer, m_length);
// create a new buffer that holds the old one PLUS the new stuff
delete[] m_buffer;
m_buffer = new char[m_length + len];
// copy old buffer and new data into the new (bigger) buffer
memcpy(m_buffer, oldbuff, m_length);
memcpy(&m_buffer[m_length], data, len);
// store the new length of the data
m_length += len;
// do clean-up
delete[] oldbuff;
}
m_sync.Leave ();
}
int CConnection::CDataStack::Remove (char* data, int len)
{
memset(data, 0, len);
m_sync.Enter ();
// if the buffer is empty, don't remove anything
if (m_length == 0)
{
m_sync.Leave ();
return 0;
}
// determine how much we will actually copy
int remlen = len;
if (remlen > m_length)
remlen = m_length;
// backup old buffer (temporarily)
char* oldbuff = new char[m_length];
memcpy(oldbuff, m_buffer, m_length);
// copy part of buffer (or all) to 'data'
memcpy(data, oldbuff, remlen);
// create a new buffer that holds the old one MINUS the removed stuff
delete[] m_buffer;
if (m_length - remlen <= 0)
{
m_buffer = NULL;
m_length = 0;
}
else
{
m_buffer = new char[m_length - remlen];
// remove the part that has been copied from the buffer
memcpy(m_buffer, &oldbuff[remlen], m_length - remlen);
// store the new length of the data
m_length -= remlen;
}
// do clean-up
delete[] oldbuff;
m_sync.Leave ();
return remlen;
}
int CConnection::CDataStack::Length ()
{
long length;
m_sync.Enter ();
length = m_length;
m_sync.Leave ();
return length;
}
CSync::CSync ()
{
m_sync = CreateMutex (NULL, false, NULL);
if (m_sync == NULL)
throw CError (1001);
}
CSync::~CSync()
{
if (m_sync != NULL)
{
CloseHandle (m_sync);
m_sync = NULL;
}
}
void CSync::Enter () const
{
WaitForSingleObject (m_sync, INFINITE);
}
void CSync::Leave () const
{
ReleaseMutex (m_sync);
}
CError::CError (long err)
{
m_number = err;
}
long CError::GetErrorString (char* str, long len)
{
static const long lErrCodes[] =
{
WSAEACCES,
WSAEADDRINUSE,
WSAEADDRNOTAVAIL,
WSAEAFNOSUPPORT,
WSAEALREADY,
WSAECONNABORTED,
WSAECONNREFUSED,
WSAECONNRESET,
WSAEDESTADDRREQ,
WSAEFAULT,
WSAEHOSTDOWN,
WSAEHOSTUNREACH,
WSAEINPROGRESS,
WSAEINTR,
WSAEINVAL,
WSAEISCONN,
WSAEMFILE,
WSAEMSGSIZE,
WSAENETDOWN,
WSAENETRESET,
WSAENETUNREACH,
WSAENOBUFS,
WSAENOPROTOOPT,
WSAENOTCONN,
WSAENOTSOCK,
WSAEOPNOTSUPP,
WSAEPFNOSUPPORT,
WSAEPROCLIM,
WSAEPROTONOSUPPORT,
WSAEPROTOTYPE,
WSAESHUTDOWN,
WSAESOCKTNOSUPPORT,
WSAETIMEDOUT,
WSAEWOULDBLOCK,
WSAHOST_NOT_FOUND,
WSANOTINITIALISED,
WSANO_DATA,
WSANO_RECOVERY,
WSASYSNOTREADY,
WSATRY_AGAIN,
WSAVERNOTSUPPORTED,
WSAEDISCON,
1001,
1002
};
static const char lpErrMsgs[][64] =
{
"Permission denied.",
"Address already in use.",
"Cannot assign requested address.",
"Address family not supported by protocol family.",
"Operation already in progress.",
"Software caused connection abort.",
"Connection refused.",
"Connection reset by peer.",
"Destination address required.",
"Bad address.",
"Host is down.",
"No route to host.",
"Operation now in progress.",
"Interrupted function call.",
"Invalid argument.",
"Socket is already connected.",
"Too many open sockets.",
"Message too long.",
"Network is down.",
"Network dropped connection on reset.",
"Network is unreachable.",
"No buffer space available.",
"Bad protocol option.",
"Socket is not connected.",
"Socket operation on nonsocket.",
"Operation not supported.",
"Protocol family not supported.",
"Too many processes.",
"Protocol not supported.",
"Protocol wrong type for socket.",
"Cannot send after socket shutdown.",
"Socket type not supported.",
"Connection timed out.",
"Resource temporarily unavailable.",
"Host not found.",
"Successful WSAStartup not yet performed.",
"Valid name, no data record of requested type.",
"This is a nonrecoverable error.",
"Network subsystem is unavailable.",
"Nonauthoritative host not found.",
"Winsock.dll version out of range.",
"Graceful shutdown in progress."
"Mutex not created.",
"Thread not created."
};
for (int i = 0; i < sizeof (lErrCodes) / sizeof (long); i++)
{
if (m_number == lErrCodes[i])
{
int slen = strlen (lpErrMsgs[i]);
if (len > slen + 1) len = slen + 1;
if (str) memcpy (str, lpErrMsgs[i], len);
return slen;
}
}
char lpUnknown[] = "Unknown error.";
int slen = strlen (lpUnknown);
if (len > slen + 1) len = slen + 1;
if (str) memcpy (str, lpUnknown, len);
return slen;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -