Click here to Skip to main content
15,894,907 members
Articles / Web Development / ASP.NET

P2P Communication - Proxy Listeners and Proxy Clients

Rate me:
Please Sign up or sign in to vote.
3.67/5 (3 votes)
19 Apr 2008CPOL4 min read 58.7K   2.2K   48  
Implementation of Listener and Client objects that communicate through a Web proxy
using System;
using System.Collections.Generic;
using System.Text;

namespace ProxyCommon
{
    public class Header
    {
        #region Member Variables
        private MessageType messageType;
        private short senderEndPoint;
        private short recieverEndPoint;
        private int _contentSize;

        #endregion

        #region Constructor
        public Header(MessageType MessageType, short SenderEndPoint, short RecieverEndPoint)
        {
            messageType = MessageType;
            senderEndPoint = SenderEndPoint;
            recieverEndPoint = RecieverEndPoint;
        }
        public Header(byte[] SerializationData)
        {
            messageType = (MessageType) SerializationData[0];
            senderEndPoint = BitConverter.ToInt16(SerializationData, 1);
            recieverEndPoint = BitConverter.ToInt16(SerializationData, 3);
            _contentSize = BitConverter.ToInt32(SerializationData, 5);
        }
        #endregion

        #region Properties
        public short SenderEndPoint
        {
            get { return senderEndPoint; }
            set { senderEndPoint = value; }
        }
        public short RecieverEndPoint
        {
            get { return recieverEndPoint; }
            set { recieverEndPoint = value; }
        }
        public MessageType MessageType
        {
            get { return messageType; }
        }
        #endregion

        #region Serialization

        public virtual byte[] ToByteArray()
        {
            byte[] bytes = new byte[9];
            bytes[0] = (byte)messageType;
            BitConverter.GetBytes(senderEndPoint).CopyTo(bytes, 1);
            BitConverter.GetBytes(recieverEndPoint).CopyTo(bytes, 3);
            BitConverter.GetBytes(_contentSize).CopyTo(bytes, 5);
            return bytes;
        }

        public int ContentSize
        {
            get { return _contentSize; }
            set { _contentSize = value; }
        }

        public virtual int GetSize()
        {
            return 9;
        }

        #endregion
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions