25 #if defined(_DEBUG) && defined(_WINDOWS)
26 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
34 template class ZENI_NET_DLL Singleton<Net>;
40 Singleton<Net>::Uninit Net::g_uninit;
41 Singleton<Net>::Reinit Net::g_reinit;
50 throw Net_Init_Failure();
52 cr.lend_pre_uninit(&g_uninit);
53 cr.lend_post_reinit(&g_reinit);
80 #pragma warning( push )
81 #pragma warning( disable : 4355 )
85 #pragma warning(
pop )
112 #pragma warning( push )
113 #pragma warning( disable : 4355 )
117 #pragma warning(
pop )
138 if(sock && sockset) {
163 return SDLNet_TCP_Send(sock, const_cast<void *>(data), num_bytes) < num_bytes ? -1 : 0;
193 data.
resize(
size_t(num_bytes));
195 const int retval =
receive(const_cast<char *>(data.
c_str()), num_bytes);
199 data.
resize(
size_t(retval));
221 void TCP_Socket::Uninit::operator()() {
232 TCP_Listener::TCP_Listener(
const Uint16 &port)
235 #pragma warning( push )
236 #pragma warning( disable : 4355 )
240 #pragma warning(
pop )
262 void TCP_Listener::Uninit::operator()() {
274 UDP_Socket::UDP_Socket(
const Uint16 &port)
277 #pragma warning( push )
278 #pragma warning( disable : 4355 )
282 #pragma warning(
pop )
306 if(num_bytes < 8167
u) {
310 reinterpret_cast<Uint8 *
>(
const_cast<void *
>(
data)),
336 reinterpret_cast<Uint8 *
>(
const_cast<void *
>(
data)),
359 if(
int(data.
size()) > retval) {
360 data[
static_cast<unsigned int>(retval)] =
'\0';
361 data.
resize(
size_t(retval));
367 void UDP_Socket::Uninit::operator()() {
375 Split_UDP_Socket::Chunk_Set::Chunk_Set(
const IPaddress &sender,
376 const Nonce &incoming,
385 assert(num_chunks > which);
387 if(num_chunks > which)
388 chunks[which] =
chunk;
391 bool Split_UDP_Socket::Chunk_Set::add_chunk(
const IPaddress &sender,
392 const Nonce &incoming,
398 chunks.size() == num_chunks &&
399 chunks.size() > which)
401 if(!chunks[which].
data)
402 chunks[which] =
chunk;
410 bool Split_UDP_Socket::Chunk_Set::complete()
const {
411 return chunks_arrived == chunks.size();
423 for(std::vector<Chunk>::const_iterator it = chunks.begin(); it != chunks.end(); ++it)
424 packet.size += it->size;
426 char *
dst =
new char [packet.size];
429 for(std::vector<Chunk>::const_iterator it = chunks.begin(); it != chunks.end(); ++it) {
437 const Split_UDP_Socket::Chunk_Set * Split_UDP_Socket::Chunk_Collector::add_chunk(
const IPaddress &sender,
438 const Nonce &incoming,
443 for(std::list<Chunk_Set *>::iterator it = chunk_sets.begin(); it != chunk_sets.end(); ++it) {
444 if((*it)->add_chunk(sender, incoming, num_chunks, which, chunk)) {
445 if((*it)->complete())
452 if(
int(chunk_sets.size()) == m_size)
453 chunk_sets.pop_back();
456 Chunk_Set *cs =
new Chunk_Set(sender, incoming, num_chunks, which, chunk);
457 chunk_sets.push_front(cs);
465 Split_UDP_Socket::Split_UDP_Socket(
const Uint16 &port,
const Uint16 &chunk_sets,
const Uint16 &chunk_size)
467 m_chunk_size(chunk_size),
468 m_chunk_collector(chunk_sets)
477 const Uint16 split_size =
Uint16(m_chunk_size - offset);
478 const Uint16 num_full_chunks =
Uint16(num_bytes / split_size);
479 const Uint16 partial_chunk =
Uint16(num_bytes % split_size);
480 const Uint16 num_chunks = num_full_chunks + (partial_chunk ? 1
u : 0
u);
482 const char *ptr =
reinterpret_cast<const char *
>(
data);
483 for(
Uint16 chunk = 0; chunk < num_full_chunks; ++
chunk, ptr += split_size) {
487 std::ostringstream os;
493 memcpy(const_cast<char *>(s.
c_str()) + offset, ptr, split_size);
502 std::ostringstream os;
507 s.
resize(
size_t(offset + partial_chunk));
508 memcpy(const_cast<char *>(s.
c_str()) + offset, ptr, partial_chunk);
519 for(
int retval = -1; retval;) {
531 std::istringstream is(s.
std_str());
540 chunk.data =
new char [chunk.size];
544 const Chunk_Set * cs = m_chunk_collector.add_chunk(ip, nonce, num_chunks, which, chunk);
548 Chunk packet = cs->receive();
549 if(num_bytes >=
Uint16(packet.size)) {
550 memcpy(const_cast<void *>(data), packet.data, packet.size);
551 return int(packet.size);
564 if(
int(data.
size()) > retval) {
565 data[
static_cast<unsigned int>(retval)] =
'\0';
566 data.
resize(
size_t(retval));
DECLSPEC int SDLCALL SDLNet_TCP_Send(TCPsocket sock, const void *data, int len)
String reverse_lookup(IPaddress ip)
If you want to find a URL associated with an IP address.
DECLSPEC int SDLCALL SDLNet_CheckSockets(SDLNet_SocketSet set, Uint32 timeout)
#define SDLNet_Write16(value, areap)
else Out of place iCCP chunk
DECLSPEC UDPsocket SDLCALL SDLNet_UDP_Open(Uint16 port)
DECLSPEC void SDLCALL SDLNet_FreeSocketSet(SDLNet_SocketSet set)
DECLSPEC int SDLCALL SDLNet_Init(void)
virtual std::istream & unserialize(std::istream &is)
const char * c_str() const
std::istream & unserialize(std::istream &is, Color &value)
IPaddress resolve_host(const String &host, const Uint16 &port=0)
Default port 0 indicates a pure lookup with no intention of connecting.
std::ostream & serialize(std::ostream &os, const Color &value)
void resize(size_t n, char c)
static void remove_post_reinit(Event::Handler *const &handler)
DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Accept(TCPsocket server)
Net & get_Net()
Get access to the singleton.
void lend_pre_uninit(Event::Handler *const &handler)
DECLSPEC IPaddress *SDLCALL SDLNet_TCP_GetPeerAddress(TCPsocket sock)
IPaddress peer_address() const
static __inline__ int SDLNet_TCP_AddSocket(SDLNet_SocketSet set, TCPsocket sock)
static void remove_pre_uninit(Event::Handler *const &handler)
virtual int receive(IPaddress &ip, const void *const &data, const Uint16 &num_bytes)
Receive data of up to data.size() from the returned IPaddress; Will error if num_bytes/data.size() is too low.
A UDP Socket for sending and receiving data.
IPaddress peer_address() const
Apparently only works if the port was explicitly specified.
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
TCPsocket accept()
Returns 0 if no listener.
DECLSPEC TCPsocket SDLCALL SDLNet_TCP_Open(IPaddress *ip)
virtual int receive(IPaddress &ip, const void *const &data, const Uint16 &num_bytes)
Receive data of up to data.size() from the returned IPaddress; Will error if num_bytes/data.size() is too low.
void send(const void *const &data, const Uint16 &num_bytes)
virtual void send(const IPaddress &ip, const void *const &data, const Uint16 &num_bytes)
Send data to an IPaddress.
DECLSPEC const char *SDLCALL SDLNet_ResolveIP(const IPaddress *ip)
virtual void send(const IPaddress &ip, const void *const &data, const Uint16 &num_bytes)
Send data to an IPaddress.
DECLSPEC void SDLCALL SDLNet_Quit(void)
DECLSPEC int SDLCALL SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet)
int receive(void *const &data, const Uint16 &num_bytes)
Variable Length Unique IDentifier.
DECLSPEC int SDLCALL SDLNet_ResolveHost(IPaddress *address, const char *host, Uint16 port)
struct _TCPsocket * TCPsocket
uint8_t Uint8
An unsigned 8-bit integer type.
static __inline__ int SDLNet_TCP_DelSocket(SDLNet_SocketSet set, TCPsocket sock)
virtual std::ostream & serialize(std::ostream &os) const
Core & get_Core()
Get access to the singleton.
DECLSPEC void SDLCALL SDLNet_UDP_Close(UDPsocket sock)
DECLSPEC IPaddress *SDLCALL SDLNet_UDP_GetPeerAddress(UDPsocket sock, int channel)
int try_send(const void *const &data, const Uint16 &num_bytes)
Send data.
std::string std_str() const
DECLSPEC SDLNet_SocketSet SDLCALL SDLNet_AllocSocketSet(int maxsockets)
DECLSPEC int SDLCALL SDLNet_TCP_Recv(TCPsocket sock, void *data, int maxlen)
uint16_t Uint16
An unsigned 16-bit integer type.
int try_receive(void *const &data, const Uint16 &num_bytes)
Receive up to num_bytes.
DECLSPEC void SDLCALL SDLNet_TCP_Close(TCPsocket sock)
DECLSPEC int SDLCALL SDLNet_UDP_Recv(UDPsocket sock, UDPpacket *packet)