Click here to Skip to main content
15,881,803 members
Articles / Hosted Services / Serverless

Peer Graph - Importing and Exporting a Database

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
24 Jan 20064 min read 34K   649   12  
Importing and exporting a Peer Graph database using Microsoft's Peer-to-Peer technology.
using System;
using System.Runtime.InteropServices;

namespace Peer.Graph
{
	public class PeerOpenConnection
	{
		PeerGraph graph;
		System.Int64 connectionId;

		public PeerOpenConnection(PeerGraph Graph, System.Int64 ConnectionId)
		{
			graph = Graph;
			connectionId = ConnectionId;
		}

		public PeerGraph Graph
		{
			get
			{
				return graph;
			}
		}

		public System.Int64 ConnectionId
		{
			get
			{
				return connectionId;
			}
		}

		public void Close()
		{
			if (connectionId != 0) 
			{
				graph.CloseDirectConnection(connectionId);
				connectionId = 0;
			}
		}

		public PeerConnection Connection
		{
			get
			{
				return graph.GetConnectionInfo(connectionId);
			}
		}

		public void SendData(Guid Type, string Message)
		{
			IntPtr dataptr = Marshal.StringToHGlobalUni(Message);
			int size = (Message.Length+1)*2;
			graph.SendData(connectionId, Type, size, dataptr);
		}

		public void SendData(Guid Type, System.IO.Stream Data)
		{
			int size = (int)Data.Length;
			byte[] data = new byte[size];
			Data.Read(data, 0, size);
			IntPtr dataptr = Marshal.AllocHGlobal(size);
			Marshal.StructureToPtr(data, dataptr, false);
			graph.SendData(connectionId, Type, size, dataptr);
		}

		public delegate void DataReceivedHandler(object sender, PeerGraphDataReceiveEventArgs e);
		public event DataReceivedHandler DataReceived;

		internal void RaiseDataReceivedEvent(PeerGraphDataReceiveEventArgs args)
		{
			if (DataReceived != null)
			{
				DataReceived(this, args);
			}
		}
	}

	public class PeerConnection
	{
		private PeerConnectionType type;
		private System.Int64 connectionId;
		private System.Int64 peerId;
		private string peerName;
		private PEER_ADDRESS address;

		internal PeerConnection(PEER_CONNECTION_INFO info)
		{
			type = (PeerConnectionType)info.dwFlags;
			connectionId = info.ullConnectionId;
			peerId = info.ullNodeId;
			peerName = info.wzPeerName;
			address = info.address;
		}

		public PeerConnectionType Type
		{
			get
			{
				return type;
			}
		}

		public System.Int64 ConnectionId
		{
			get
			{
				return connectionId;
			}
		}

		public System.Int64 PeerId
		{
			get
			{
				return peerId;
			}
		}

		public string PeerName
		{
			get
			{
				return peerName;
			}
		}

		public System.Net.IPEndPoint Address
		{
			get
			{
				return new System.Net.IPEndPoint(new System.Net.IPAddress(address.sin6.sin6_addr), address.sin6.sin6_port);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Adrian Moore is the Development Manager for the SCADA Vision system developed by ABB Inc in Calgary, Alberta.

He has been interested in compilers, parsers, real-time database systems and peer-to-peer solutions since the early 90's. In his spare time, he is currently working on a SQL parser for querying .NET DataSets (http://www.queryadataset.com).

Adrian is a Microsoft MVP for Windows Networking.

Comments and Discussions