Server C++, Klient C#

Odpowiedz Nowy wątek
2014-07-24 12:01
0

Witam.
Próbuję zrobić klienta w C# do pewnej aplikacji open source napisanej w C++. Niestety mimo że jest połączenie to server wywala taki błąd:

995 Operacja We/Wy została przerwana z powodu zakończenia wątku lub żądania aplikacji

Tutaj fragment kodu serwera:

void Connection::parseHeader(const boost::system::error_code& error)
{
    m_connectionLock.lock();
    m_readTimer.cancel();

    int32_t size = m_msg.decodeHeader();
    if(error || size <= 0 || size >= NETWORKMESSAGE_MAXSIZE - 16)
        handleReadError(error);

    if(m_connectionState != CONNECTION_STATE_OPEN || m_readError)
    {
        close();
        m_connectionLock.unlock();
        return;
    }

    --m_pendingRead;
    try
    {
        ++m_pendingRead;
        m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::readTimeout));
        m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout,
            boost::weak_ptr<Connection>(shared_from_this()), boost::asio::placeholders::error));

        // Read packet content
        m_msg.setMessageLength(size + NetworkMessage::headerLength);

        boost::asio::async_read(getHandle(), boost::asio::buffer(m_msg.getBodyBuffer(), size),
            boost::bind(&Connection::parsePacket, shared_from_this(), boost::asio::placeholders::error));
    }
    catch(boost::system::system_error& e)
    {
        if(m_logError)
        {
            LOG_MESSAGE(LOGTYPE_ERROR, e.what(), "NETWORK");
            m_logError = false;
            close();
        }
    }

    m_connectionLock.unlock();
}

void Connection::parsePacket(const boost::system::error_code& error)
{
    std::cout << "brama1" << std::endl;
    m_connectionLock.lock();
    m_readTimer.cancel();
    if(error)
    {
        std::cout << error.value() << " " << " " << error.message()  << std::endl;
        handleReadError(error);
    }
    std::cout << "brama2" << std::endl;
    if(m_connectionState != CONNECTION_STATE_OPEN || m_readError)
    {
        close();
        m_connectionLock.unlock();
        return;
    }
    std::cout << "brama3" << std::endl;
    --m_pendingRead;
    uint32_t length = m_msg.getMessageLength() - m_msg.getReadPos() - 4, checksumReceived = m_msg.PeekU32(), checksum = 0;
    if(length > 0)
        checksum = adlerChecksum((uint8_t*)(m_msg.getBuffer() + m_msg.getReadPos() + 4), length);
    std::cout << "brama4" << std::endl;
    bool checksumEnabled = false;
    if(checksumReceived == checksum)
    {
        m_msg.SkipBytes(4);
        checksumEnabled = true;
    }
    std::cout << "brama5" << std::endl;
    if(!m_receivedFirst)
    {
        // First message received
        m_receivedFirst = true;
        if(!m_protocol)
        {
            // Game protocol has already been created at this point
            m_protocol = m_servicePort->makeProtocol(checksumEnabled, m_msg);
            if(!m_protocol)
            {
                close();
                m_connectionLock.unlock();
                return;
            }

            m_protocol->setConnection(shared_from_this());
        }
        else
            m_msg.SkipBytes(1); // Skip protocol

        m_protocol->onRecvFirstMessage(m_msg);
    }
    else
        m_protocol->onRecvMessage(m_msg); // Send the packet to the current protocol

    try
    {
        ++m_pendingRead;
        m_readTimer.expires_from_now(boost::posix_time::seconds(Connection::readTimeout));
        m_readTimer.async_wait(boost::bind(&Connection::handleReadTimeout,
            boost::weak_ptr<Connection>(shared_from_this()), boost::asio::placeholders::error));

        // Wait to the next packet
        boost::asio::async_read(getHandle(),
            boost::asio::buffer(m_msg.getBuffer(), NetworkMessage::headerLength),
            boost::bind(&Connection::parseHeader, shared_from_this(), boost::asio::placeholders::error));
    }
    catch(boost::system::system_error& e)
    {
        if(m_logError)
        {
            LOG_MESSAGE(LOGTYPE_ERROR, e.what(), "NETWORK");
            m_logError = false;
            close();
        }
    }

    m_connectionLock.unlock();
}

Te std::cout << "brama dodałem by wiedzieć gdzie dochodzi kod. (do brama2 dla informacji.)

A tutaj prosty kod C# dla testu:

        TcpClient client;
        StreamReader reader;
        StreamWriter writer;
        public Connection()
        {
            try
            {
                client = new TcpClient("127.0.0.1", 7171);
                client.ReceiveTimeout = 150000;
                client.SendTimeout = 150000;
                reader = new StreamReader(client.GetStream());
                writer = new StreamWriter(client.GetStream());
                writer.AutoFlush = true;
                sendFirstPacket();
                while (true)
                {
                    if (client.Available> 0)
                        Logger.log(Log_Type.ERROR, "pozdro");
                }
            }
            catch (Exception e)
            {
                Logger.log(Log_Type.ERROR, "Cos nie tak!");
            }
        }

        void sendFirstPacket()
        {
            writer.Write((byte)10);
            writer.Write((ushort)2);
            writer.Write((ushort)854);
            UInt32[] k = new UInt32[4];

            k[0] = (12444 << 16) | 12444;
            k[1] = (12444 << 16) | 12444;
            k[2] = (12444 << 16) | 12444;
            k[3] = (12444 << 16) | 12444;

            writer.Write((byte)0); // first byte have to be 0
            writer.Write((UInt32)k[0]);
            writer.Write((UInt32)k[1]);
            writer.Write((UInt32)k[2]);
            writer.Write((UInt32)k[3]);

            writer.Write((byte)0);

        }
    }

Ktoś ma pomysł o co chodzi?

edytowany 1x, ostatnio: Kemsai, 2014-07-24 12:02
nie pisz takich magicznych liczb jak 12444. Jeśli to jest maska bitowa to podawaj w heksach. - Azarien 2014-07-24 12:49

Pozostało 580 znaków

2014-07-24 17:43
0

Gdy umieściłem sendFirstPacket w tej pętli while to dochodzi bez problemu.. nie mam pojęcia dlaczego i jakim cudem o.O

Pozostało 580 znaków

Odpowiedz
Liczba odpowiedzi na stronę

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0