1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/* NewNet - A networking framework in C++
Copyright (C) 2006-2007 Ingmar K. Steen (iksteen@gmail.com)
Copyright 2008 little blue poney <lbponey@users.sourceforge.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "nnclientsocket.h"
#include "nnlog.h"
#include "platform.h"
#include <iostream>
void
NewNet::ClientSocket::disconnect(bool invoke)
{
if((socketState() == SocketUninitialized) || (descriptor() < 0))
{
NNLOG("newnet.net.warn", "Trying to disconnect an uninitialized client socket.");
if (invoke)
disconnectedEvent(this);
return;
}
closesocket(descriptor());
setSocketState(SocketDisconnected);
if (invoke)
disconnectedEvent(this);
}
void
NewNet::ClientSocket::process()
{
if(socketState() == SocketConnecting)
{
if(readyState() & StateSend)
{
sockopt_t so_error;
socklen_t so_len = sizeof(int);
getsockopt(descriptor(), SOL_SOCKET, SO_ERROR, &so_error, &so_len);
if(so_len != sizeof(int) || ! so_error)
{
NNLOG("newnet.net.debug", "Connected to host");
setSocketState(SocketConnected);
connectedEvent(this);
}
else
{
NNLOG("newnet.net.warn", "Cannot connect to host, error: %i.", so_error);
setSocketError(ErrorCannotConnect);
cannotConnectEvent(this);
return;
}
}
}
if(readyState() & StateException)
{
unsigned char buf;
ssize_t received = ::recv(descriptor(), (char *)&buf, 1, MSG_OOB);
if(received < 1)
{
NNLOG("newnet.net.warn", "Socket %u encountered error %i. Closing it.", descriptor(), errno);
closesocket(descriptor());
setSocketError(ErrorUnknown);
disconnectedEvent(this);
return;
}
m_ReceiveBuffer.append(&buf, 1);
dataReceivedEvent(this);
}
if(readyState() & StateReceive)
{
unsigned char buf[1024];
ssize_t received = ::recv(descriptor(), (char *)buf, 1024, 0);
if(received == -1)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
NNLOG("newnet.net.debug", "EAGAIN while receiving data on socket %i.", descriptor());
setReadyState(readyState() & ~StateReceive);
}
else
{
NNLOG("newnet.net.warn", "Socket %u encountered error %i. Closing it.", descriptor(), errno);
closesocket(descriptor());
setSocketError(ErrorUnknown);
disconnectedEvent(this);
return;
}
}
else if(received == 0)
{
NNLOG("newnet.net.debug", "Socket %u was disconnected.", descriptor());
closesocket(descriptor());
setSocketState(SocketDisconnected);
disconnectedEvent(this);
return;
}
else
{
NNLOG("newnet.net.debug", "Received %i bytes on socket %u.", received, descriptor());
if(downRateLimiter())
downRateLimiter()->transferred(received);
m_ReceiveBuffer.append(buf, received);
dataReceivedEvent(this);
}
}
if((readyState() & StateSend) && dataWaiting())
{
size_t n = std::min((size_t)1024, m_SendBuffer.count());
ssize_t sent = ::send(descriptor(), (const char *)m_SendBuffer.data(), n, 0);
if(sent < 0)
{
if(errno == EAGAIN)
setReadyState(readyState() & ~StateSend);
else
{
NNLOG("newnet.net.warn", "Socket %u encountered error %i. Closing it.", descriptor(), errno);
closesocket(descriptor());
setSocketError(ErrorUnknown);
disconnectedEvent(this);
return;
}
}
else {
NNLOG("newnet.net.debug", "Sent %i bytes to socket %u.", sent, descriptor());
if(upRateLimiter())
upRateLimiter()->transferred(sent);
m_SendBuffer.seek(sent);
setDataWaiting(m_SendBuffer.count() != 0);
dataSentEvent(this);
}
}
}
|