Exemple #1
0
void 
NetServer::onStateMessage( MessagePtr m )
{
	K_ASSERT( m.Get() != 0 );

	NetStateMessage* nsm = static_cast<NetStateMessage*>( m.Get() );

	switch ( nsm->state )
	{
	case NetStateMessage::TCP_CLOSED:
		{
			NetGroupMap::iterator i = m_groups.find( nsm->groupId );

			if ( i != m_groups.end() )
			{
				NetGroup* group = i->second;

				if ( group != 0 )
				{
					group->Leave( nsm->connectionId );
				}
			}	
		}
		break;
	}

	m_listener->Notify( m );
}
Exemple #2
0
void 
NetClient::Send( MessagePtr m )
{
	K_ASSERT( m.Get() != 0 );

	m_sendQ.Put( m );
}
Exemple #3
0
void 
NetServer::Send( MessagePtr m )
{
	K_ASSERT( m.Get() != 0 );

	m_sendQ.Put( m );
}
Exemple #4
0
void 
NetClient::onControl( MessagePtr m )
{
	NetControlMessage* cm = static_cast<NetControlMessage*>( m.Get() );

	switch ( cm->control )
	{
	case NetControlMessage::TCP_CONNECT:
		{
			m_tcp.Connect( cm->remote );
		}
		break;
	case NetControlMessage::TCP_LISTEN:
		{
			m_tcp.Listen( cm->remote, cm->sl );
		}
		break;
	case NetControlMessage::TCP_CLOSE:
		{
			m_tcp.Close( cm->connectionId );
			m_udp.CheckRelay( cm->connectionId );
		}
		break;
	}
}
Exemple #5
0
void 
NetClient::Notify( MessagePtr m )
{
	K_ASSERT( m.Get() != 0 );

	m_recvQ.Put( m );
}
Exemple #6
0
void 
NetClient::onGroupPrepare( MessagePtr m )
{
	K_ASSERT( m_groupId == 0 );
	K_ASSERT( m_selfTag == 0 );

	m_groupId = 0;
	m_selfTag = 0;

	NmGroupPrepare* gp = static_cast<NmGroupPrepare*>( m.Get() );

	TcpConnection* c = m_tcp.FindById( gp->remote );

	if ( c == 0 )
	{
		LOG( FT_WARN, 
			 _T("NetClient::onGroupPrepare> Connection %d not found"), 
			 gp->remote );

		return;
	}
	
	m_udp.Fini();

	m_groupId = gp->groupId;
	m_selfTag = gp->connectionId;

	// init udp with c as a relay connection 
	// use same ip:port as TCP connection
	bool rc = m_udp.Init( this, 
						  &m_ios, 
						  c->GetSocket()->GetAddress(), 
						  m_selfTag, 
						  gp->sl, 
						  gp->challenge, 
						  c );

	if ( !rc )
	{
		LOG( FT_WARN, _T("NetClient::onGroupPrepare> Failed to init udp") );

		return;
	}

	NmGroupPrepared* p = new NmGroupPrepared;

	p->remote 		= gp->remote;
	p->groupId 		= gp->groupId;
	p->connectionId = gp->connectionId;
	p->in 			= c->GetSocket()->GetAddress();

	m_tcp.Send( m->remote, MessagePtr( p ) );

	LOG( FT_DEBUG, _T("NetClient::onGroupPrepare> Prepared group %d connection %d"), 
		 gp->groupId, 
		 gp->connectionId );
}
Exemple #7
0
void 
NetClient::onGroupJoin( MessagePtr m )
{
	K_ASSERT( m_groupId != 0 );

 	NmGroupJoin* join = static_cast<NmGroupJoin*>( m.Get() );

	K_ASSERT( join->groupId == m_groupId );

	LOG( FT_DEBUG, 
		 _T("NetClient::onGroupJoin> Group %d Members %d"), 
		 join->groupId, 
		 join->members.size() );

	NmGroupJoin::MemberList::iterator i( join->members.begin() );
	NmGroupJoin::MemberList::iterator iEnd( join->members.end() );

	for ( ; i != iEnd; ++i )
	{
		NetGroupMember& member = *i;

		if ( member.connectionId != m_selfTag )
		{
			UdpConnection* c = m_udp.FindByTag( member.connectionId );

			if ( c != 0 )
			{
				continue;
			}

			// tag is used for communication
			(void)m_udp.Connect( member.connectionId, member.in, member.ex ); 

			NmGroupJoined* joined = new NmGroupJoined;

			joined->groupId 	 = join->groupId;
			joined->connectionId = member.connectionId;
			joined->extra		 = member.extra;

			m_listener->Notify( MessagePtr( joined ) );

			LOG( FT_DEBUG, 
				_T("NetClient::onGroupJoin> Self %d Tag %d joined In %s Ex %s"), 
				m_selfTag,
				member.connectionId, 
				member.in.ToString().c_str(), 
				member.ex.ToString().c_str() );
		}
	}
}
Exemple #8
0
void 
NetServer::Notify( MessagePtr m )
{
	K_ASSERT( m.Get() != 0 );
	
	switch ( m->type )
	{
	case NET_CONTROL_MESSAGE:
	case NET_GROUP_PREPARED:
	case NET_STATE_MESSAGE:
		m_recvQ.Put( m );
		break;
	default:
		m_listener->Notify( m );
		break;
	}
}
Exemple #9
0
void 
NetServer::onGroupPrepared( MessagePtr m )
{
	NmGroupPrepared* gp = static_cast<NmGroupPrepared*>( m.Get() );

	NetGroupMap::iterator i = m_groups.find( gp->groupId );

	if ( i == m_groups.end() )
	{
		LOG( FT_WARN, _T("NetServer::onGroupPrepared> %d not found"), gp->groupId );

		// TODO: error report

		return;
	}	

	NetGroup* group = i->second;
	K_ASSERT( group != 0 );

	group->OnPrepared( gp->connectionId, gp->in );
}
Exemple #10
0
void 
NetClient::onGroupLeave( MessagePtr m )
{
	K_ASSERT( m_groupId != 0 );

	NmGroupLeave* leave = static_cast<NmGroupLeave*>( m.Get() );

	m_udp.CloseByTag( leave->connectionId );			

	if ( leave->connectionId == m_selfTag )
	{
		m_groupId = 0;
		m_selfTag = 0;

		m_udp.Fini(); // finish by left the group by myself.
	}

	LOG( FT_DEBUG, 
		 _T("NetClient::onGroupLeave> Tag %d left"), 
		 leave->connectionId );

	m_listener->Notify( m );
}