예제 #1
0
bool GChat::SendToSelected(const GString &msg,const list<UINT32> &in_iSelectedPlayer)
{

   SDK::GGameEventSPtr l_event = CREATE_GAME_EVENT(SDK::Event::GChatEvent);
   l_event->m_iSource = g_Joshua.Client()->Id();
   ((SDK::Event::GChatEvent *)l_event.get())->Message(msg);   
  
   // Private Message
   if(in_iSelectedPlayer.size() > 0)
   {
      ((SDK::Event::GChatEvent *)l_event.get())->Private(true);
      list<UINT32>::const_iterator l_playerIt = in_iSelectedPlayer.begin();
      while(l_playerIt != in_iSelectedPlayer.end())
      {
         if(*l_playerIt != (UINT32)g_Joshua.Client()->Id())
         {
            l_event->m_iTarget = *l_playerIt;
            g_Joshua.RaiseEvent(l_event); 
         }
         l_playerIt++;
      }

      // See your own message
      AddSentMessage(g_Joshua.Client()->Id(), msg,true);

   }
   // Broadcast to all
   else
   {
      ((SDK::Event::GChatEvent *)l_event.get())->Private(false);
	   l_event->m_iTarget = SDK::Event::ESpecialTargets::BroadcastHumanPlayers;
	   g_Joshua.RaiseEvent(l_event);
      AddSentMessage(g_Joshua.Client()->Id(), msg,false);
   }

   
   return true;
}
/**
 * send MESSAGE packet to SIP server
 */
int CPocketSMDlg::SendSIPMessage(CString to, CString body, int nr)
{
	CString strBuf, strPort, callID;
	CString servAddress;
	UINT servPort;
	char outBuf[2048], pBuf[1024], callIDBuf[50];
	int i, l, n;
	CTime t = CTime::GetCurrentTime();

	if(!m_bSetupOK)
		return -1;

	strPort.Format(_T("%d"), m_nIPPort);
	UpdateCallID();
	callID.Format(_T("8923PkSM@%s.%08lX-%X"), m_strIPAddress, t.GetTime(), m_nCallID);
	l = callID.GetLength();
	for(i=0; i<l && i < 49; i++)
		callIDBuf[i] = (char)(callID.GetAt(i) & 0xFF);
	callIDBuf[i] = 0;

	strBuf="MESSAGE sip:";
	strBuf += to;

	strBuf += " SIP/2.0\r\nVia: SIP/2.0/UDP "+m_strIPAddress+":"+strPort;
    strBuf += "\r\nFrom: <sip:"+m_strUserName;
    strBuf += ">;tag="+m_strFTag;
    strBuf += "\r\nTo: <sip:"+to;
    strBuf += ">\r\nCall-ID: "+callID;
    strBuf += "\r\nCSeq: 1 MESSAGE\r\nContact: <sip:"
		+m_strUserName+">\r\nContent-Type: text/plain; charset=UTF-8";
    strBuf += "\r\nUser-Agent: PocketSipM v0.1\r\nContent-Length: ";
	strPort.Format(_T("%d"), body.GetLength());
	strBuf += strPort+"\r\n\r\n";
	strBuf += body;

	// convert from UNICODE to ASCII
	l = strBuf.GetLength();
	for(i=0; i<l; i++)
		outBuf[i] = (char)(strBuf.GetAt(i) & 0xFF);
	
	n = 1;
	/*** clean up the socket buffer ***/
	while(m_pCSock->ReceiveFrom((void*)pBuf, 2047, servAddress, servPort) > 0)
		Sleep(10);
	while(n < nr+1)
	{
		i = m_pCSock->SendTo(outBuf, l, m_nSipSrvPort, (LPCTSTR)m_strSipSrvAddress);
		if(i == SOCKET_ERROR || i != l)
			return -1;
		Sleep(RET_MILISEC);
		i = m_pCSock->ReceiveFrom((void*)pBuf, 1023, servAddress, servPort);
		if(i > 0)
		{
			pBuf[i] = 0;
			if(!strncmp(pBuf, "SIP/2.0 ", 8) && strstr(pBuf, callIDBuf))
			{
				if(*(pBuf+8) == '2')
				{
					AddSentMessage(to, body);
					UpdateDisplayedMessage();
					return 0;
				}
				if(*(pBuf+8) == '3' || *(pBuf+8) == '4' 
						|| *(pBuf+8) == '5' || *(pBuf+8) == '6')
					return -1;
			}
		}
		n++;
	}
	return -1;
}