Kaynağa Gözat

setup

master
Robin Thoni 10 yıl önce
ebeveyn
işleme
8dd1cf7e4c

+ 8
- 2
libptsocket/ptserver.cpp Dosyayı Görüntüle

@@ -1,6 +1,12 @@
1 1
 #include "ptserver.h"
2 2
 
3
-PTServer::PTServer(QObject *parent) :
4
-	QObject(parent)
3
+PTServer::PTServer(QObject *parent) : QTcpServer(parent)
5 4
 {
5
+	connect(this, SIGNAL(newConnection()), this, SLOT(m_newConnection()));
6
+}
7
+
8
+void PTServer::m_newConnection()
9
+{
10
+	PTSocket* client = dynamic_cast<PTSocket*>(nextPendingConnection());
11
+	m_tempClients.append(client);
6 12
 }

+ 14
- 2
libptsocket/ptserver.h Dosyayı Görüntüle

@@ -3,16 +3,28 @@
3 3
 
4 4
 #include "libptsocket_global.h"
5 5
 #include <QObject>
6
+#include <QTcpServer>
7
+#include <QTcpSocket>
8
+#include <QHostAddress>
9
+#include "ptsocket.h"
6 10
 
7
-class LIBPTSOCKETSHARED_EXPORT PTServer : public QObject
11
+class LIBPTSOCKETSHARED_EXPORT PTServer : public QTcpServer
8 12
 {
9 13
 	Q_OBJECT
10 14
 public:
11 15
 	explicit PTServer(QObject *parent = 0);
12
-	
16
+
13 17
 signals:
18
+	void newClient(PTSocket*);
14 19
 	
15 20
 public slots:
21
+
22
+private slots:
23
+	void m_newConnection();
24
+
25
+private:
26
+	QList<PTSocket*> m_tempClients;
27
+	QList<PTSocket*> m_clients;
16 28
 	
17 29
 };
18 30
 

+ 52
- 1
libptsocket/ptsocket.cpp Dosyayı Görüntüle

@@ -1,6 +1,57 @@
1 1
 #include "ptsocket.h"
2 2
 
3 3
 
4
-PTSocket::PTSocket()
4
+PTSocket::PTSocket(QObject* p) : QTcpSocket(p)
5 5
 {
6
+	m_hasHandshaked = false;
7
+	m_timeout = 5000;
8
+	connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(m_handshakeError()));
9
+	connect(this, SIGNAL(readyRead()), this, SLOT(m_readyRead()));
10
+}
11
+
12
+int PTSocket::getTimeout() const
13
+{
14
+	return m_timeout;
15
+}
16
+
17
+QByteArray PTSocket::handshakeData() const
18
+{
19
+	return "LibPTSocket";
20
+}
21
+
22
+void PTSocket::handshake()
23
+{
24
+	write(handshakeData());
25
+}
26
+
27
+void PTSocket::setTimeout(int t)
28
+{
29
+	m_timeout = t;
30
+}
31
+
32
+void PTSocket::m_readyRead()
33
+{
34
+	if(m_hasHandshaked)
35
+	{
36
+
37
+	}
38
+	else
39
+	{
40
+		QByteArray handshake = handshakeData();
41
+		if(bytesAvailable() >= handshake.size())
42
+		{
43
+			QByteArray data = read(handshake.size());
44
+			if(data == handshake)
45
+			{
46
+				m_hasHandshaked = true;
47
+			}
48
+			else
49
+				m_handshakeError();
50
+		}
51
+	}
52
+}
53
+
54
+void PTSocket::m_handshakeError()
55
+{
56
+
6 57
 }

+ 20
- 2
libptsocket/ptsocket.h Dosyayı Görüntüle

@@ -3,12 +3,30 @@
3 3
 
4 4
 #include "libptsocket_global.h"
5 5
 #include <QObject>
6
+#include <QTcpSocket>
7
+#include <QTimer>
6 8
 
7
-class LIBPTSOCKETSHARED_EXPORT PTSocket : public QObject
9
+class LIBPTSOCKETSHARED_EXPORT PTSocket : public QTcpSocket
8 10
 {
9 11
 	Q_OBJECT
10 12
 public:
11
-	PTSocket();
13
+	PTSocket(QObject* p);
14
+	int getTimeout() const;
15
+
16
+	static QByteArray handshakeData() const;
17
+
18
+public slots:
19
+	void handshake();
20
+	void setTimeout(int t);
21
+
22
+private slots:
23
+	void m_readyRead();
24
+	void m_handshakeError();
25
+
26
+private:
27
+	bool m_hasHandshaked;
28
+	QTimer m_timeoutTimer;
29
+	int m_timeout;
12 30
 };
13 31
 
14 32
 #endif // PTSOCKET_H

+ 5
- 0
tests/main.cpp Dosyayı Görüntüle

@@ -1,8 +1,13 @@
1 1
 #include <QCoreApplication>
2 2
 
3
+#include "test.h"
4
+
3 5
 int main(int argc, char *argv[])
4 6
 {
5 7
 	QCoreApplication a(argc, argv);
8
+	Test t;
9
+	t.testListen(6950);
10
+	t.testNewConnection();
6 11
 	
7 12
 	return a.exec();
8 13
 }

+ 31
- 0
tests/test.cpp Dosyayı Görüntüle

@@ -0,0 +1,31 @@
1
+#include "test.h"
2
+
3
+Test::Test(QObject *parent) : QObject(parent)
4
+{
5
+	m_serv = new PTServer(this);
6
+	connect(m_serv, SIGNAL(newConnection()), this, SLOT(m_newConnection()));
7
+	connect(m_serv, SIGNAL(newClient(PTSocket*)), this, SLOT(m_newClient(PTSocket*)));
8
+}
9
+
10
+void Test::testListen(quint16 p)
11
+{
12
+	Q_ASSERT(m_serv->listen(QHostAddress::Any, p));
13
+}
14
+
15
+void Test::testNewConnection()
16
+{
17
+	ASSERT_LISTEN;
18
+	PTSocket* sock = new PTSocket(this);
19
+	m_clients.append(sock);
20
+	sock->connectToHost("127.0.0.1", m_serv->serverPort());
21
+}
22
+
23
+void Test::m_newConnection()
24
+{
25
+	qDebug()<<"New socket";
26
+}
27
+
28
+void Test::m_newClient(PTSocket* s)
29
+{
30
+	qDebug()<<"New client:"<<s->peerAddress()<<s->peerPort();
31
+}

+ 32
- 0
tests/test.h Dosyayı Görüntüle

@@ -0,0 +1,32 @@
1
+#ifndef TEST_H
2
+#define TEST_H
3
+
4
+#include <QObject>
5
+#include "ptserver.h"
6
+#include "ptsocket.h"
7
+#include <QDebug>
8
+
9
+#define ASSERT_LISTEN Q_ASSERT(m_serv->isListening())
10
+
11
+class Test : public QObject
12
+{
13
+	Q_OBJECT
14
+public:
15
+	explicit Test(QObject *parent = 0);
16
+	
17
+signals:
18
+	
19
+public slots:
20
+	void testListen(quint16 p);
21
+	void testNewConnection();
22
+
23
+private slots:
24
+	void m_newConnection();
25
+	void m_newClient(PTSocket* s);
26
+
27
+private:
28
+	PTServer* m_serv;
29
+	QList<PTSocket*> m_clients;
30
+};
31
+
32
+#endif // TEST_H

+ 13
- 2
tests/tests.pro Dosyayı Görüntüle

@@ -4,7 +4,7 @@
4 4
 #
5 5
 #-------------------------------------------------
6 6
 
7
-QT       += core
7
+QT       += core network
8 8
 
9 9
 QT       -= gui
10 10
 
@@ -15,4 +15,15 @@ CONFIG   -= app_bundle
15 15
 TEMPLATE = app
16 16
 
17 17
 
18
-SOURCES += main.cpp
18
+SOURCES += main.cpp \
19
+    test.cpp
20
+
21
+win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../libptsocket/release/ -llibptsocket
22
+else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../libptsocket/debug/ -llibptsocket
23
+else:unix:!macx: LIBS += -L$$OUT_PWD/../libptsocket/ -llibptsocket
24
+
25
+INCLUDEPATH += $$PWD/../libptsocket
26
+DEPENDPATH += $$PWD/../libptsocket
27
+
28
+HEADERS += \
29
+    test.h

Loading…
İptal
Kaydet