UdpProtocol.cs :  » Business-Application » DC » DCSharp » Backend » Protocols » Nmdc » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » DC 
DC » DCSharp » Backend » Protocols » Nmdc » UdpProtocol.cs
/* 
 * Copyright (C) 2004-2005 Jonathan Bindel
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

using DCSharp.Backend.Connections;
using DCSharp.Backend.Objects;

namespace DCSharp.Backend.Protocols.Nmdc{
  /// <summary>
  /// A class that handles NMDC messages sent over Udp.
  /// </summary>
  public class UdpProtocol : Protocol
  {
    private const int MaxActiveResults = 10;
    private const int MaxPassiveResults = 5;
    
    public UdpProtocol(ISearchResultHandler handler,
      ILegacyUserIdentifier userIdentifier)
    {
      if (handler == null)
      {
        throw new ArgumentNullException("handler");
      }
      if (userIdentifier == null)
      {
        throw new ArgumentNullException("userIdentifier");
      }
      this.handler = handler;
      this.userIdentifier = userIdentifier;
    }
    
    /// <summary>
    /// 
    /// </summary>
    public ISearchResultHandler ResultHandler
    {
      get { return handler; }
    }
    private ISearchResultHandler handler;
    
    /// <summary>
    /// 
    /// </summary>
    public ILegacyUserIdentifier UserIdentifier
    {
      get { return userIdentifier; }
    }
    private ILegacyUserIdentifier userIdentifier;
    
    protected override void Invoke(string command, string argument)
    {
      if (command == "$SR")
      {
        ParseSearchResult(argument);
      }
      else
      {
        base.Invoke(command, argument);
      }
    }
    
    protected void ParseSearchResult(string argument)
    {
      // $SR <nick> <result> <free slots>/<total slots><0x05><hubname> (<hubip:port>)
      //
      // <result> is one of the following:
      //
      // - For files: <filename><0x05><filesize>
      // - For directories: <directory>
      //
      // If it's a TTH search <hubname> is: TTH:<tth>
      
      int i = 0;
      
      int j = argument.IndexOf(' ');
      if (j <= 0)
        return;
      
      SearchResult result = new SearchResult();
      
      string nick = argument.Substring(0, j);
      
      string remaining = argument.Substring(j + 1);
      char bit = (char)5;
      int count = Regex.Matches(remaining, bit.ToString()).Count;
      
      if (count == 1)
      {
        // We have a directory...find the first space beyond the first 0x05 from the back 
        // (dirs might contain spaces as well...clever protocol, eh?)
        result.Type = ResultType.Directory;
        
        // Get past the hubname that might contain spaces
        j = remaining.LastIndexOf(bit);
        if (j < 0)
          return;
        
        j = remaining.LastIndexOf(' ', j - 1);
        if (j < 0)
          return;
        
        result.Path = remaining.Substring(0, j);
      }
      else if (count == 2)
      {
        result.Type = ResultType.File;
        
        j = remaining.IndexOf(bit);
        result.Path = remaining.Substring(0, j);
        
        i = j + 1;
        long size;
        
        j = remaining.IndexOf(' ', i);
        if (j < 0 || !long.TryParse(remaining.Substring(i, j - i), out size))
          return;
        
        result.Size = size;
      }
      else
      {
        return;
      }
      
      int slots;
      
      i = j + 1;
      j = remaining.IndexOf('/', i);
      if (j < 0 || !int.TryParse(remaining.Substring(i, j - i), out slots))  
        return;
      
      result.FreeSlots = slots;
      
      i = j + 1;
      j = remaining.IndexOf(bit, i);
      if (j < 0 || !int.TryParse(remaining.Substring(i, j - i), out slots))
        return;
      
      result.Slots = slots;
      
      i = j + 1;
      j = remaining.LastIndexOf(" (");
      if (j < 0)
        return;
      
      string hubName = remaining.Substring(i, j - i);
      if (hubName.StartsWith("TTH:"))
      {
        result.TTH = hubName.Substring(4);
        hubName = null;
      }
      
      i = j + 2;
      j = remaining.IndexOf(')', i);
      if (j < 0)
        return;
      
      string hubAddress = remaining.Substring(i, j - i);
      
      result.User = GetIdentity(nick, hubName, hubAddress);
      if (result.User != null)
      {
        handler.HandleSearchResult(result);
      }
    }
    
    protected virtual Identity GetIdentity(string nick, string hubName,
      string hubAddress)
    {
      Identity identity;
      HubConnection hub;
      
      userIdentifier.IdentifyUser(nick, hubName, hubAddress,
        out hub, out identity);
      
      return identity;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.