Skip to content

Commit

Permalink
Initial public version
Browse files Browse the repository at this point in the history
  • Loading branch information
andrivet committed Aug 30, 2011
0 parents commit 5abae8f
Show file tree
Hide file tree
Showing 12 changed files with 2,692 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.lib
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
58 changes: 58 additions & 0 deletions ADVsock2pipe.Core/ADVsock2pipe.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{EE38BED3-0746-44C1-8CE3-37D219AB4738}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Advtools.Advsock2pipe</RootNamespace>
<AssemblyName>ADVsock2pipe.Core</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\ADVsock2pipe.Core.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Options.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Server.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
110 changes: 110 additions & 0 deletions ADVsock2pipe.Core/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* This file is part of ADVsock2pipe
* Copyright (c) 2011 - ADVTOOLS SARL
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NDesk.Options;

namespace Advtools.Advsock2pipe
{
/// <summary>
/// Configuration of this application
/// </summary>
public class Config
{
/// <summary>TCP port number</summary>
public int Port { get; private set; }
/// <summary>Name of the pipe</summary>
public string Pipe { get; private set; }
/// <summary>Level of the messages to log</summary>
public Level LogLevel { get; private set; }

/// <summary>
/// Constructor
/// </summary>
public Config()
{
// Default log level is informative messages
LogLevel = Level.Info;
}

/// <summary>
/// Parse the command-line arguments
/// </summary>
/// <param name="args">Command-line arguments</param>
/// <returns>Return true if the application can continue, false if it has to stop there</returns>
public bool Parse(string[] args)
{
// Help requested (by default, no)?
bool help = false;

// Definition of the command-line arguments and how to set the related configuration data
var options = new OptionSet()
{
{ "pipe=", "Name of the pipe", o => Pipe = o },
{ "port=", "TCP port number", (int o) => Port = o },
{ "log=", "Level of messages to log", (Level o) => LogLevel = o },
{ "h|help", "Show this message", o => help = o != null }
};

try
{
// Parse the command-line arguments (thanks NDesk!)
List<string> extra = options.Parse(args);
// If there are some arguments not parsed, no name of pipe or an invalid port number, show the help
if(extra.Count > 0 || Pipe == null || Port <= 0 || Port > 65535)
{
ShowHelp(options);
return false; // Do not continue the application
}
}
catch(OptionException e)
{
// Something wrong with the arguments
Console.WriteLine(e.Message);
Console.WriteLine("Try 'advsock2pipe --help' for more information.");
return false;
}

// Show the help?
if(help)
{
ShowHelp(options);
return false;
}

// Continue the application
return true;
}

/// <summary>
/// Display some help about this application
/// </summary>
/// <param name="options"></param>
private void ShowHelp(OptionSet options)
{
Console.WriteLine("Usage: advsock2pipe [OPTIONS]");
Console.WriteLine("Redirect data from a TCP port to a Windows named pipe.");
Console.WriteLine();
Console.WriteLine("Options:");
options.WriteOptionDescriptions(Console.Out);
}
}
}
76 changes: 76 additions & 0 deletions ADVsock2pipe.Core/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This file is part of ADVsock2pipe
* Copyright (c) 2011 - ADVTOOLS SARL
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Advtools.Advsock2pipe
{
#region Enumerations
/// <summary>
/// Level of the messages to log
/// </summary>

public enum Level
{
/// <summary>Message for debugging</summary>
Debug,
/// <summary>Informative messages</summary>
Info,
/// <summary>Warnings</summary>
Warning,
/// <summary>Error messages</summary>
Error,
/// <summary>Critical error</summary>
Critical
}
#endregion

internal class Logger
{
#region Private fields
private readonly Level level_;
#endregion

public Logger(Level level)
{
level_ = level;
}

/// <summary>
/// Log a message
/// </summary>
/// <param name="level">Level of the log</param>
/// <param name="message">Message to log</param>
public void Log(Level level, string message)
{
if(level < level_)
return;
Console.WriteLine("[" + level + "] " + message);
}

public void Log(Level level, string format, params object[] arg)
{
if(level < level_)
return;
Console.WriteLine("[" + level + "] " + format, arg);
}
}
}
Loading

0 comments on commit 5abae8f

Please sign in to comment.