/***************************************************************************
 *   Copyright (C) 2004 by yunfan                                          *
 *   yunfan_zg@163.com                                                     *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef EVACONNECTER_H 
#define EVACONNECTER_H

#include <qobject.h>
#include <qptrlist.h>
#include <qhostaddress.h>
#include <qstring.h>
#include "evapacket.h"
#include "evanetwork.h"

class QTimer;

class EvaConnecter : public QObject {
	Q_OBJECT
public:
	EvaConnecter(EvaNetwork *network);
	~EvaConnecter();
	
	void append(OutPacket *out);
	InPacket *getInPacket();
	const unsigned int numOfOutPackets() const { return outPool.count(); }
	const unsigned int numOfInPackets() const { return inPool.count(); }
	void redirectTo(const int ip, const short port);
	void connect();
        void stop();
	
        const EvaNetwork::Type getConnectionType() const { return connecter->connectionType(); }
signals:
	void isReady();
	void networkException(int);
	void packetException(int);
	void newPacket();
private:
	bool connectionReady;
	QPtrList<InPacket> inPool;
	QPtrList<OutPacket> outPool;
	QTimer *timer;
	bool isConnected;
	EvaNetwork *connecter;	
	void sendOut( OutPacket *out);
	void removePacket(const int hashCode ); // remove packet which needs acknowlegement
	void removeOutRequests(const short cmd);
private slots:
	void isReadySlot();
	void dataCommingSlot(int len);
	void packetMonitor();
};

#endif
