Пример #1
0
 protected void SocketCloseAndDispose()
 {
     if (_cachedSocket != null)
     {
         _cachedSocket.Dispose();
         _cachedSocket = null;
     }
 }
Пример #2
0
        public IDictionary<string, string> DoRequest(string command, IDictionary<string, string> args)
        {
            if (command == null) throw new ArgumentNullException("command");
            if (args == null) throw new ArgumentNullException("args");

            var argString = EncodeUrlParameters(args);
            var request = command + " " + argString + "\r\n";

            // Try first with the cached socket. If no good, we try again, if we get exception then, we throw..
            for (int i = 0; i < 2; i++) {
                if (_cachedSocket != null) {
                    try {
                        _cachedSocket.Writer.Write(request);
                        _cachedSocket.Writer.Flush();
                        break;
                    }
                    catch (Exception ex) {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                        SocketCloseAndDispose();
                        if (i == 1)
                            throw new TrackerCommunicationException("problem finding a working tracker");
                    }
                }
                if (_cachedSocket == null) {
                    _cachedSocket = GetSocketReaderAndWriter();
                }
            }
            try {
                // ok - we finally got a message off to a tracker
                // now get a response
                var response = _cachedSocket.Reader.ReadLine();

                if (response == null)
                    throw new TrackerCommunicationException(
                        "Received null response from tracker at " + _cachedSocket.EndPoint);

                var ok = OK_PATTERN.Match(response);
                if (ok.Success) {
                    return DecodeUrlString(ok.Groups[ARGS_PART].Value);
                }

                var err = ERROR_PATTERN.Match(response);
                if (err.Success)
                {
                    // error response
                    LastErr = err.Groups[ERR_PART].Value;
                    LastErrStr = err.Groups[ERRSTR_PART].Value;

                    return null;
                }

                throw new TrackerCommunicationException(
                    "invalid server response from "
                    + _cachedSocket.EndPoint + ": "
                    + response);

            } catch (Exception ex) {
                throw new TrackerCommunicationException(
                    "problem talking to server at "
                    + _cachedSocket.EndPoint, ex);
            }
        }
Пример #3
0
 protected void SocketCloseAndDispose()
 {
     if (_cachedSocket != null) {
         _cachedSocket.Dispose();
         _cachedSocket = null;
     }
 }
Пример #4
0
        public IDictionary <string, string> DoRequest(string command, IDictionary <string, string> args)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }
            if (args == null)
            {
                throw new ArgumentNullException("args");
            }

            var argString = EncodeUrlParameters(args);
            var request   = command + " " + argString + "\r\n";

            // Try first with the cached socket. If no good, we try again, if we get exception then, we throw..
            for (int i = 0; i < 2; i++)
            {
                if (_cachedSocket != null)
                {
                    try {
                        _cachedSocket.Writer.Write(request);
                        _cachedSocket.Writer.Flush();
                        break;
                    }
                    catch (Exception ex) {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                        SocketCloseAndDispose();
                        if (i == 1)
                        {
                            throw new TrackerCommunicationException("problem finding a working tracker");
                        }
                    }
                }
                if (_cachedSocket == null)
                {
                    _cachedSocket = GetSocketReaderAndWriter();
                }
            }
            try {
                // ok - we finally got a message off to a tracker
                // now get a response
                var response = _cachedSocket.Reader.ReadLine();

                if (response == null)
                {
                    throw new TrackerCommunicationException(
                              "Received null response from tracker at " + _cachedSocket.EndPoint);
                }

                var ok = OK_PATTERN.Match(response);
                if (ok.Success)
                {
                    return(DecodeUrlString(ok.Groups[ARGS_PART].Value));
                }

                var err = ERROR_PATTERN.Match(response);
                if (err.Success)
                {
                    // error response
                    LastErr    = err.Groups[ERR_PART].Value;
                    LastErrStr = err.Groups[ERRSTR_PART].Value;

                    return(null);
                }

                throw new TrackerCommunicationException(
                          "invalid server response from "
                          + _cachedSocket.EndPoint + ": "
                          + response);
            } catch (Exception ex) {
                throw new TrackerCommunicationException(
                          "problem talking to server at "
                          + _cachedSocket.EndPoint, ex);
            }
        }