コード例 #1
0
        public static byte[] GetResponse(byte[] receivedData)
        {
            FileTransferRequest request = RequestHandler.RestoreRequest <FileTransferRequest>(receivedData);

            IEnumerable <BasicDataset> predictions = GetPrediction(request);

            PredictionResponse response = CreateResponse(predictions);

            return(ResponseManager.CreateByteResponse(response));
        }
コード例 #2
0
        public static void ReadCallback(IAsyncResult ar)
        {
            string content = string.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            int bytesRead;

            try
            {
                // Read data from the client socket.
                bytesRead = handler.EndReceive(ar);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message + " Error code: " + ex.ErrorCode);

                ShutdownSocket(handler);

                return;
            }

            if (bytesRead > 0)
            {
                // There  might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(
                                    state.buffer, 0, bytesRead));

                state.receivedData.Add(state.buffer.Take(bytesRead).ToArray());

                // Check for end-of-file tag. If it is not there, read
                // more data.

                if (state.totalBytesExpected == 0)
                {
                    try
                    {
                        state.totalBytesExpected = RequestHandler.ReadRequestLength(state.buffer) + sizeof(int);
                    }
                    catch (Exception ex)
                    {
                        if (ex is OverflowException || ex is ArgumentOutOfRangeException)
                        {
                            Console.WriteLine(ex.Message);

                            Response clientErrorResponse = new Response((int)ResponseCode.Declinded);

                            byte[] responseBytes = ResponseManager.CreateByteResponse(clientErrorResponse);

                            Send(handler, responseBytes);

                            return;
                        }

                        throw;
                    }
                }

                state.totalBytesRead += bytesRead;

                if (state.totalBytesRead >= state.totalBytesExpected)
                {
                    try
                    {
                        // All the data has been read from the
                        // client. Display it on the console.
                        byte[] data = state.receivedData.SelectMany(a => a).ToArray(),
                        response = Controller.GetResponse(data);

                        Console.WriteLine("Read {0} bytes from socket.", data.Length);

                        // Echo the data back to the client.
                        Send(handler, response);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);

                        Response serverErrorResponse = new Response((int)ResponseCode.Error);

                        byte[] responseBytes = ResponseManager.CreateByteResponse(serverErrorResponse);

                        Send(handler, responseBytes);
                    }
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                         new AsyncCallback(ReadCallback), state);
                }
            }
            else
            {
                Response clientErrorResponse = new Response((int)ResponseCode.Declinded);

                byte[] responseBytes = ResponseManager.CreateByteResponse(clientErrorResponse);

                Send(handler, responseBytes);
            }
        }