12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #include "test.h"
-
- Test::Test(QObject *parent) : QObject(parent)
- {
- m_serv = new PTServer(this);
- connect(m_serv, SIGNAL(newConnection()), this, SLOT(m_newConnection()));
- connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient(PTSocket*)));
- }
-
- void Test::testIntConvert()
- {
- QList<int> tests;
- tests << 0 << 1 << 255 << 256 << 65535 << 65536 << -1 << -65535 << -65536 << 123456789 << -123456789;
- for(int i = 0; i < tests.size(); ++i)
- Q_ASSERT(PTSocket::byteArrayToInt(PTSocket::intToByteArray(tests[i])) == tests[i]);
- qDebug()<<"Int convert OK";
- }
-
- void Test::testListen(quint16 p)
- {
- Q_ASSERT(m_serv->listen(QHostAddress::Any, p));
- }
-
- void Test::testNewConnection()
- {
- ASSERT_LISTEN;
- PTSocket* sock = new PTSocket(this);
- connect(sock, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
- m_clients.append(sock);
- sock->connectToHost("127.0.0.1", m_serv->serverPort());
- }
-
- void Test::m_newConnection()
- {
- qDebug()<<"New socket";
- }
-
- void Test::m_newClient(PTSocket* s)
- {
- connect(s, SIGNAL(stateChanged(PTSocket::State)), this, SLOT(m_clientStateChanged(PTSocket::State)));
- qDebug()<<"New client:"<<s->peerAddress()<<s->peerPort();
- }
-
- void Test::m_clientStateChanged(PTSocket::State s)
- {
- PTSocket* sock = qobject_cast<PTSocket*>(sender());
- if(sock == 0)
- return;
- qDebug()<<sock<<sock->isServerSide()<<s;
- if(s == PTSocket::Error)
- qDebug()<<sock->errorString();
- }
|