XBMSPUtility.cs :  » Game » Media-File-XStream » MediaStream » 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 » Game » Media File XStream 
Media File XStream » MediaStream » XBMSPUtility.cs
/* 
Media File XStream, Network file stream server supporting XBMSP
Copyright (C) 2004  j3g

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

This project can be found on SourceForge.
http://sourceforge.net/projects/mfxstream
*/
using System;
using System.Collections;

namespace MediaStream{
  /// <summary>
  /// 
  /// </summary>
  public class CXBMSPUtility : CConvertUtility
  {
    private CShares _Shares;
    private CFileReader _CurrentFile;
    private ShareItem _ShareItem = new ShareItem();
    private string _Path;
    private ArrayList _FileList;
    private int _FileListCounter;
    private string _strAuth;
    private Boolean _blnAuthOk = false;

    public CShares GetShares { get { return(this._Shares); } set { this._Shares = value; } }

    public CXBMSPUtility()
    {
      this._blnAuthOk = false;
      this._FileList = null;
      this._CurrentFile = null;
    }
    private void LogDebug1(String strMessage)
    {
      CLogFile.Log(strMessage, "CXBMSPUtility", LogLevel.DebugLevel1);
    }
    private void LogDebug2(String strMessage)
    {
      CLogFile.Log(strMessage, "CXBMSPUtility", LogLevel.DebugLevel2);
    }

    public Boolean CheckAuthentication(CIPConnection pConn, Byte [] xsMsgId, XBMSPClient xsMessage)
    {
      if (!CSettings.GetAuthenication) return (true);
      if (_blnAuthOk) return (true);
      if (XBMSPClient.XBMSP_PACKET_AUTHENTICATE == xsMessage || 
        XBMSPClient.XBMSP_PACKET_AUTHENTICATION_INIT == xsMessage) return (true);
      if (CSettings.CheckTrust(pConn.GetIPAddress)) return (true);

      SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_AUTHENTICATION_NEEDED, null);
      return (false);
    }

    #region HandlePacket Functions
    public void HandlePacketSetCwd(CIPConnection pConn, Byte [] xsMsgId)
    {
      Int32 nFolderLength = BufferToInt32(pConn.GetMessage, 9);
      Boolean blnOk = true;
      String strFolder = "";

      if (0 != nFolderLength)
      {
        strFolder = ByteArrayToString(pConn.GetMessage, 13, nFolderLength);
        switch (strFolder)
        {
          case "/" :
            this._Path = "/";
            break;
          case "." :
            break;
          case ".." :
            int nPos = this._Path.LastIndexOf("/");
            if (0 == nPos)
              this._Path = "/";
            else
              this._Path.Substring(0, nPos);
            break;
          default :
            if (this._Path.Equals("/"))
              this._Path = "/" + strFolder;
            else
              this._Path += "/" + strFolder;
            break;
        }
      }
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("SetCwd(" + strFolder + "):" + (blnOk ? "Ok" : "Error"));
      if (blnOk)
        SendPacketOk(pConn, xsMsgId);
      else
        SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_NO_SUCH_FILE, "");
    }
    public void HandlePacketFileListOpen(CIPConnection pConn, Byte [] xsMsgId)
    {
      this._FileList = this._Shares[this._Path];
      this._FileListCounter = 0;
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("FileListOpen():Handle(" + this._FileList.GetHashCode() + ")");

      if (null != this._FileList)
        SendPacketHandle(pConn, xsMsgId, this._FileList.GetHashCode());
      else
        SendPacketError(pConn, xsMsgId, XBMSPError.XBMSP_ERROR_OPEN_FAILED, null);

    }
    public void HandlePacketFileListRead(CIPConnection pConn, Byte [] xsMsgId)
    {
      if (this._FileListCounter < this._FileList.Count)
      {
        ShareItem si = (ShareItem) this._FileList[this._FileListCounter++];
        if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
          LogDebug1("FileListRead(), File " + si.Name + ":FileData");
        SendPacketFileData(pConn, xsMsgId, si.Name, CShares.GetXBMSPXml(si));
      }
      else
      {
        if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
          LogDebug1("FileListRead(), Not found.");
        SendPacketFileData(pConn, xsMsgId, null, null);
      }
    }
    public void HandlePacketFileInfo(CIPConnection pConn, Byte [] xsMsgId)
    {
      int nFileLength = BufferToInt32(pConn.GetMessage, 9);
      String strFile = "";
      
      if (0 != nFileLength)
      {
        strFile = ByteArrayToString(pConn.GetMessage, 13, nFileLength);
        this._ShareItem = this._Shares.GetShareItem(this._Path, strFile);
        if (null != this._ShareItem)
        {
          if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
            LogDebug1("FileInfo(" + strFile + "): FileData");
          SendPacketFileData(pConn, xsMsgId, this._ShareItem.Name, CShares.GetXBMSPXml(this._ShareItem));
          return;
        }
      }
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("FileInfo(" + strFile + "): Error");
      SendPacketError(pConn, xsMsgId, XBMSPError.XBMSP_ERROR_NO_SUCH_FILE,"");
    }
    public void HandlePacketFileOpen(CIPConnection pConn, Byte [] xsMsgId)
    {
      int nFileLength = BufferToInt32(pConn.GetMessage, 9);
      Boolean blnOk = false;
      String strFile = "";

      if (0 != nFileLength)
      {
        strFile = ByteArrayToString(pConn.GetMessage, 13, nFileLength);
        this._ShareItem = this._Shares.GetShareItem(this._Path, strFile);
        if (null != this._ShareItem)
        {
          this._CurrentFile = new CFileReader(this._ShareItem.Path);
          blnOk = this._CurrentFile.Open();
        }
      }    
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("FileOpen(" + strFile + "):" + (blnOk ? ("Handle(" + this._CurrentFile.GetHashCode() + ")") : "Error"));
      if (blnOk)
        SendPacketHandle(pConn, xsMsgId, this._CurrentFile.GetHashCode());
      else
        SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_INVALID_FILE,"");
    }
    public void HandlePacketFileRead(CIPConnection pConn, Byte [] xsMsgId)
    {
      Int32 nHandle = BufferToInt32(pConn.GetMessage, 9);
      int nFileLength = 0;
      Byte [] pbResult = null;
      if (null != this._CurrentFile)
      {
        nFileLength = BufferToInt32(pConn.GetMessage, 13);
        pbResult = this._CurrentFile.Read(nFileLength);
        if (LogLevel.DebugLevel2 == CSettings.GetLogLevel)
        {
          if (null == pbResult)
            LogDebug2("FileRead(" + this._CurrentFile.Name + "), " + nFileLength + "->0" + ":Ok");
          else
            LogDebug2("FileRead(" + this._CurrentFile.Name + "), " + nFileLength + "->" + pbResult.Length + ":Ok");
        }
        SendPacketFileContents(pConn, xsMsgId, pbResult);
      }
      else
      {
        if (LogLevel.DebugLevel2 == CSettings.GetLogLevel)
          LogDebug2("FileRead(" + nHandle + "), " + nFileLength + ":Error");
        SendPacketError(pConn, xsMsgId, XBMSPError.XBMSP_ERROR_INVALID_HANDLE,"");
      }
    }
    public void HandlePacketFileSeek(CIPConnection pConn, Byte [] xsMsgId)
    {
      Int32 nHandle = BufferToInt32(pConn.GetMessage, 9);
      FileSeekType xsSeekType = (FileSeekType) pConn.GetMessage[13];;
      long xsSeekLength = BufferToLong(pConn.GetMessage, 14);

      Boolean blnOk = false;
      if (null != this._CurrentFile)
      {
        if (this._CurrentFile.Seek(xsSeekType, xsSeekLength))
          blnOk = true;
      }

      if (blnOk)
      {
        if (LogLevel.DebugLevel2 == CSettings.GetLogLevel)
          LogDebug2("FileSeek(" + this._CurrentFile.Name + "), " + xsSeekType.ToString() + ", " + xsSeekLength + ":Ok");
        SendPacketOk(pConn, xsMsgId);
      }
      else
      {
        if (LogLevel.DebugLevel2 == CSettings.GetLogLevel)
          LogDebug2("FileSeek(" + this._CurrentFile.Name + "), " + xsSeekType.ToString() + ", " + xsSeekLength + ":Error");
        SendPacketError(pConn, xsMsgId, XBMSPError.XBMSP_ERROR_INVALID_HANDLE,"");
      }
    }
    public void HandlePacketClose(CIPConnection pConn, Byte [] xsMsgId)
    {
      if (null != this._CurrentFile)
      {
        this._CurrentFile.Close();
        this._CurrentFile = null;
      }
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("Close():Ok");
      SendPacketOk(pConn, xsMsgId);
    }
    public void HandlePacketCloseAll(CIPConnection pConn, Byte [] xsMsgId)
    {
//      _xsCurrent.Close();
      if (null != this._CurrentFile)
        this._CurrentFile.Close();
      this._CurrentFile = null;
      this._FileList = null;
      this._FileListCounter = 0;
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("CloseAll():Ok");
      SendPacketOk(pConn, xsMsgId);
    }
    public void HandlePacketUpCwd(CIPConnection pConn, Byte [] xsMsgId)
    {
      int nPos = this._Path.LastIndexOf("/");
      if (0 == nPos)
        this._Path = "/";
      else
        this._Path.Substring(0, nPos);
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("UpCwd():Ok");
      SendPacketOk(pConn, xsMsgId);
    }
    public void HandlePacketAuthenticationInit(CIPConnection pConn, Byte [] xsMsgId)
    {
      Int32 xsStrLength = BufferToInt32(pConn.GetMessage, 9);
      Boolean blnOk = false;
      if (0 != xsStrLength)
      {
        _strAuth = ByteArrayToString(pConn.GetMessage, 13, xsStrLength);
        if (_strAuth.StartsWith("password"))
          blnOk = true;

      }

      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("AuthenticationInit(" + _strAuth + "):" + (blnOk ? "Ok" : "Error"));
      if (blnOk)
        SendPacketHandle(pConn, xsMsgId, 99);
      else
        SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_AUTHENTICATION_FAILED,"");
    }
    public void HandlePacketAuthenticate(CIPConnection pConn, Byte [] xsMsgId)
    {
      String xsPW = "";
      String xsUN = "";
      if (_strAuth.StartsWith("password"))
      {
        Int32 xsUNLength = BufferToInt32(pConn.GetMessage, 13);
        Int32 xsPWLength = BufferToInt32(pConn.GetMessage, xsUNLength + 17);
        if (0 != xsUNLength)
        {

          xsUN = ByteArrayToString(pConn.GetMessage, 17, xsUNLength);
          if (0 != xsPWLength)
          {
            xsPW = ByteArrayToString(pConn.GetMessage, xsUNLength + 21, xsPWLength);
          }
          if (CSettings.CheckUser(xsUN, xsPW))
            _blnAuthOk = true;
          else
            _blnAuthOk = false;
        }
      }
      if (LogLevel.DebugLevel1 <= CSettings.GetLogLevel)
        LogDebug1("Authenticate(" + xsUN + "):" + (_blnAuthOk ? "Ok" : "Error"));
      if (_blnAuthOk)
        SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_AUTHENTICATION_FAILED,"");
      else
        SendPacketError(pConn, xsMsgId,XBMSPError.XBMSP_ERROR_AUTHENTICATION_FAILED,"");
    }
    #endregion

    #region SendPacket Functions
    public void SendPacketOk(CIPConnection pConn, Byte [] msgId)
    {
      Byte [] pbMessage = new Byte[9];
      pbMessage[3] = (Byte) 5;
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_OK;
      msgId.CopyTo(pbMessage, 5);

      pConn.Send(pbMessage);
    }
    public void SendPacketError(CIPConnection pConn, Byte [] msgId, XBMSPError msgError, String msgText)
    {
      Byte [] pbText = null;
      Int32 nTextLength = 0;
      if (null != msgText)
      {
//        pbText = System.Text.Encoding.ASCII.GetBytes(msgText);
        pbText = StringToByteArray(msgText);
        nTextLength = pbText.Length;
      }
      Byte [] pbMessage = new Byte[nTextLength + 14];

      Int32ToBuffer(pbMessage, nTextLength + 10, 0);
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_ERROR;
      msgId.CopyTo(pbMessage, 5);

      pbMessage[10] = (Byte) msgError;

      if (null != msgText)
      {
        Int32ToBuffer(pbMessage, nTextLength, 10);
        pbText.CopyTo(pbMessage, 14);
      }

      pConn.Send(pbMessage);
    }
    public void SendPacketHandle(CIPConnection pConn, Byte [] msgId, Int32 msgHandle)
    {
      Byte [] pbMessage = new Byte[13];
      pbMessage[3] = (Byte) 9;
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_HANDLE;
      msgId.CopyTo(pbMessage, 5);

      Int32ToBuffer(pbMessage, msgHandle, 9);

      pConn.Send(pbMessage);
    }
    public void SendPacketFileData(CIPConnection pConn, Byte [] msgId, String msgFileName, String msgFileInfo)
    {
      Byte [] pbMessage;
      Byte [] pbFileName = null;
      Int32 nFileNameLength = 0;
      Byte [] pbFileInfo = null;
      Int32 nFileInfoLength = 0;
      if (null != msgFileName)
      {
//        pbFileName = System.Text.Encoding.UTF8.GetBytes(msgFileName);
        pbFileName = StringToByteArray(msgFileName);
        nFileNameLength = pbFileName.Length;
//        pbFileInfo = System.Text.Encoding.UTF8.GetBytes(msgFileInfo);
        pbFileInfo = StringToByteArray(msgFileInfo);
        nFileInfoLength = pbFileInfo.Length;
        pbMessage = new Byte[nFileNameLength + nFileInfoLength + 17];
        Int32ToBuffer(pbMessage, nFileNameLength + nFileInfoLength + 13, 0);
      }
      else
      {
        pbMessage = new Byte[nFileNameLength + nFileInfoLength + 13];
        Int32ToBuffer(pbMessage, nFileNameLength + nFileInfoLength + 9, 0);
      }

      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_FILE_DATA;
      msgId.CopyTo(pbMessage, 5);

      if (null != msgFileName)
      {
        Int32ToBuffer(pbMessage, nFileNameLength, 9);
        pbFileName.CopyTo(pbMessage, 13);
        Int32ToBuffer(pbMessage, nFileInfoLength, nFileNameLength + 13);
        pbFileInfo.CopyTo(pbMessage, nFileNameLength + 17);
      }
      pConn.Send(pbMessage);
    }
    public void SendPacketFileContents(CIPConnection pConn, Byte [] msgId, String msgFileContents)
    {
      Byte [] pbFileContents = null;
      Int32 nFileContentsLength = 0;
      if (null != msgFileContents)
      {
//        pbFileContents = System.Text.Encoding.ASCII.GetBytes(msgFileContents);
        pbFileContents = StringToByteArray(msgFileContents); 
        nFileContentsLength = pbFileContents.Length;
      }
      Byte [] pbMessage = new Byte[nFileContentsLength + 13];

      Int32ToBuffer(pbMessage, nFileContentsLength + 9, 0);
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_FILE_CONTENTS;
      msgId.CopyTo(pbMessage, 5);

      if (null != msgFileContents)
      {
        Int32ToBuffer(pbMessage, nFileContentsLength, 9);
        pbFileContents.CopyTo(pbMessage, 13);
      }

      pConn.Send(pbMessage);
    }
    public void SendPacketFileContents(CIPConnection pConn, Byte [] msgId, Byte [] msgFileContents)
    {
      Int32 nFileContentsLength = 0;
      if (null != msgFileContents)
      {
        nFileContentsLength = msgFileContents.Length;
      }
      Byte [] pbMessage = new Byte[nFileContentsLength + 13];

      Int32ToBuffer(pbMessage, nFileContentsLength + 9, 0);
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_FILE_CONTENTS;
      msgId.CopyTo(pbMessage, 5);

      if (null != msgFileContents)
      {
        Int32ToBuffer(pbMessage, nFileContentsLength, 9);
        msgFileContents.CopyTo(pbMessage, 13);
      }

      pConn.Send(pbMessage);
    }
    public void SendPacketAuthenticationContinue(CIPConnection pConn, Byte [] msgId, Byte [] msgAuthData)
    {
      Int32 nAuthDataLength = 0;
      if (null != msgAuthData)
        nAuthDataLength = msgAuthData.Length;

      Byte [] pbMessage = new Byte[nAuthDataLength + 9];

      Int32ToBuffer(pbMessage, nAuthDataLength + 5, 0);
      pbMessage[4] = (Byte) XBMSPServer.XBMSP_PACKET_AUTHENTICATION_CONTINUE;
      msgId.CopyTo(pbMessage, 5);

      if (null != msgAuthData)
        msgAuthData.CopyTo(pbMessage, 9);

      pConn.Send(pbMessage);
    }
    #endregion

  }

  public enum XBMSPClient
  {
    XBMSP_PACKET_NULL                    =  10,
    XBMSP_PACKET_SETCWD                  =  11,
    XBMSP_PACKET_FILELIST_OPEN           =  12,
    XBMSP_PACKET_FILELIST_READ           =  13,
    XBMSP_PACKET_FILE_INFO               =  14,
    XBMSP_PACKET_FILE_OPEN               =  15,
    XBMSP_PACKET_FILE_READ               =  16,
    XBMSP_PACKET_FILE_SEEK               =  17,
    XBMSP_PACKET_CLOSE                   =  18,
    XBMSP_PACKET_CLOSE_ALL               =  19,
    XBMSP_PACKET_SET_CONFIGURATION_OPTION=  20,
    XBMSP_PACKET_AUTHENTICATION_INIT     =  21,
    XBMSP_PACKET_AUTHENTICATE            =  22,
    XBMSP_PACKET_UPCWD                   =  23,
    XBMSP_PACKET_INIT                    =  80
  }
  public enum XBMSPServer
  {
    XBMSP_PACKET_OK                      =  1,
    XBMSP_PACKET_ERROR                   =  2,
    XBMSP_PACKET_HANDLE                  =  3,
    XBMSP_PACKET_FILE_DATA               =  4,
    XBMSP_PACKET_FILE_CONTENTS           =  5,
    XBMSP_PACKET_AUTHENTICATION_CONTINUE =  6
  }
  public enum XBMSPError
  {
    XBMSP_ERROR_OK                       =  0, // Reserved
    XBMSP_ERROR_FAILURE                  =  1,
    XBMSP_ERROR_UNSUPPORTED              =  2,
    XBMSP_ERROR_NO_SUCH_FILE             =  3,
    XBMSP_ERROR_INVALID_FILE             =  4,
    XBMSP_ERROR_INVALID_HANDLE           =  5,
    XBMSP_ERROR_OPEN_FAILED              =  6,
    XBMSP_ERROR_TOO_MANY_OPEN_FILES      =  7,
    XBMSP_ERROR_TOO_LONG_READ            =  8,
    XBMSP_ERROR_ILLEGAL_SEEK             =  9,
    XBMSP_ERROR_OPTION_IS_READ_ONLY      = 10,
    XBMSP_ERROR_INVALID_OPTION_VALUE     = 11,
    XBMSP_ERROR_AUTHENTICATION_NEEDED    = 12,
    XBMSP_ERROR_AUTHENTICATION_FAILED    = 13
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.