Esempio n. 1
0
        public void ReadCallback(IAsyncResult ar)
        {
            try
            {
                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;

                // Read data from the client socket.
                int bytesRead = handler.EndReceive(ar);

                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));

                    // Check for end-of-file tag. If it is not there, read
                    // more data.
                    content = state.sb.ToString();
                    if (content.IndexOf("<EOF>") > -1)
                    {
                        // All the data has been read from the
                        // client.

                        StatusResponse response1 = null;
                        try
                        {
                            DataRequestBatch requestBatch = Encoder.DecodeDataRequestBatch(content);
                            Logger.Write(Logger.Tag.INFO, "Received DataRequestBatch from " + Params.NODE_NAME + ", with batch size: " + requestBatch.Batch.Count.ToString());
                            response1 = MakeResponse(requestBatch);
                        } catch
                        {
                            Logger.Write(Logger.Tag.ERROR, "Failed to parse DataRequestBatch.");
                            response1 = new StatusResponse()
                            {
                                Status = new Dictionary <string, bool>()
                            };
                        }

                        Send(handler, Encoder.EncodePointResponse(response1));
                    }
                    else
                    {
                        // Not all data received. Get more.
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                             new AsyncCallback(ReadCallback), state);
                    }
                }
            } catch (Exception e)
            {
                Logger.Write(Logger.Tag.ERROR, e.ToString());
            }
        }
Esempio n. 2
0
        private StatusResponse MakeResponse(DataRequestBatch requestBatch)
        {
            Dictionary <string, bool> response = new Dictionary <string, bool>();

            foreach (DataRequest request in requestBatch.Batch)
            {
                // request.T3 = Utils.Micros.ToString();
                handler.Requests.Enqueue(request);
                // Logger.Write(Logger.Tag.INFO, "Enqueued request: " + handler.Requests.Count.ToString());
                response.Add(request.ID, true);
            }

            return(new StatusResponse()
            {
                Status = response
            });
        }
Esempio n. 3
0
 public static byte[] EncodeDataRequestBatch(DataRequestBatch msg)
 {
     return(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(msg, Formatting.None) + "<EOF>"));
 }