Code Search for Developers
 
 
  

PacketUdp.cpp from Make Controller at Krugle


Show PacketUdp.cpp syntax highlighted

/*********************************************************************************

 Copyright 2006 MakingThings

 Licensed under the Apache License, 
 Version 2.0 (the "License"); you may not use this file except in compliance 
 with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0 
  
 Unless required by applicable law or agreed to in writing, software distributed
 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 CONDITIONS OF ANY KIND, either express or implied. See the License for
 the specific language governing permissions and limitations under the License.

*********************************************************************************/

#include "PacketUdp.h"

#include <QHostInfo>
#include <QHostAddress>
#include <QSettings>
#include <QString>

#define COMM_TIMEOUT 3000

PacketUdp::PacketUdp( )
{ 
	timer = new QTimer(this);
	lastMessage = NULL;
	socket = NULL;
	packetReadyInterface = NULL;
	connect( timer, SIGNAL(timeout()), this, SLOT( pingTimedOut( ) ) );
}

PacketUdp::~PacketUdp( )
{
	delete timer;
}

PacketUdp::Status PacketUdp::open( ) //part of PacketInterface
{	
  socket = new QUdpSocket( this );
  return OK;	
}

PacketUdp::Status PacketUdp::pingTimedOut( )
{
	timer->stop( );
	monitor->deviceRemoved( socketKey );
	return OK;
}

PacketUdp::Status PacketUdp::close( )	//part of PacketInterface
{
	if ( socket != 0 )
	  socket->close( );
	
	if( lastMessage != NULL )
		delete lastMessage;
  		
  return OK;
}

void PacketUdp::resetTimer( void )
{
	timer->start( COMM_TIMEOUT );
}

QString PacketUdp::getKey( )
{
	return socketKey;
}

char* PacketUdp::location( )
{
	return remoteHostName.data( );
}

PacketUdp::Status PacketUdp::sendPacket( char* packet, int length )	//part of PacketInterface
{
	qint64 result = socket->writeDatagram( (const char*)packet, (qint64)length, remoteHostAddress, remotePort );
	if( result < 0 )
  {
		QString msg = QString( "Error - Could not send packet.");
		messageInterface->messageThreadSafe( msg, MessageEvent::Error, QString("Ethernet") );
		return IO_ERROR;
  }
	return OK;
}

bool PacketUdp::isPacketWaiting( )	//part of PacketInterface
{
  return lastMessage != NULL;
}

bool PacketUdp::isOpen( )
{
	return socket != NULL;
}

void PacketUdp::processPacket( )	//slot to be called back automatically when datagrams are ready to be read
{
  if( packetReadyInterface != NULL )
  	packetReadyInterface->packetWaiting( );
}

int PacketUdp::receivePacket( char* buffer, int size )
{
	int length = lastMessage->size( );
	if( length > size )
	{
		QString msg = QString( "Error - packet too large.");
		messageInterface->messageThreadSafe( msg, MessageEvent::Error, QString("Ethernet") );
		return 0;
	}
	memcpy( buffer, lastMessage->data( ), length );
	delete lastMessage;
	lastMessage = NULL;
	
	return length;
}

void PacketUdp::incomingMessage( QByteArray* message )
{
	lastMessage = message;
}

void PacketUdp::setRemoteHostInfo( QHostAddress* address, quint16 port )
{
	if( *address == QHostAddress::Null )
		return;
		
	remoteHostAddress = *address;
	remoteHostName = remoteHostAddress.toString( ).toAscii();
	remotePort = port;
}

void PacketUdp::setKey( QString key )
{
	socketKey = key;
}

void PacketUdp::setPacketReadyInterface( PacketReadyInterface* packetReadyInterface )
{
	this->packetReadyInterface = packetReadyInterface;
}

void PacketUdp::setInterfaces( MessageInterface* messageInterface, MonitorInterface* monitor )
{
	this->messageInterface = messageInterface;
	this->monitor = monitor;
}





See more files for this project here

Make Controller

The Make Controller is an open microcontroller platform for Makers of all kinds, by MakingThings. This project maintains the official firmware for the board and source for the software tools used with the board.

Project homepage: http://sourceforge.net/projects/makingthings
Programming language(s): C,C#,C++
License: other

  layouts/
    mchelper.ui
    mchelperPrefs.ui
  linux_driver/
    Makefile
    at91.c
    at91.mod.c
  Board.cpp
  Board.h
  BoardArrivalEvent.h
  IconPackageOSX.icns
  McHelperWindow.cpp
  McHelperWindow.h
  MessageEvent.cpp
  MessageEvent.h
  MessageInterface.h
  MonitorInterface.h
  NetworkMonitor.cpp
  NetworkMonitor.h
  Osc.cpp
  Osc.h
  OscXmlServer.cpp
  OscXmlServer.h
  OutputWindow.cpp
  OutputWindow.h
  PacketInterface.h
  PacketReadyInterface.h
  PacketUdp.cpp
  PacketUdp.h
  PacketUsbCdc.cpp
  PacketUsbCdc.h
  ReadMe.rtf
  Samba.cpp
  Samba.h
  SambaMonitor.cpp
  SambaMonitor.h
  UploaderThread.cpp
  UploaderThread.h
  UsbMonitor.cpp
  UsbMonitor.h
  UsbSerial.cpp
  UsbSerial.h
  guid829.h
  loader256_data.h
  main.cpp
  mchelper.pro
  mchelper.qrc
  mchelper.rc
  mchelper_icon.ico
  mticon128.png