public static IDictionary<string, string> FromData(byte [] data) { if (data == null) throw new ArgumentNullException ("data"); // Specialized.NameValueCollection would probably be // better, but it doesn't implement IDictionary. var pairs = new Dictionary<string,string> (); int index = 0; // Loop through the array, reading pairs at a specified // position until the end is reached. while (index < data.Length) { var pair = new NameValuePair (data, ref index); if (pairs.ContainsKey (pair.Name)) { Logger.Write (LogLevel.Warning, Strings.NameValuePair_DuplicateParameter, pair.Name); pairs [pair.Name] = pair.Value; } else pairs.Add (pair.Name, pair.Value); } return pairs; }
/// <summary> /// Receives and responds to records until all requests have /// been completed. /// </summary> /// <remarks> /// If the last received BeginRequest record is flagged /// with keep-alive, the connection will be kept alive event /// after all open requests have been completed. /// </remarks> public void Run() { Logger.Write(LogLevel.Notice, Strings.Connection_BeginningRun); do { Record record; try { record = new Record(socket, receive_buffer); } catch (System.Net.Sockets.SocketException) { StopRun(Strings.Connection_RecordNotReceived); Stop(); break; } Request request = GetRequest(record.RequestID); switch (record.Type) { // Creates a new request. case RecordType.BeginRequest: // If a request with the given ID // already exists, there's a bug in the // client. Abort. if (request != null) { StopRun(Strings.Connection_RequestAlreadyExists); break; } // If there are unfinished requests // and multiplexing is disabled, inform // the client and don't begin the // request. if (!server.MultiplexConnections && UnfinishedRequests) { EndRequest(record.RequestID, 0, ProtocolStatus.CantMultiplexConnections); break; } // If the maximum number of requests is // reached, inform the client and don't // begin the request. if (!server.CanRequest) { EndRequest(record.RequestID, 0, ProtocolStatus.Overloaded); break; } BeginRequestBody body = new BeginRequestBody (record); // If the role is "Responder", and it is // supported, create a ResponderRequest. if (body.Role == Role.Responder && server.SupportsResponder) { request = new ResponderRequest (record.RequestID, this); } // If the request is null, the role is // not supported. Inform the client and // don't begin the request. if (request == null) { Logger.Write(LogLevel.Warning, Strings.Connection_RoleNotSupported, body.Role); EndRequest(record.RequestID, 0, ProtocolStatus.UnknownRole); break; } lock (request_lock) { requests.Add(request); } keep_alive = (body.Flags & BeginRequestFlags.KeepAlive) != 0; break; // Gets server values. case RecordType.GetValues: byte [] response_data; // Look up the data from the server. try { #if NET_2_0 IDictionary <string, string> pairs_in = NameValuePair.FromData( record.GetBody()); IDictionary <string, string> pairs_out = server.GetValues( pairs_in.Keys); #else IDictionary pairs_in = NameValuePair.FromData( record.GetBody()); IDictionary pairs_out = server.GetValues( pairs_in.Keys); #endif response_data = NameValuePair.GetData(pairs_out); } catch { response_data = new byte [0]; } SendRecord(RecordType.GetValuesResult, record.RequestID, response_data); break; // Sends params to the request. case RecordType.Params: if (request == null) { StopRun(Strings.Connection_RequestDoesNotExist, record.RequestID); break; } request.AddParameterData(record.GetBody()); break; // Sends standard input to the request. case RecordType.StandardInput: if (request == null) { StopRun(Strings.Connection_RequestDoesNotExist, record.RequestID); } request.AddInputData(record); break; // Sends file data to the request. case RecordType.Data: if (request == null) { StopRun(Strings.Connection_RequestDoesNotExist, record.RequestID); } request.AddFileData(record); break; // Aborts a request when the server aborts. case RecordType.AbortRequest: if (request != null) { break; } request.Abort( Strings.Connection_AbortRecordReceived); break; // Informs the client that the record type is // unknown. default: Logger.Write(LogLevel.Warning, Strings.Connection_UnknownRecordType, record.Type); SendRecord(RecordType.UnknownType, record.RequestID, new UnknownTypeBody( record.Type).GetData()); break; } }while (!stop && (UnfinishedRequests || keep_alive)); if (requests.Count == 0) { socket.Close(); server.EndConnection(this); server.ReleaseBuffers(receive_buffer, send_buffer); } Logger.Write(LogLevel.Notice, Strings.Connection_EndingRun); }