/*<<<***************************************************************************
The MIT License (MIT)

Copyright(C) 2013 - 2015 Marc Mirti Mancinelli, Christophe Lardau and Philippe Pamart (xdcube.org)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
#Title: xbSocketItf
#Project: xdcBase
////////////////////////////////////////////////////////////////////////////////
#Author: Marc MIRTI MANCINELLI
#History:
<15/09/2013: creation.
12/02/2014: this notice inserted.
***************************************************************************>>>*/
#include "stdafx.h"

#include "xbSocketItf.h"

#include "_xbVer.h"

xbEimpl(xbEsocket,xbEadv);
xbEimpl(xbEsocketUser,xbEsocket);

xbMsg			xbWMCTRL_WINSOCK_NOTIF("xbWMCTRL_WINSOCK_NOTIF");

xbHotPlugType	 g_xbSocketPlugType("Socket",XDCBASE_HSVER,"xdcBase HotPlug Socket",xbEngines(&xbWMCTRL_WINSOCK_NOTIF));

int xbSocketGetPort(const xbString &szPort,xbString *pRes,xbOpMode opMode)
{
	xbString port;
	int pos = szPort.LastIndexOf('.');
	if (pos >= 0)
	{
		if (pRes) 
			*pRes = xbString(szPort,pos);
		port = xbString(((LPCSTR)szPort)+pos+1);
	}
	else
	{
		if (pRes) pRes->Empty();
		port = szPort;
	}

	int nPort = ::xbStrToInt(port);
	if (nPort <= 0 || nPort > 0xFFFF)
	{
		if (opMode != xbOp_Required) 
			return -1;

		xbThrow xbEsocketUser("Invalid port number ('"+szPort+"')");
	}
	return nPort;
}
