Exemplo n.º 1
0
err_t TcpConnection::staticOnReceive(void *arg, tcp_pcb *tcp, pbuf *p, err_t err)
{
//	debugf("Static OnReceive buf = %d", tcp_sndbuf(tcp));
	TcpConnection* con = (TcpConnection*)arg;
	err_t ret_err;
	//Serial.println("echo_recv!");

	if (con == NULL)
	{
		if (p != NULL)
		{
		  /* Inform TCP that we have taken the data. */
		  tcp_recved(tcp, p->tot_len);
		  pbuf_free(p);
		}
		closeTcpConnection(tcp);
		return ERR_OK;
	}
	else
		con->sleep = 0;

	if (err != ERR_OK /*&& err != ERR_CLSD && err != ERR_RST*/)
	{
		debugf("Received ERROR %d", err);
		/* exit and free resources, for unkown reason */
		if (p != NULL)
		{
		  /* Inform TCP that we have taken the data. */
		  tcp_recved(tcp, p->tot_len);
		  pbuf_free(p);
		}
		closeTcpConnection(tcp); // ??
		con->tcp = NULL;
		con->onError(err);
		//con->close();
		return err == ERR_ABRT ? ERR_ABRT : ERR_OK;
	}

	//if (tcp != NULL && tcp->state == ESTABLISHED) // If active
	/* We have taken the data. */
	if (p != NULL)
		tcp_recved(tcp, p->tot_len);

	err_t res = con->onReceive(p);

	if (p != NULL)
		pbuf_free(p);
	else
	{
		con->close();
		closeTcpConnection(tcp);
	}

	con->checkSelfFree();
	//debugf("<staticOnReceive");
	return res;
}
Exemplo n.º 2
0
void TcpConnection::staticDnsResponse(const char *name, ip_addr_t *ipaddr, void *arg)
{
	DnsLookup* dlook = (DnsLookup*)arg;
	if (dlook == NULL) return;

	if (ipaddr != NULL)
	{
		IPAddress ip = *ipaddr;
		debugf("DNS record found: %s = %d.%d.%d.%d",
				name, ip[0], ip[1], ip[2], ip[3]);

		dlook->con->internalTcpConnect(ip, dlook->port);
	}
	else
	{
		#ifdef NETWORK_DEBUG
		debugf("DNS record _not_ found: %s", name);
		#endif

		closeTcpConnection(dlook->con->tcp);
		dlook->con->tcp = NULL;
		dlook->con->close();
	}

	delete dlook;
}
Exemplo n.º 3
0
void NetworkMgr::_handleTcpError(QAbstractSocket::SocketError e)
{
    std::cerr << "TCP Error " << qint32(e) << std::endl;
    if (_connState == STATE_AUTHED)
        _window->handleServerConnectionLost(e, _tcpSock.errorString());
    closeTcpConnection();
}
Exemplo n.º 4
0
err_t TcpConnection::staticOnPoll(void *arg, tcp_pcb *tcp)
{
	TcpConnection* con = (TcpConnection*)arg;

	if (con == NULL)
	{
		closeTcpConnection(tcp);
		return ERR_OK;
	}

	//if (tcp->state != ESTABLISHED)
	//	return ERR_OK;

	con->sleep++;
	err_t res = con->onPoll();
	con->checkSelfFree();
	//debugf("<staticOnPoll");
	return res;
}
Exemplo n.º 5
0
void NetworkMgr::_readInput()
{
    while (_tcpSock.bytesAvailable() > 0)
    {
        std::cout << "CALL READ INPUT" << std::endl;
        bool welcoming = _connState == STATE_WELCOMING;
        char buff[8];
        std::cout << "AVAILABLE: " << _tcpSock.bytesAvailable() << " _connState: " << quint32(_connState) << std::endl;
        _tcpSock.read(buff, welcoming ? 8 : 4);

        switch (_connState)
        {
            case STATE_DISCONNECTED:
                break;
            case STATE_WELCOMING:
            {
                QString welcome = "WELCOME";
                if (welcome.compare(buff) == 0)
                {
                    std::cout << "WELCOME OK" << std::endl;
                    _window->handleRequireAuth(_tcpSock.localAddress().toString());
                    _connState = STATE_WAITING_AUTH;
                }
                else
                    closeTcpConnection();
                break;
            }
            case STATE_WAITING_AUTH:
            {
                quint16 size = qFromBigEndian<quint16>(*((quint16 const*)&buff[0]));
                quint16 code = qFromBigEndian<quint16>(*((quint16 const*)&buff[2]));
                if (size != 1 || code != SMSG_AUTH_RESULT)
                {
                    closeTcpConnection();
                    break;
                }
                char data[Packet::MaxBodySize];
                if (size > 0 && size < Packet::MaxBodySize)
                    _tcpSock.read(data, size);
                Packet pkt(code, data, size);
                if (_window->handleAuthResult(pkt))
                {
                    sClientMgr->setPrivateIp(_tcpSock.localAddress().toString());
                    _connState = STATE_AUTHED;
                }
                break;
            }
            case STATE_AUTHED:
            {
                quint16 size = qFromBigEndian<quint16>(*((quint16 const*)&buff[0]));
                quint16 code = qFromBigEndian<quint16>(*((quint16 const*)&buff[2]));

                std::cout << "RECEIV SIZE: " << size << " - CODE : " << code << std::endl;
                char data[Packet::MaxBodySize];
                if (size > 0 && size < Packet::MaxBodySize)
                    _tcpSock.read(data, size);
                Packet pkt(code, data, size);
                //pkt.dumpHex();

                OpcodeMgr::OpcodeDefinition const* opcodedef = OpcodeMgr::getOpcodeDefinition(pkt.getOpcode());
                if (!opcodedef)
                {
                    std::cerr << "Error: receiv unknow opcode: " << pkt.getOpcode() << std::endl;
                    return;
                }
                if (opcodedef->func)
                    (_window->*opcodedef->func)(pkt);
                break;
            }
        }
    }
}