//
// Asura_Platform_NetSocket.cpp
//
// PS3 version
//
// Created by MH - 3.12.2007
//
// Copyright (C) 2004 Rebellion, All Rights Reserved.
//

#include "Core/Asura.h"

#ifdef ASURA_SUPPORT_EXTERNAL_NETWORK

// This file's header.
#include "Network/Asura_NetSocket.h"

// Asura headers
#include "Core/Memory/Asura_Mem.h"
#include "Network/Asura_NetSocket_BSD.h"

// (MH - 19.07.2004) these are platform functions in order to work around funny packet headers on the
// XBox (used for Voice Data) and the XBox limit of 1304 bytes per sendto call. On PC/PS2/PS3 we don't need
// to fiddle about with headers or impose a limit (the independent code should keep packet sizes down 
// below about 1kb anyway).
bool Asura_NetSocket::Platform_SendTo(const Asura_NetSocket_MessageInfo* pxMessageInfo)
{
	Asura_NetSocket_BSD_AddressUnion xAddress;
	Asura_Mem::Set(&xAddress, 0, sizeof(SOCKADDR_IN));
	xAddress.xSockAddrIn.sin_family = AF_INET;
	xAddress.xSockAddrIn.sin_addr.s_addr = pxMessageInfo->m_xAddress;
	xAddress.xSockAddrIn.sin_port = htons(pxMessageInfo->m_usPort);
	// fix up the special PS3 vport member
	ASURA_NETSOCKET_FIXUPSOCKADDRIN(xAddress.xSockAddrIn);

	return sendto(m_xSocket, pxMessageInfo->m_pcMessage, pxMessageInfo->m_iLength, 0, &xAddress.xSockAddr, sizeof(SOCKADDR_IN)) > SOCKET_ERROR;
}

bool Asura_NetSocket::Platform_GetData(const char*& pcMessage, int& iLength)
{
	return true;
}

bool Asura_NetSocket::Platform_SetToNonBlocking()
{
	int iNonBlocking = 1;
	int iResult = setsockopt (m_xSocket, SOL_SOCKET, SO_NBIO, &iNonBlocking, sizeof(int));
    Asura_Assert( iResult > SOCKET_ERROR, "Error setting socket to non blocking" );
	return iResult > SOCKET_ERROR;
}

// needed for ps2 only (where it might sleep the thread).
void Asura_NetSocket::Platform_Update()
{
}

// if Asura_NetAddress is u_int this function is easy, just return it. On PSP things are harder...
u_int Asura_NetSocket::Platform_GetINetAddress( const Asura_NetAddress& xAddress )
{
	return xAddress;
}


#endif // #ifdef ASURA_SUPPORT_EXTERNAL_NETWORK
