Skip to content

Commit

Permalink
Task starter editing and data parser refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammedikinci committed Sep 15, 2017
1 parent bf4c322 commit 88fbb6a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 7 deletions.
5 changes: 4 additions & 1 deletion FuzzyCore/CommandClasses/GetFolderList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void GetFoldersName(Data.JsonCommand Command)
catch (Exception ex)
{
message.Write(ex.Message.ToString(), ConsoleMessage.MessageType.ERROR);
Console.WriteLine("GetFolderList->GetFoldersName");
}
}

Expand All @@ -56,13 +57,14 @@ public void SendDataString(String Data, Socket Client)
{
try
{
Thread.Sleep(10);
//Thread.Sleep(10);
byte[] arr = Encoding.UTF8.GetBytes(Data);
Client.Send(arr);
}
catch (Exception ex)
{
message.Write(ex.Message,ConsoleMessage.MessageType.ERROR);
Console.WriteLine("GetFolderList->SendDataString");
}
}
public void SendDataArray(byte[] Data, Socket Client)
Expand All @@ -75,6 +77,7 @@ public void SendDataArray(byte[] Data, Socket Client)
catch (Exception ex)
{
message.Write(ex.Message, ConsoleMessage.MessageType.ERROR);
Console.WriteLine("GetFolderList->SendDataArray");
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions FuzzyCore/Data/DataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,25 @@ namespace FuzzyCore.Data
public class DataParser
{
public static string LastCommand = "None";
JsonCommand jsonComm;
public DataParser(String Data, Socket Client)
{
try
{
JsonCommand jsonComm = JsonConvert.DeserializeObject<JsonCommand>(Data);
jsonComm.Client_Socket = Client;
LastCommand = jsonComm.CommandType.ToString();
DataSerializer ds = new DataSerializer();
string a = ds.Serialize(Data);
if (a == "WAIT_NEXT_DATA")
{
jsonComm.CommandType = "WAIT_NEXT_DATA";
jsonComm.Client_Socket = Client;
LastCommand = jsonComm.CommandType.ToString();
}
else
{
jsonComm = JsonConvert.DeserializeObject<JsonCommand>(a);
jsonComm.Client_Socket = Client;
LastCommand = jsonComm.CommandType.ToString();
}
switch (jsonComm.CommandType)
{
case "print_message":
Expand Down Expand Up @@ -90,7 +102,7 @@ public DataParser(String Data, Socket Client)
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.Message + ex.Source.ToString());
}
}
}
Expand Down
46 changes: 46 additions & 0 deletions FuzzyCore/Data/DataSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FuzzyCore.Data
{
class DataSerializer
{
public static String LastData = null;
public static bool CreateCommand = false;
public String Serialize(String Data)
{
if (CreateCommand)
{
LastData = null;
CreateCommand = false;
}
if (Data.IndexOf("{") != -1)
{
if (Data.IndexOf("}") != -1)
{
return Data;
}
else
{
LastData = Data;
return "WAIT_NEXT_DATA";
}
}
else
{
if (!string.IsNullOrEmpty(LastData))
{
CreateCommand = true;
return LastData + Data;
}
else
{
return "";
}
}
}
}
}
1 change: 1 addition & 0 deletions FuzzyCore/FuzzyCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Database\mysql.cs" />
<Compile Include="Database\User.cs" />
<Compile Include="Data\DataParser.cs" />
<Compile Include="Data\DataSerializer.cs" />
<Compile Include="Data\JsonCommand.cs" />
<Compile Include="Initialize\Init.cs" />
<Compile Include="Listener.cs" />
Expand Down
10 changes: 8 additions & 2 deletions FuzzyCore/Listener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ void AcceptSocket(IAsyncResult State)
Thread AcceptTaskTh = new Thread(new ThreadStart(() => {
AcceptTask(CurrentClient);
}));
AcceptTaskTh.Start();
if (AcceptTask != null)
{
AcceptTaskTh.Start();
}

//Add created client to socket list
SocketList.Add(CurrentClientId, CurrentClient);
Expand Down Expand Up @@ -157,7 +160,10 @@ public void ReceiveData(IAsyncResult State)
{
DataParser parser = new DataParser(Data, CurrentSocket);
Thread reciveTask = new Thread(new ThreadStart(() => { ReceiverTask(Data, GetClientBySocket(CurrentSocket)); }));
reciveTask.Start();
if (ReceiverTask != null)
{
reciveTask.Start();
}
CurrentSocket.BeginReceive(_buff, 0, _buff.Length, SocketFlags.None, new AsyncCallback(ReceiveData), CurrentSocket);
}
else
Expand Down
Binary file modified FuzzyCore/bin/Debug/FuzzyCore.dll
Binary file not shown.
Binary file modified FuzzyCore/bin/Debug/FuzzyCore.pdb
Binary file not shown.
Binary file not shown.
Binary file modified FuzzyCore/obj/Debug/FuzzyCore.dll
Binary file not shown.
Binary file modified FuzzyCore/obj/Debug/FuzzyCore.pdb
Binary file not shown.

0 comments on commit 88fbb6a

Please sign in to comment.