Exemple #1
0
        public async Task <IActionResult> TestSendReceive([FromBody] XmlPageRequestBody xmlPageRequestBody)
        {
            ILogger              logger = new ConsoleLogger();
            JsonResult           jsonResult;
            RequestResponseFrame responseFrame;
            object jsonObject;

            string methodName = "TestSendReceive";

            logger.WriteEntry(methodName + " entry.");

            if (ModelState.IsValid)
            {
                logger.WriteEntry(methodName + " ModelState is valid.");

                string receiveUrl    = xmlPageRequestBody.ReceiveUrl;
                string receiveSearch = xmlPageRequestBody.ReceiveSearch;

                GeneralClassLibrary.XmlPageRequestFrame xmlPageRequestFrame = new XmlPageRequestFrame(xmlPageRequestBody);

                //	Transform the API request to an ITcpFrame.
                byte[] sendFrameBytes             = xmlPageRequestFrame.ToByteArray();
                RequestResponseFrame requestFrame = new RequestResponseFrame(sendFrameBytes);

                //	Connect to the server and obtain a network stream.
                BufferedStream bufferedStream = await SendReceiveReply.ConnectToServer(RemoteHostName, RemotePortNumber, SizeStreamBuffer, logger);

                //	Send and receive data via the stream.
                responseFrame = await SendReceiveReply.RequestReceiveResponse(bufferedStream, requestFrame, logger);

                ITcpFrame tcpFrame        = responseFrame;
                byte[]    replyFrameBytes = tcpFrame.FramePacket;

                IpcFrameBase ipcFrameBase = ( IpcFrameBase )IpcFrameBase.FromByteArray(replyFrameBytes);

                //	Transform the ITcpFrame to an API response.
                logger.WriteEntry(methodName + " building the JSON result.");
                XmlPageReplyFrame xmlPageReplyFrame = ( XmlPageReplyFrame )ipcFrameBase;
                jsonObject = new
                {
                    textArray = xmlPageReplyFrame.ResultList,
                    isSuccess = true
                };
            }
            else
            {
                logger.WriteEntry(methodName + " ModelState is not valid!");
                jsonObject = new
                {
                    textArray = new List <string>(),
                    isSuccess = false
                };
            }

            logger.WriteEntry(methodName + " exit.");

            jsonResult = new JsonResult(jsonObject);
            return(jsonResult);
        }
Exemple #2
0
        //	Handle one connection cycle of Receive - Service - Reply.
        public async void HandleClientConnection(object appDocument)
        {
            BufferedStream  bufferedStream;
            ILogger         logger;
            IServiceRequest serviceRequest;
            TcpClient       tcpClient;

            tcpClient      = TcpClient;
            logger         = Logger;
            serviceRequest = ServiceRequest;

            string methodName = "HandleClientConnection";

            logger.WriteEntry(methodName + " A connection has been made to the server.");

            NetworkStream networkStream = tcpClient.GetStream();

            bufferedStream = new BufferedStream(networkStream, SizeStreamBuffer);
            await SendReceiveReply.ReceiveServiceReply(bufferedStream, serviceRequest, logger, ( IAppDocument )appDocument);

            bufferedStream.Close();
            logger.WriteEntry(methodName + " done");
        }