/// <summary> /// Expect a packet. /// </summary> /// <returns>The PccrrPacket.</returns> public PccrrPacket ExpectPacket() { if (this.httpClientTransport == null) { throw new InvalidOperationException("The transport is null for not connected."); } byte[] payloadBuffer = null; this.httpClientTransport.Receive(ref payloadBuffer); PccrrPacket resp = null; resp = new PccrrUtitlity().DecodeResponseMessage(payloadBuffer); return(resp); }
/// <summary> /// Send a packet. /// </summary> /// <param name="packet">The packet need to be sent.</param> public void SendPacket(PccrrPacket packet) { if (packet == null) { throw new ArgumentNullException("packet"); } if (this.httpServerTransport == null) { throw new InvalidOperationException("The transport is null for not connected."); } byte[] bytes = new byte[] { }; switch (packet.PacketType) { case MsgType_Values.MSG_NEGO_RESP: bytes = ((PccrrNegoResponsePacket)packet).Encode(); break; case MsgType_Values.MSG_BLKLIST: bytes = ((PccrrBLKLISTResponsePacket)packet).Encode(); break; case MsgType_Values.MSG_BLK: bytes = ((PccrrBLKResponsePacket)packet).Encode(); break; } //try //{ this.httpServerTransport.Send(null, bytes, this.httpListenerContext); //} //catch (HttpListenerException ex) //{ // if (this.logger != null) // { // this.logger.AddDebug( // string.Format( // "Unexpected exception send packet failed: {0}", // ex.Message)); // } // throw; //} }
/// <summary> /// Decode a packet. /// </summary> /// <param name="uri">The uri of the request.</param> /// <param name="httpMethod">The http method.</param> /// <param name="rawdata">The rawdata.</param> /// <returns>The PccrrPacket.</returns> private PccrrPacket DecodePacket(Uri uri, HttpMethod httpMethod, byte[] rawdata) { if (uri == null) { throw new ArgumentNullException("uri"); } if (this.httpServerTransport == null) { throw new InvalidOperationException("The transport is null for not connected."); } PccrrPacket req = null; lock (this.obj) { req = new PccrrUtitlity().DecodeRequestMessage(rawdata, uri, httpMethod); return(req); } }
/// <summary> /// Verify the received message from client. /// </summary> /// <param name="remoteAddr">The remote address.</param> /// <param name="pccrrPacket">The pccrrPacket.</param> private void RetrievalTransport_Receive(IPEndPoint remoteAddr, PccrrPacket pccrrPacket) { PccrrGETBLKSRequestPacket pccrrGETBLKSRequestPacket = pccrrPacket as PccrrGETBLKSRequestPacket; if (pccrrGETBLKSRequestPacket != null) { MSG_GETBLKS msgGETBLKS = pccrrGETBLKSRequestPacket.MsgGetBLKS; MESSAGE_HEADER messageHEADER = pccrrGETBLKSRequestPacket.MessageHeader; this.sid = msgGETBLKS.SegmentID; REQUEST_MESSAGE requestMESSAGE = new REQUEST_MESSAGE(); requestMESSAGE.MESSAGEBODY = msgGETBLKS; requestMESSAGE.MESSAGEHEADER = messageHEADER; this.uiPayload = Marshal.SizeOf(messageHEADER) + Marshal.SizeOf(msgGETBLKS) + 24; PccrrBothRoleCaptureCode.CaptureBlockRangeRequirements(msgGETBLKS.ReqBlockRanges[0]); PccrrBothRoleCaptureCode.CaptureMessageHeaderRequirements(messageHEADER, this.uiPayload); this.VerifyGetBlocks(msgGETBLKS); this.VerifyRequestMessage(requestMESSAGE); PccrrBothRoleCaptureCode.CaptureSegmentIdRequirements(msgGETBLKS.SegmentID); PccrrBothRoleCaptureCode.CaptureHttpRequirements(); PccrrBothRoleCaptureCode.CaptureMessageRequirements(); this.ReceiveMsgGetBlk(msgGETBLKS.ReqBlockRanges[0].Index); } else { PccrrGETBLKLISTRequestPacket pccrrGETBLKLISTRequestPacket = pccrrPacket as PccrrGETBLKLISTRequestPacket; if (pccrrGETBLKLISTRequestPacket != null) { MSG_GETBLKLIST msgGETBLKLIST = pccrrGETBLKLISTRequestPacket.MsgGetBLKLIST; this.sid = msgGETBLKLIST.SegmentID; this.VerifyGetBlkList(msgGETBLKLIST); PccrrBothRoleCaptureCode.CaptureCommonDataTypesRequirements(msgGETBLKLIST); _BLOCKRANGE[] blockRanges = ClientHelper.ConvertFromStackBLOCKRANGEArray(msgGETBLKLIST.NeededBlockRanges); this.ReceiveMsgGetBlkList(blockRanges); } else { this.ReceiveMsgNegoReq(); } } }
/// <summary> /// Decode response message. /// </summary> /// <param name="rawdata">The raw data.</param> /// <returns>The PccrrPacket.</returns> public PccrrPacket DecodeResponseMessage(byte[] rawdata) { if (rawdata == null) { throw new ArgumentNullException("rawdata"); } if (rawdata.Length == 0) { throw new ArgumentException("The raw data should not be empty."); } int messageLength = 0; messageLength = rawdata.Length; PccrrPacket packet = null; if (messageLength > 0) { int index = 0; RESPONSE_MESSAGE ret = new RESPONSE_MESSAGE(); ret.TRANSPORTRESPONSEHEADER.Size = MarshalHelper.GetUInt32(rawdata, ref index, false); ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index); switch (ret.MESSAGEHEADER.MsgType) { case MsgType_Values.MSG_BLKLIST: PccrrBLKLISTResponsePacket pccrrBLKLISTResponsePacket = new PccrrBLKLISTResponsePacket(); MSG_BLKLIST msgBLKLIST = this.DecodeMSG_BLKLIST(rawdata, ref index); pccrrBLKLISTResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER; pccrrBLKLISTResponsePacket.MsgBLKLIST = msgBLKLIST; pccrrBLKLISTResponsePacket.MessageHeader = ret.MESSAGEHEADER; packet = pccrrBLKLISTResponsePacket; break; case MsgType_Values.MSG_BLK: PccrrBLKResponsePacket pccrrBLKResponsePacket = new PccrrBLKResponsePacket(); MSG_BLK msgBLK = this.DecodeMSG_BLK(rawdata, ref index); pccrrBLKResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER; pccrrBLKResponsePacket.MsgBLK = msgBLK; pccrrBLKResponsePacket.MessageHeader = ret.MESSAGEHEADER; packet = pccrrBLKResponsePacket; break; case MsgType_Values.MSG_NEGO_RESP: PccrrNegoResponsePacket pccrrNegoResponsePacket = new PccrrNegoResponsePacket(); MSG_NEGO_RESP msgNEGORESP = this.DecodeMSG_NEGO_RESP(rawdata, ref index); pccrrNegoResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER; pccrrNegoResponsePacket.MsgNegoResp = msgNEGORESP; pccrrNegoResponsePacket.MessageHeader = ret.MESSAGEHEADER; packet = pccrrNegoResponsePacket; break; case MsgType_Values.MSG_SEGLIST: PccrrSegListResponsePacket pccrrSegListResponsePacket = new PccrrSegListResponsePacket(); pccrrSegListResponsePacket.MsgSegList = TypeMarshal.ToStruct <MSG_SEGLIST>(rawdata, ref index); pccrrSegListResponsePacket.TransportResponseHeader = ret.TRANSPORTRESPONSEHEADER; pccrrSegListResponsePacket.MessageHeader = ret.MESSAGEHEADER; packet = pccrrSegListResponsePacket; break; } } return(packet); }
/// <summary> /// Decode request message. /// </summary> /// <param name="rawdata">The raw data.</param> /// <param name="uri">The request uri.</param> /// <param name="method">The request method.</param> /// <returns>The PccrrPacket.</returns> public PccrrPacket DecodeRequestMessage(byte[] rawdata, Uri uri, HttpMethod method) { if (rawdata == null) { throw new ArgumentNullException("rawdata"); } if (rawdata.Length == 0) { throw new ArgumentException("The rawdata should not be empty."); } int messageLength = 0; messageLength = rawdata.Length; PccrrPacket packet = null; if (messageLength > 0) { int index = 0; REQUEST_MESSAGE ret = new REQUEST_MESSAGE(); ret.MESSAGEHEADER = this.DecodeMessageHeader(rawdata, ref index); switch (ret.MESSAGEHEADER.MsgType) { case MsgType_Values.MSG_NEGO_REQ: PccrrNegoRequestPacket pccrrNegoRequestPacket = new PccrrNegoRequestPacket(); MSG_NEGO_REQ msgNEGOREQ = this.DecodeMSG_NEGO_REQ(rawdata, ref index); pccrrNegoRequestPacket.MsgNegoReq = msgNEGOREQ; pccrrNegoRequestPacket.MessageHeader = ret.MESSAGEHEADER; pccrrNegoRequestPacket.Method = method; pccrrNegoRequestPacket.Uri = uri; packet = pccrrNegoRequestPacket; break; case MsgType_Values.MSG_GETBLKLIST: PccrrGETBLKLISTRequestPacket pccrrGETBLKLISTRequestPacket = new PccrrGETBLKLISTRequestPacket(); MSG_GETBLKLIST msgGETBLKLIST = this.DecodeMSG_GETBLKLIST(rawdata, ref index); pccrrGETBLKLISTRequestPacket.MsgGetBLKLIST = msgGETBLKLIST; pccrrGETBLKLISTRequestPacket.MessageHeader = ret.MESSAGEHEADER; pccrrGETBLKLISTRequestPacket.Method = method; pccrrGETBLKLISTRequestPacket.Uri = uri; packet = pccrrGETBLKLISTRequestPacket; break; case MsgType_Values.MSG_GETBLKS: PccrrGETBLKSRequestPacket pccrrGETBLKSRequestPacket = new PccrrGETBLKSRequestPacket(); MSG_GETBLKS msgGETBLKS = this.DecodeMSG_GETBLKS(rawdata, ref index); pccrrGETBLKSRequestPacket.MsgGetBLKS = msgGETBLKS; pccrrGETBLKSRequestPacket.MessageHeader = ret.MESSAGEHEADER; pccrrGETBLKSRequestPacket.Method = method; pccrrGETBLKSRequestPacket.Uri = uri; packet = pccrrGETBLKSRequestPacket; break; case MsgType_Values.MSG_GETSEGLIST: PccrrGetSegListRequestPacket pccrrGetSegListRequestPacket = new PccrrGetSegListRequestPacket(); pccrrGetSegListRequestPacket.MsgGetSegList = TypeMarshal.ToStruct <MSG_GETSEGLIST>(rawdata, ref index); pccrrGetSegListRequestPacket.MessageHeader = ret.MESSAGEHEADER; pccrrGetSegListRequestPacket.Method = method; pccrrGetSegListRequestPacket.Uri = uri; packet = pccrrGetSegListRequestPacket; break; } } return(packet); }
/// <summary> /// The handle of MessageArrivedEventArgs happened. /// </summary> /// <param name="remoteAddr">Represents the remote endpoint address</param> /// <param name="pccrrPacket">The received pccrrPacket, used to judge whether /// it is a PccrrGETBLKLISTRequestPacket</param> private void RetrievalTransport_Receive(IPEndPoint remoteAddr, PccrrPacket pccrrPacket) { if (pccrrPacket.PacketType == MsgType_Values.MSG_GETBLKLIST) { PccrrGETBLKLISTRequestPacket package = (PccrrGETBLKLISTRequestPacket)pccrrPacket; int port = package.Uri.Port; byte[] segmentID = package.MsgGetBLKLIST.SegmentID; this.ReceivePccrrRequestHandler(this, new ReceivedPccrrRequestEventArg(port, segmentID)); } else if (pccrrPacket.PacketType == MsgType_Values.MSG_GETBLKS) { PccrrGETBLKSRequestPacket package = (PccrrGETBLKSRequestPacket)pccrrPacket; int port = package.Uri.Port; byte[] segmentID = package.MsgGetBLKS.SegmentID; this.ReceivePccrrRequestHandler(this, new ReceivedPccrrRequestEventArg(port, segmentID)); } }
/// <summary> /// Send a packet. /// </summary> /// <param name="packet">The packet need to be sent.</param> /// <param name="timeout">The timeout.</param> public void SendPacket(PccrrPacket packet, TimeSpan timeout) { if (packet == null) { throw new ArgumentNullException("packet"); } if (timeout == null) { throw new ArgumentNullException("timeout"); } if (this.httpClientTransport == null) { throw new InvalidOperationException("The transport is null for not connected."); } byte[] bytes = new byte[] { }; switch (packet.PacketType) { case MsgType_Values.MSG_NEGO_REQ: bytes = ((PccrrNegoRequestPacket)packet).Encode(); break; case MsgType_Values.MSG_GETBLKLIST: bytes = ((PccrrGETBLKLISTRequestPacket)packet).Encode(); break; case MsgType_Values.MSG_GETBLKS: bytes = ((PccrrGETBLKSRequestPacket)packet).Encode(); break; case MsgType_Values.MSG_GETSEGLIST: bytes = ((PccrrGetSegListRequestPacket)packet).Encode(); break; default: throw new InvalidOperationException( string.Format( "The packet type {0} is not supported on client side.", packet.PacketType)); } try { this.httpClientTransport.Send(HttpVersion.Version11, null, bytes, CommonStack.HttpMethod.POST, (int)timeout.TotalMilliseconds); } catch (WebException ex) { if (this.logger != null) { this.logger.AddDebug( string.Format( "Unexpected exception send packet failed: {0}", ex.Message)); } } catch (ObjectDisposedException ex) { if (this.logger != null) { this.logger.AddDebug( string.Format( "Unexpected exception send packet failed: {0}", ex.Message)); } } }
void pccrrServer_MessageArrived(System.Net.IPEndPoint sender, PccrrPacket pccrrPacket) { var pccrrNegoRequest = pccrrPacket as PccrrNegoRequestPacket; if (pccrrNegoRequest != null) { HandlePccrrNegoRequestPacket(pccrrNegoRequest); goto logevent; } var pccrrGetBlkListRequest = pccrrPacket as PccrrGETBLKLISTRequestPacket; if (pccrrGetBlkListRequest != null) { HandlePccrrGETBLKLISTRequestPacket(pccrrGetBlkListRequest); goto logevent; } var pccrrGetBlkRequest = pccrrPacket as PccrrGETBLKSRequestPacket; if (pccrrGetBlkRequest != null) { HandlePccrrGETBLKSRequestPacket(pccrrGetBlkRequest); goto logevent; } var pccrrGetSegListRequest = pccrrPacket as PccrrGetSegListRequestPacket; if (pccrrGetSegListRequest != null) { HandlePccrrGetSegListRequestPacket(pccrrGetSegListRequest); goto logevent; } // Unknown packet throw new NotImplementedException("Unknown PCCRR message type " + pccrrPacket.PacketType); logevent: eventQueue.LogEvent(typeof(PccrrServer).GetEvent("MessageArrived"), sender, pccrrPacket); }