2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2004-07-04 17:41:17 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#include "Network.h"
|
|
|
|
#include "StreamSocket.h"
|
|
|
|
|
2005-02-21 18:13:31 +01:00
|
|
|
#include "NetLog.h"
|
2004-11-12 23:11:04 +01:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
CStreamSocket::CStreamSocket()
|
|
|
|
{}
|
|
|
|
|
|
|
|
CStreamSocket::CStreamSocket(CSocketInternal *pInt):
|
|
|
|
CSocketBase(pInt)
|
|
|
|
{}
|
|
|
|
|
|
|
|
CStreamSocket::~CStreamSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void *CStreamSocket_ConnectThread(void *data)
|
|
|
|
{
|
2008-09-27 12:05:11 +02:00
|
|
|
debug_SetThreadName("net_connect");
|
2005-08-09 17:55:44 +02:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
CStreamSocket *pSock=(CStreamSocket *)data;
|
|
|
|
PS_RESULT res=PS_OK;
|
2004-03-08 03:11:41 +01:00
|
|
|
CSocketAddress addr;
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2004-03-08 03:11:41 +01:00
|
|
|
res=CSocketAddress::Resolve(pSock->m_pConnectHost, pSock->m_ConnectPort, addr);
|
2007-10-13 17:17:50 +02:00
|
|
|
NET_LOG4("CStreamSocket_ConnectThread: Resolve: %s -> %s [%s]", pSock->m_pConnectHost, addr.GetString().c_str(), res);
|
2003-11-25 03:11:50 +01:00
|
|
|
if (res == PS_OK)
|
|
|
|
{
|
|
|
|
pSock->Initialize();
|
2005-02-21 18:13:31 +01:00
|
|
|
// If we don't do this we'll get spurious callbacks called since our
|
|
|
|
// network thread will notice the socket getting connected (and
|
|
|
|
// potentially receiving data) while we might not yet have called the
|
|
|
|
// ConnectComplete callback
|
|
|
|
pSock->SetOpMask(0);
|
2007-07-09 07:10:02 +02:00
|
|
|
pSock->SetNonBlocking(false);
|
2003-11-25 03:11:50 +01:00
|
|
|
res=pSock->Connect(addr);
|
2007-10-13 17:17:50 +02:00
|
|
|
NET_LOG2("CStreamSocket_ConnectThread: Connect: %s", res);
|
2003-11-25 03:11:50 +01:00
|
|
|
}
|
|
|
|
|
2004-03-08 03:11:41 +01:00
|
|
|
if (res == PS_OK)
|
|
|
|
{
|
|
|
|
pSock->SetNonBlocking(true);
|
2003-11-30 18:00:27 +01:00
|
|
|
|
2004-03-08 03:11:41 +01:00
|
|
|
// This should call the right callbacks, so that you get the expected
|
|
|
|
// results if you call Read or Write before the connect actually is complete
|
|
|
|
pSock->SetOpMask((pSock->m_WriteContext.m_Valid?CSocketBase::WRITE:0)|CSocketBase::READ);
|
|
|
|
}
|
2003-11-30 18:00:27 +01:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
pSock->ConnectComplete(res);
|
|
|
|
|
|
|
|
free(pSock->m_pConnectHost);
|
2005-02-21 18:13:31 +01:00
|
|
|
pSock->m_pConnectHost=NULL;
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
PS_RESULT CStreamSocket::BeginConnect(const char *hostname, int port)
|
|
|
|
{
|
|
|
|
m_pConnectHost=strdup(hostname);
|
|
|
|
m_ConnectPort=port;
|
|
|
|
|
|
|
|
// Start thread
|
|
|
|
pthread_t thread;
|
|
|
|
pthread_create(&thread, NULL, &CStreamSocket_ConnectThread, this);
|
|
|
|
|
|
|
|
return PS_OK;
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
PS_RESULT CStreamSocket::Read(void *buf, size_t len)
|
2003-11-25 03:11:50 +01:00
|
|
|
{
|
|
|
|
// Check socket status
|
|
|
|
if (GetState() != SS_CONNECTED)
|
|
|
|
return GetErrorState();
|
|
|
|
|
|
|
|
// Check for running read operation
|
|
|
|
if (m_ReadContext.m_Valid)
|
|
|
|
return CONFLICTING_OP_IN_PROGRESS;
|
|
|
|
|
|
|
|
// Fill in read_cb
|
|
|
|
m_ReadContext.m_Valid=true;
|
|
|
|
m_ReadContext.m_pBuffer=buf;
|
|
|
|
m_ReadContext.m_Length=len;
|
|
|
|
m_ReadContext.m_Completed=0;
|
|
|
|
|
|
|
|
SetOpMask(GetOpMask()|READ);
|
|
|
|
|
|
|
|
return PS_OK;
|
|
|
|
}
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
PS_RESULT CStreamSocket::Write(void *buf, size_t len)
|
2003-11-25 03:11:50 +01:00
|
|
|
{
|
|
|
|
// Check status
|
|
|
|
if (GetState() != SS_CONNECTED)
|
|
|
|
return GetErrorState();
|
|
|
|
|
|
|
|
// Check running Write operation
|
|
|
|
if (m_WriteContext.m_Valid)
|
|
|
|
return CONFLICTING_OP_IN_PROGRESS;
|
|
|
|
|
|
|
|
// Fill in read_cb
|
|
|
|
m_WriteContext.m_pBuffer=buf;
|
|
|
|
m_WriteContext.m_Length=len;
|
|
|
|
m_WriteContext.m_Completed=0;
|
2003-11-30 18:00:27 +01:00
|
|
|
m_WriteContext.m_Valid=true;
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
SetOpMask(GetOpMask()|WRITE);
|
|
|
|
|
|
|
|
return PS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MakeDefaultCallback(_nm) void CStreamSocket::_nm(PS_RESULT error) \
|
2007-10-13 17:17:50 +02:00
|
|
|
{ NET_LOG2("CStreamSocket::"#_nm"(): %s", error); }
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2004-03-08 03:11:41 +01:00
|
|
|
MakeDefaultCallback(OnClose)
|
2003-11-25 03:11:50 +01:00
|
|
|
MakeDefaultCallback(ConnectComplete)
|
|
|
|
MakeDefaultCallback(ReadComplete)
|
|
|
|
MakeDefaultCallback(WriteComplete)
|
|
|
|
|
|
|
|
void CStreamSocket::OnWrite()
|
|
|
|
{
|
|
|
|
if (!m_WriteContext.m_Valid)
|
|
|
|
{
|
|
|
|
SetOpMask(GetOpMask() & (~WRITE));
|
|
|
|
return;
|
|
|
|
}
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t bytes=0;
|
2004-03-08 03:11:41 +01:00
|
|
|
PS_RESULT res=CSocketBase::Write(
|
|
|
|
((char *)m_WriteContext.m_pBuffer)+m_WriteContext.m_Completed,
|
|
|
|
m_WriteContext.m_Length-m_WriteContext.m_Completed,
|
|
|
|
&bytes);
|
2003-11-25 03:11:50 +01:00
|
|
|
if (res != PS_OK)
|
|
|
|
{
|
|
|
|
WriteComplete(res);
|
|
|
|
return;
|
|
|
|
}
|
2007-10-13 17:17:50 +02:00
|
|
|
NET_LOG2("CStreamSocket::OnWrite(): %u bytes", bytes);
|
2003-11-25 03:11:50 +01:00
|
|
|
m_WriteContext.m_Completed+=bytes;
|
|
|
|
if (m_WriteContext.m_Completed == m_WriteContext.m_Length)
|
|
|
|
{
|
|
|
|
m_WriteContext.m_Valid=false;
|
|
|
|
WriteComplete(PS_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamSocket::OnRead()
|
|
|
|
{
|
|
|
|
if (!m_ReadContext.m_Valid)
|
|
|
|
{
|
2005-02-21 18:13:31 +01:00
|
|
|
NET_LOG("CStreamSocket::OnRead(): No Read request in progress");
|
2003-11-25 03:11:50 +01:00
|
|
|
return;
|
|
|
|
}
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
size_t bytes=0;
|
2003-11-25 03:11:50 +01:00
|
|
|
PS_RESULT res=CSocketBase::Read(
|
2005-02-21 18:13:31 +01:00
|
|
|
((u8 *)m_ReadContext.m_pBuffer)+m_ReadContext.m_Completed,
|
2003-11-25 03:11:50 +01:00
|
|
|
m_ReadContext.m_Length-m_ReadContext.m_Completed,
|
|
|
|
&bytes);
|
2007-10-13 17:17:50 +02:00
|
|
|
NET_LOG4("CStreamSocket::OnRead(): %s, %u bytes read of %u",
|
|
|
|
res, bytes,
|
|
|
|
m_ReadContext.m_Length-m_ReadContext.m_Completed);
|
2003-11-25 03:11:50 +01:00
|
|
|
if (res != PS_OK)
|
|
|
|
{
|
|
|
|
ReadComplete(res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_ReadContext.m_Completed+=bytes;
|
|
|
|
if (m_ReadContext.m_Completed == m_ReadContext.m_Length)
|
|
|
|
{
|
|
|
|
m_ReadContext.m_Valid=false;
|
|
|
|
ReadComplete(PS_OK);
|
|
|
|
}
|
|
|
|
}
|