Skip to content

Commit

Permalink
[v.0.02a] Major Minor Update
Browse files Browse the repository at this point in the history
NetworkServer:
-----------------------------------------
Bugfixes: Changed the name of the class from NewtworkServer to
NetworkServer
Changes: Added Basic Functionality. ( Now App can be killed with
TooManyConnections :) )
ToDo: Stop() functionality; MaxProcessingThreads value and its
functionality; Actual functionality at least for ping and changeip
messages.
-----------------------------------------
MainWindow:
-----------------------------------------
Changes: Added tabs and changed style to orangish. Also added a method
to show yes-no dialog - ShowBinaryDialog()
ToDo: Add actual *basic* functionality to FileShare tab and Users tab
-----------------------------------------
*NEW* NetPacket:
-----------------------------------------
Basic skeleton of the class. This class will handle package version
detection, package data decryption, encryption, checksum checks and etc.
-----------------------------------------
*NEW* UserInfo:
-----------------------------------------
This class has some functionality of storing Users' data and state. It
is also *probably*  able to serialize itself into a string/file.
However, this is have been not tested yet.
-----------------------------------------
*NEW* SerializableList
-----------------------------------------
This class *again, not tested* is able to hold the List of some objects
and serialize it at your wish. Maybe it's reinventing the wheel, but who
cares?
ToDo: Change its behavior, so it will hold not the List<T>, but
ICollection<T>
-----------------------------------------
And added/changed some little things here and there.
  • Loading branch information
FakeEmperor committed Mar 12, 2015
1 parent 817806f commit e3818de
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 36 deletions.
6 changes: 6 additions & 0 deletions AutoShare/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
<setting name="FolderPath" serializeAs="String">
<value>AutoShare</value>
</setting>
<setting name="UserlistPrefix" serializeAs="String">
<value>users</value>
</setting>
<setting name="UserlistExtension" serializeAs="String">
<value>userlist</value>
</setting>
</AutoShare.Properties.Settings>
<AutoShare.Settings1>
<setting name="FolderPath" serializeAs="String">
Expand Down
25 changes: 22 additions & 3 deletions AutoShare/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,37 @@ namespace AutoShare
public partial class App : Application
{
public AutoShare.Engine.IO.FilesFolderChecker FolderWatchdog;
public AutoShare.Engine.IO.SerializableList<AutoShare.Engine.Network.Sharing.UserInfo> KnownUsers;

//TEMP
public bool AskUserExit = true;
App()
{
#region Initialization Block

string path;
if (AutoShare.Properties.Settings.Default.UseDocFolder)
path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + System.IO.Path.DirectorySeparatorChar + AutoShare.Properties.Settings.Default.FolderPath;
else
path = AutoShare.Properties.Settings.Default.FolderPath;
FolderWatchdog = new Engine.IO.FilesFolderChecker(path, true);
#endregion
FolderWatchdog = new Engine.IO.FilesFolderChecker(path, true);

}

KnownUsers = new Engine.IO.SerializableList<Engine.Network.Sharing.UserInfo>();

#endregion
#region Subscription to events


#endregion
}
public void Shutdown()
{
//save all settings
AutoShare.Properties.Settings.Default.Save();
//save folder state

//save user state
}
}
}
3 changes: 3 additions & 0 deletions AutoShare/AutoShare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,12 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Engine\IO\FileSystem.cs" />
<Compile Include="Engine\IO\SerializableList.cs" />
<Compile Include="Engine\Network\NetworkServer.cs" />
<Compile Include="Engine\Network\AutoClient.cs" />
<Compile Include="Engine\Network\NetPacket.cs" />
<Compile Include="Engine\Network\Sharing\SharedFile.cs" />
<Compile Include="Engine\Network\Sharing\UserInfo.cs" />
<Compile Include="Engine\XAMLConverters\ImageToSourceConverter.cs" />
<Compile Include="Engine\XAMLConverters\MultiIntToBoolConverter.cs" />
<Compile Include="Engine\XAMLConverters\PercentageConverter.cs" />
Expand Down
36 changes: 36 additions & 0 deletions AutoShare/Engine/IO/SerializableList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;

namespace AutoShare.Engine.IO
{

[Serializable()]
public class SerializableList<T>:ISerializable
{
List<T> lst;
public List<T> List { get { return lst; } set { lst = value; } }
public SerializableList()
{
lst = new List<T>();
}
public SerializableList(List<T> List)
{
lst = List;
}

public SerializableList( SerializationInfo info, StreamingContext context)
{
lst = (List<T>)info.GetValue("list" , typeof(List<T>));
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{

info.AddValue("list", lst, typeof(List<T>));
}
}
}
116 changes: 116 additions & 0 deletions AutoShare/Engine/Network/NetPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace AutoShare.Engine.Network
{
public class NetPacket
{
#region Helper Classes
public enum PacketType
{
PT_PUBLIC_WITH_HASH, //Packet's integrity is secured with MD5 checksum
PT_PUBLIC_WITHOUT_HASH, //Packet's integrity is not guaranteed
//PT_PUBLIC_STREAMING, //Packet's integrity is not checked as it is a public packet. Special structure for continous packets
PT_PRIVATE, //Packet Integrity is
//PT_PRIVATE_STREAMING //Packet's integrity is guaranteed through streaming encryption with ECB mode. Special structure for streaming packets.
}
public abstract class PacketSignatureRule
{
#region Helper Classes
public enum PacketSignatureAlignement {
PS_ALIGN_BEGIN,
PS_ALIGN_END
}
#endregion
#region Public Static Members
public static int MaxSignatureSize = 80;
#endregion
#region Public Members
public readonly int SignatureSize;
public readonly int SignatureVersion;
public readonly PacketSignatureAlignement Alignement;
public readonly bool DataEncrypted;
public readonly bool MD5HashPresent;
public readonly string SignatureName;
public readonly string SignatureDetectionHelper;
#endregion
#region Constructors
public PacketSignatureRule(
int SignatureSize, int SignatureVersion, PacketSignatureAlignement Alignement,
bool DataEncrypted, bool MD5HashPresent,
string SignatureName, string SignatureDetectionHelper
)
{
this.SignatureSize = SignatureSize;
if (MaxSignatureSize < this.SignatureSize)
throw new TypeInitializationException("PacketSignatureRule",
new Exception("SignatureSize value is more than MaxSignatureSize. Either change the PacketSignatureRule.MaxSignatureSize or lower the passed SignatureSize value."+System.Environment.NewLine+
"Ignoring this message may change the behaviour of the application when accepting packets over network using MaxSignatureSize as the buffer size."));
this.SignatureName = SignatureName;
this.SignatureVersion = SignatureVersion;
this.Alignement = Alignement;
this.DataEncrypted = DataEncrypted;
this.MD5HashPresent = MD5HashPresent;
this.SignatureDetectionHelper = SignatureDetectionHelper;

}
#endregion

#region API Calls
public abstract PacketType GetPacketType();
#endregion
}

public class PublicNodePacket:PacketSignatureRule
{
#region Static Members
private static readonly int SizeFieldSize = 8; //Size of the field in the signature, that tells us the size of the actual packet
private static readonly int PacketVersion = 1;
private static readonly string SignatureHelper = "{PUBLIC}";

public static readonly PacketType Type = PacketType.PT_PUBLIC_WITH_HASH;
#endregion
public PublicNodePacket():
base(16+SizeFieldSize+SignatureHelper.Length, PacketVersion, PacketSignatureAlignement.PS_ALIGN_BEGIN, false, true, "PublicNodePacket", SignatureHelper)
{

}

public override PacketType GetPacketType()
{
return Type;
}
}
public class PublicUnsafePacket : PacketSignatureRule
{
#region Static Members
private static readonly int SizeFieldSize = 8; //Size of the field in the signature, that tells us the size of the actual packet
private static readonly int PacketVersion = 1;
private static readonly string SignatureHelper = "{UNSAFE}";

public static readonly PacketType Type = PacketType.PT_PUBLIC_WITHOUT_HASH;
#endregion
public PublicUnsafePacket()
: base(SizeFieldSize + SignatureHelper.Length, PacketVersion, PacketSignatureAlignement.PS_ALIGN_BEGIN, false, false, "PublicUnsafePacket", SignatureHelper)
{

}

public override PacketType GetPacketType()
{
return Type;
}
}
#endregion
#region Static Members
public static readonly PacketSignatureRule[] SignatureRules = new PacketSignatureRule[2]{ new PublicNodePacket(), new PublicUnsafePacket() };
#endregion
#region Static Methods
#endregion

}
}
99 changes: 76 additions & 23 deletions AutoShare/Engine/Network/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,100 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using AutoShare.Engine.Network;

namespace AutoShare.Engine.Network
{
class NewtworkServer
public class NetworkServer
{
#region
TcpListener Listener; // Îáúåêò, ïðèíèìàþùèé TCP-êëèåíòîâ
ManualResetEvent HEvent;
List<Thread> ProcessingThreads;
Thread ServerThread;

ManualResetEvent Hevent;

TcpListener WaitForClient()
#endregion
#region Threading Functions
void ProcessClientThreadingFunction(object client)
{
while (true)
TcpClient cli = client as TcpClient;
try
{
if (cli != null)
{
if (HEvent.WaitOne(0)) //check if the class is being destructed //Probably unnecessary
cli.Close();
else
{
NetworkStream ns = cli.GetStream();
byte[] bytes_to_send, buffer = new byte[NetPacket.PacketSignatureRule.MaxSignatureSize];
//Accept the signature and some data (probably)
ns.Read(buffer, 0, buffer.Length);

//TEMP
bytes_to_send = System.Text.UTF8Encoding.UTF8.GetBytes("PONG");
//Final Function
ns.Write(bytes_to_send, 0, bytes_to_send.Length);
}
}
}
catch (ThreadAbortException e)
{
Listener.AcceptTcpClient();
//Close connection as the thread will be removed soon
if (cli != null)
cli.Close();
}

}
// Çàïóñê ñåðâåðà
public NewtworkServer(int Port)
void ServerThreadFunction()
{
Listener.Start();
Listener.BeginAcceptTcpClient( (IAsyncResult res)=>{
TcpClient cli = Listener.EndAcceptTcpClient(res);
if(cli!=null){
Thread thr = new Thread(ProcessClientThreadingFunction);
ProcessingThreads.Add(thr);
thr.Start( cli );
}
}, null);
this.HEvent.WaitOne();
Listener.Stop();

}
#endregion

#region Constructors and Destructors
//Çàïóñê Ñåðâåðà
public NetworkServer(int Port, bool StartImmediately = true)
{
// Ñîçäàåì "ñëóøàòåëÿ" äëÿ óêàçàííîãî ïîðòà

Listener = new TcpListener(IPAddress.Any, Port);
Listener.Start(); // Çàïóñêàåì åãî
//  áåñêîíå÷íîì öèêëå
while (true)
{
// Ïðèíèìàåì íîâûõ êëèåíòîâ
Listener.AcceptTcpClient();
}
this.Listener = new TcpListener(IPAddress.Any, Port);
this.HEvent = new ManualResetEvent(false);
this.ServerThread = new Thread( ServerThreadFunction );
this.ProcessingThreads = new List<Thread>();
if (StartImmediately)
this.ServerThread.Start();

}

// Îñòàíîâêà ñåðâåðà
~NewtworkServer()
~NetworkServer()
{
// Åñëè "ñëóøàòåëü" áûë ñîçäàí
if (Listener != null)
{
// Îñòàíîâèì åãî
Listener.Stop();
}
this.HEvent.Set();
for (int i = 0, s = ProcessingThreads.Count; i < s; ++i )
ProcessingThreads[i].Abort();
}
#endregion
#region API Calls

public void Start()
{
if(ServerThread.ThreadState != ThreadState.Running){
ServerThread.Start();
}
}
#endregion
}
}
Loading

0 comments on commit e3818de

Please sign in to comment.