summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/msn/transport.h
blob: 0dae0f32eee8043f732f676f1789af4b0a78007a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
    transport.h - Peer to peer transport

    Copyright (c) 2005 by Gregg Edghill     <[email protected]>

    Kopete    (c) 2002-2005 by the Kopete developers <[email protected]>

    *************************************************************************
    *                                                                       *
    * This program is free software; you can redistribute it and/or modify  *
    * it under the terms of the GNU General Public License as published by  *
    * the Free Software Foundation; version 2 of the License.               *
    *                                                                       *
    *************************************************************************
*/

#ifndef PEERTOPEERTRANSPORT_H
#define PEERTOPEERTRANSPORT_H

//BEGIN QT Includes
#include <qobject.h>
#include <qguardedptr.h>
#include <qvaluelist.h>
//END

//BEGIN KDE Includes
#include <ksocketaddress.h>
//END

namespace KNetwork {
class KClientSocketBase;
}

class MSNSwitchBoardSocket;

namespace PeerToPeer {

class MessageFormatter;
class TransportBridge;

enum TransportBridgeType
{
	Tcp = 1,
	Udp = 2
};
	
/**
 @author Gregg Edghill <[email protected]> */
/** @brief Represents the protocol used to send and receive message between peers. */
class Transport : public QObject
{
	Q_OBJECT
public:
	/** @brief Creates a new instance of the class Transport. */
    Transport(QObject* parent, const char* name = 0l);
    ~Transport();
	/** @brief Get a transport bridge with the specified address, port, type and identifier. */
    TransportBridge* getBridge(const QString& address, Q_UINT16 port, TransportBridgeType type, const QString& identifier);
    /** @brief Sets the default transport bridge. */
	void setDefaultBridge(MSNSwitchBoardSocket* mss);
	
private slots:
	/** @brief Invokes when a message is received on a transport bridge. */
// 	void slotOnReceive(Message& message);
	/** @brief Invokes when a message is received on the default transport bridge (relay). */
	void slotOnReceive(const QString& contact, const QByteArray& bytes);

private:
	/** @brief Known SocketAddresses of peers. */
	QMap<QString, KNetwork::KInetSocketAddress> mAddresses;
	/** @brief The list the connected transport bridges. */
	QValueList<TransportBridge*> mBridges;
	/** @brief The default transport bridge (relay). */
	QGuardedPtr<MSNSwitchBoardSocket> mDefaultBridge;
	/** @brief Message formatter used to ser/deser message. */
	MessageFormatter *mFormatter;
};

/** @brief Represents the channel connecting two peers. */
class TransportBridge : public QObject
{
	Q_OBJECT
public:
	virtual ~TransportBridge();

protected:
	/** @brief Creates a new instance of the class TransportBridge with the specified address and formatter. */
	TransportBridge(const KNetwork::KInetSocketAddress& to, MessageFormatter* formatter, QObject* parent, const char* name = 0l);
	/** @brief Creates a new instance of the class TransportBridge with the specified socket and formatter. */
	TransportBridge(KNetwork::KClientSocketBase* socket, MessageFormatter* formatter, QObject* parent, const char* name = 0l);

public:
	/** @brief Creates a connection between two peers. */
	void connect();
	/** @brief Disconnects the connection between two peers. */
	void disconnect();

protected slots:
	virtual void slotOnConnect();
	virtual void slotOnDisconnect();
	virtual void slotOnError(int);
	virtual void slotOnSocketClose();
	virtual void slotOnSocketConnect();
	virtual void slotOnSocketReceive();

signals:
	void bridgeConnect();
	void bridgeDisconnect();
	void bridgeError(const QString& e);
	void bytesReceived(const QByteArray&);
	
protected:
	
	KNetwork::KInetSocketAddress mAddress;
	bool mConnected;
	MessageFormatter *mFormatter;
	Q_UINT32 mLength;
	KNetwork::KClientSocketBase *mSocket;
	bool mVerified;
};

class TcpTransportBridge : public TransportBridge
{
	Q_OBJECT
	friend class Transport;
	
public:
	virtual ~TcpTransportBridge();

private:
	TcpTransportBridge(const KNetwork::KInetSocketAddress& to, MessageFormatter* formatter, QObject* parent, const char* name = 0l);
	TcpTransportBridge(KNetwork::KClientSocketBase* socket, MessageFormatter* formatter, QObject* parent, const char* name = 0l);
	
protected slots:
	virtual void slotOnConnect();
	virtual void slotOnDisconnect();
	virtual void slotOnError(int);
	virtual void slotOnSocketClose();
	virtual void slotOnSocketConnect();
	virtual void slotOnSocketReceive();
	
private slots:
	void slotOnSocketConnectTimeout();

signals:
	void bridgeConnectTimeout();
	
private:
	class Buffer : public QByteArray
	{
	public:
		Buffer(Q_UINT32 length = 0);
		~Buffer();
		
	public:
		void write(const QByteArray& bytes);
		QByteArray read(Q_UINT32 length);
	};
	
	Buffer mBuffer;
	Q_UINT32 mLength;
};


}

#endif