/// <summary>
        /// Convert <paramref name="container"/> to ProtocolMessage.
        /// </summary>
        /// <param name="container">The container to be converted.</param>
        /// <returns>The converted protocol message.</returns>
        public ProtocolMessage CreateMessage(ProtocolMessageContainer container)
        {
            byte[] data = container.Data;
            switch (container.Type)
            {
                case ProtocolMessage.MessageType.InvitationRequest:
                    return CommonHelper.Deserialize<InvitationRequest>(data);
                case ProtocolMessage.MessageType.InvitationAck:
                    return CommonHelper.Deserialize<InvitationAck>(data);
                case ProtocolMessage.MessageType.SyncRequest:
                    return CommonHelper.Deserialize<SyncRequest>(data);
                case ProtocolMessage.MessageType.SyncAck:
                    return CommonHelper.Deserialize<SyncAck>(data);
                case ProtocolMessage.MessageType.PeerListMessage:
                    return CommonHelper.Deserialize<PeerListMessage>(data);
                case ProtocolMessage.MessageType.VersionListMessage:
                    return CommonHelper.Deserialize<VersionListMessage>(data);
                case ProtocolMessage.MessageType.PatchRequest:
                    return CommonHelper.Deserialize<PatchRequest>(data);
                case ProtocolMessage.MessageType.FileResponse:
                    return CommonHelper.Deserialize<FileDataResponse>(data);
            }

            throw new Exception("ToDerivedClass: What class is this? Maybe you forgot to add an enum / case statement?");
        }
Exemple #2
0
        /// <summary>
        /// Convert <paramref name="container"/> to ProtocolMessage.
        /// </summary>
        /// <param name="container">The container to be converted.</param>
        /// <returns>The converted protocol message.</returns>
        public ProtocolMessage CreateMessage(ProtocolMessageContainer container)
        {
            byte[] data = container.Data;
            switch (container.Type)
            {
            case ProtocolMessage.MessageType.InvitationRequest:
                return(CommonHelper.Deserialize <InvitationRequest>(data));

            case ProtocolMessage.MessageType.InvitationAck:
                return(CommonHelper.Deserialize <InvitationAck>(data));

            case ProtocolMessage.MessageType.SyncRequest:
                return(CommonHelper.Deserialize <SyncRequest>(data));

            case ProtocolMessage.MessageType.SyncAck:
                return(CommonHelper.Deserialize <SyncAck>(data));

            case ProtocolMessage.MessageType.PeerListMessage:
                return(CommonHelper.Deserialize <PeerListMessage>(data));

            case ProtocolMessage.MessageType.VersionListMessage:
                return(CommonHelper.Deserialize <VersionListMessage>(data));

            case ProtocolMessage.MessageType.PatchRequest:
                return(CommonHelper.Deserialize <PatchRequest>(data));

            case ProtocolMessage.MessageType.FileResponse:
                return(CommonHelper.Deserialize <FileDataResponse>(data));
            }

            throw new Exception("ToDerivedClass: What class is this? Maybe you forgot to add an enum / case statement?");
        }
        public void Test()
        {
            InvitationRequest request = new InvitationRequest(1234);

            ProtocolMessageContainer container = new ProtocolMessageContainer(request);

            Assert.AreEqual(ProtocolMessage.MessageType.InvitationRequest, container.Type);
            Assert.AreEqual(request.SerializeAsBytes(), container.Data);
        }
Exemple #4
0
        /// <summary>
        /// Sends a message to peer.
        /// </summary>
        /// <param name="peer">The peer.</param>
        /// <param name="message">The message.</param>
        /// <param name="onCompleteHandler">On complete handler.</param>
        private static void SendMessage(Peer peer, ProtocolMessage message, AtomicMessageSender.OnCompleteHandler onCompleteHandler = null)
        {
            if (peer == null)
            {
                return;
            }

            AtomicMessageSender sender = new Network.AtomicMessageSender(peer);

            if (onCompleteHandler != null)
            {
                sender.OnComplete += onCompleteHandler;
            }

            byte[] bytesMessage = new ProtocolMessageContainer(message).SerializeAsBytes();
            sender.SendBytes(bytesMessage);

            // TODO on error
        }
        /// <summary>
        /// Sends a message to peer.
        /// </summary>
        /// <param name="peer">The peer.</param>
        /// <param name="message">The message.</param>
        /// <param name="onCompleteHandler">On complete handler.</param>
        private static void SendMessage(Peer peer, ProtocolMessage message, AtomicMessageSender.OnCompleteHandler onCompleteHandler = null)
        {
            if (peer == null)
            {
                return;
            }

            AtomicMessageSender sender = new Network.AtomicMessageSender(peer);
            if (onCompleteHandler != null)
            {
                sender.OnComplete += onCompleteHandler;
            }

            byte[] bytesMessage = new ProtocolMessageContainer(message).SerializeAsBytes();
            sender.SendBytes(bytesMessage);

            // TODO on error
        }