Skip to content

Commit

Permalink
Lot of changes over the year
Browse files Browse the repository at this point in the history
  • Loading branch information
NoobNotFound committed Jan 27, 2024
1 parent 5d79db2 commit e13561d
Show file tree
Hide file tree
Showing 91 changed files with 1,037 additions and 246 deletions.
3 changes: 3 additions & 0 deletions Noobsenger.Core/ByteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ChatData : Interfaces.IData
public Avatars Avatar { get; set; }
public Uri[] Uploads { get; set; }
public DataType DataType { get; set; }
public string GUID { get; set; }
public byte[] Files { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }

public ChatData(string clientName = "", string message = "", Avatars avatar = Avatars.Gamer, Uri[] uploads = null, DataType dataType = DataType.Chat, string infoCode = null, int count = 0)
{
ClientName = clientName;
Expand Down
3 changes: 2 additions & 1 deletion Noobsenger.Core/Interfaces/IClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ public interface IClient
event EventHandler<IData> ChatRecieved;
event EventHandler NameChanged;
string UserName { get; set; }
public Guid GUID { get; set; }
TcpClient clientSocket { get; set; }
Avatars Avatar { get; set; }
void Connect(IPAddress ip, int port, string userName, Avatars avatar);
void Connect(IPAddress ip, int port, string userName, Avatars avatar,Guid guid);
Task SendMessage(IData data);
}
}
2 changes: 2 additions & 0 deletions Noobsenger.Core/Interfaces/IData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public interface IData
string ClientName { get; set; }
string Message { get; set; }
string InfoCode { get; set; }
string GUID { get; set; }
Avatars Avatar { get; set; }
Uri[] Uploads { get; set; }
DataType DataType { get; set; }
byte[] Files { get; set; }
}
}
20 changes: 15 additions & 5 deletions Noobsenger.Core/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class InfoCodes
public static string ChannelClosed { get; set; } = "IC8";
public static string ImgFromWeb { get; set; } = "IC9";
public static string AddGPTChannel { get; set; } = "IC10";
public static string Thinking { get; set; } = "IC11";
public static string NotThinking { get; set; } = "IC12";
public static string MembersUpdated { get; set; } = "IC13";
public static string MessageDelete { get; set; } = "IC14";
public static string ChannelsRequest { get; set; } = "IC15";
}

public enum Avatars
Expand Down Expand Up @@ -72,6 +77,7 @@ public static IPAddress ParseIPAddress(string ipString)
}
}
}
[Obsolete]
public static class Server
{
public static IPAddress IP;
Expand All @@ -88,7 +94,7 @@ public static string ServerName
}
}
public static Hashtable ClientsList = new();
public static bool IsRuns = true;
public static bool IsRunning = true;
public static bool IsHosted = false;
public static TcpListener ServerSocket;
public static void Host(IPAddress address, int port, string serverName)
Expand All @@ -106,7 +112,7 @@ public static void Host(IPAddress address, int port, string serverName)
private static void Reciver()
{
TcpClient clientSocket = default;
while (IsRuns)
while (IsRunning)
{
clientSocket = ServerSocket.AcceptTcpClient();

Expand Down Expand Up @@ -161,7 +167,7 @@ public class ClientHandler
{
public TcpClient ClientSocket;
private string clNo;
public bool IsRuns = true;
public bool IsRunning = true;

public void StartClient(TcpClient inClientSocket, string clineNo)
{
Expand All @@ -175,7 +181,7 @@ public void doChat()
{
byte[] bytesFrom = new byte[10025];

while (IsRuns)
while (IsRunning)
{
try
{
Expand All @@ -202,6 +208,7 @@ public void doChat()
//end doChat
}
}
[Obsolete]
public class Client : IClient
{
public event EventHandler<IData> ChatRecieved = delegate { };
Expand All @@ -211,9 +218,12 @@ public class Client : IClient
public string UserName { get; set; }
public Avatars Avatar { get; set; }
public string ServerName { get; set; }
public async void Connect(IPAddress ip, int port, string userName, Avatars avatar)
public Guid GUID { get; set; }

public async void Connect(IPAddress ip, int port, string userName, Avatars avatar,Guid guid)
{
await clientSocket.ConnectAsync(ip, port);
GUID = guid;
serverStream = clientSocket.GetStream();
this.UserName = userName;
this.Avatar = avatar;
Expand Down
9 changes: 5 additions & 4 deletions Noobsenger.Core/Noobsenger.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Betalgo.OpenAI.GPT3" Version="6.6.6" />
<PackageReference Include="Betalgo.OpenAI" Version="7.4.1" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OpenAI" Version="1.6.0" />
<PackageReference Include="OpenAI" Version="1.7.2" />
</ItemGroup>

</Project>
45 changes: 27 additions & 18 deletions Noobsenger.Core/Ultra/Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
using System.Threading;
using System.Threading.Tasks;
using Noobsenger.Core.Ultra.DataManager;
using OpenAI_API;
using OpenAI_API.Completions;
using OpenAI_API.Models;

namespace Noobsenger.Core.Ultra
{
Expand All @@ -35,7 +32,7 @@ public string ChannelName
}
}
public Hashtable TcpClientsList { get; set; } = new Hashtable();
public bool IsRuns = false;
public bool IsRunning = false;
public bool IsHosted = false;
public TcpListener ServerSocket;
public Channel(int Count)
Expand All @@ -52,7 +49,7 @@ public bool Host(IPAddress address, int port, string serverName)
ServerSocket = new TcpListener(address, port);
IP = address;
Port = port;
IsRuns = true;
IsRunning = true;
ServerSocket.Start();
var t = new Thread(Reciver);
t.Start();
Expand All @@ -66,7 +63,7 @@ public bool Host(IPAddress address, int port, string serverName)
private void Reciver()
{
TcpClient clientSocket = default;
while (IsRuns)
while (IsRunning)
{
clientSocket = ServerSocket.AcceptTcpClient();

Expand All @@ -79,10 +76,11 @@ private void Reciver()

if (dataFromClient.DataType == DataType.InfoMessage)
{

if (dataFromClient.InfoCode == InfoCodes.Join)
{
ClientsCount++;
var client = new ClientHandler(clientSocket, dataFromClient.ClientName, ClientsCount);
var client = new ClientHandler(clientSocket, dataFromClient.ClientName, ClientsCount,guid:dataFromClient.GUID);
client.Disconnected += (sender, e) =>
{
try
Expand Down Expand Up @@ -112,7 +110,8 @@ private void Reciver()
TcpClientsList.Add(ClientsCount, clientSocket);
ClientHandlersList.Add(client);
client.Start();
BroadcastAll(new Data(dataFromClient.ClientName, dataFromClient.ClientName + " Joined.", dataFromClient.Avatar, new List<Uri>().ToArray(), DataType.InfoMessage, InfoCodes.Join));
BroadcastAll(new Data(dataFromClient.ClientName, dataFromClient.ClientName + " Joined.", dataFromClient.Avatar, null, DataType.InfoMessage, InfoCodes.Join));
Task.Delay(200).Wait();
BroadcastAll(new Data(ChannelName, ChannelName, dataType: DataType.InfoMessage, infoCode: InfoCodes.ServerNameReceived));
}
else if (dataFromClient.InfoCode == InfoCodes.ServerClosed)
Expand All @@ -131,7 +130,7 @@ private void Reciver()

}
}
catch { }
catch { }
}
}
}
Expand All @@ -142,15 +141,22 @@ public void BroadcastAll(Data data)
{
foreach (DictionaryEntry Item in TcpClientsList)
{
TcpClient broadcastSocket;
broadcastSocket = (TcpClient)Item.Value;
NetworkStream broadcastStream = broadcastSocket.GetStream();
byte[] broadcastBytes;
try
{
TcpClient broadcastSocket;
broadcastSocket = (TcpClient)Item.Value;
if (broadcastSocket.Connected)
{
NetworkStream broadcastStream = broadcastSocket.GetStream();
byte[] broadcastBytes;

broadcastBytes = data.ToBytes();
broadcastBytes = data.ToBytes();

broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
broadcastStream.Flush();
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
broadcastStream.Flush();
}
}
catch { }
}
}
public void BroadcastAll(byte[] data,int length)
Expand All @@ -173,10 +179,13 @@ internal class ClientHandler
public event EventHandler<BytesRecievedEventArgs> BytesRecieved = delegate { };
public event EventHandler Disconnected = delegate { };
public int ClientNumber;
public bool IsRuns = true;
public bool IsRunning = true;
public string ClientName;
public ClientHandler(TcpClient clientSocket, string clientName, int clientNumber, bool start = false)
public Guid ClientId;
public ClientHandler(TcpClient clientSocket, string clientName, int clientNumber, bool start = false,string guid = null)
{
if (!Guid.TryParse(guid, out ClientId))
ClientId = Guid.NewGuid();
ClientSocket = clientSocket;
ClientName = clientName;
ClientNumber = clientNumber;
Expand Down
13 changes: 9 additions & 4 deletions Noobsenger.Core/Ultra/DataManager/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ public class Data : Interfaces.IData
{
public int Count { get; set; }
public string ClientName { get; set; }
public string GUID { get; set; }
public string Message { get; set; }
public string InfoCode { get; set; }
public Avatars Avatar { get; set; }
public Uri[] Uploads { get; set; }
public DataType DataType { get; set; }
public object[] Objects { get; set; }
public Data(string clientName = "", string message = "", Avatars avatar = Avatars.Gamer, Uri[] uploads = null, DataType dataType = DataType.Chat, string infoCode = null,object[] objects = null, int count = 0)
public byte[] Files { get; set; }
public Data(string clientName = "", string message = "", Avatars avatar = Avatars.Gamer, Uri[] uploads = null, DataType dataType = DataType.Chat, string infoCode = null,byte[] Files = null, int count = 0, string gUID = null)
{
ClientName = clientName;
Message = message;
Avatar = avatar;
Uploads = uploads;
DataType = dataType;
InfoCode = infoCode;
Objects = objects;
this.Files = Files;
Count = count;
GUID = gUID;
}
}
internal class DataString
Expand All @@ -34,6 +36,9 @@ internal class DataString
public string Avatar { get; set; }
public string[] Uploads { get; set; }
public string DataType { get; set; }
public string GUID { get; set; }
public string Count { get; set; }
public string Files { get; set; }
}

}
}
24 changes: 20 additions & 4 deletions Noobsenger.Core/Ultra/DataManager/Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public static byte[] ToBytes(this Data data)
Message = data.Message.ToString(),
ClientName = data.ClientName,
DataType = data.DataType.ToString(),
InfoCode = data.InfoCode
InfoCode = data.InfoCode,
GUID = data.GUID,
Count = data.Count.ToString(),
Files = data.Files != null ? string.Join(',', data.Files.Select(x=> x.ToString()).ToArray()) : ""
};
if (data.Uploads != null)
{
Expand All @@ -28,16 +31,29 @@ public static byte[] ToBytes(this Data data)
}
return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(byteData));
}


// Convert a byte array to an Object
public static Data ToData(this byte[] arrBytes)
{
string str = Encoding.UTF8.GetString(arrBytes);
DataString r = JsonConvert.DeserializeObject<DataString>(str);
Data data = new(r.ClientName, r.Message, (Avatars)Enum.Parse(typeof(Avatars), r.Avatar), dataType: (DataType)Enum.Parse(typeof(DataType), r.DataType), infoCode: r.InfoCode);
byte[] files = null;
try
{
files = r.Files.Split(',').Select(x => byte.Parse(x)).ToArray();
}
catch { }
Data data = new(r.ClientName,
r.Message,
(Avatars)Enum.Parse(typeof(Avatars),
r.Avatar),
dataType: (DataType)Enum.Parse(typeof(DataType), r.DataType),
infoCode: r.InfoCode,
gUID: r.GUID,
count: (int.TryParse(r.Count, out int i) ? i : 0),
Files: files);
if (data.Uploads != null)
{

List<Uri> uploads = new();
foreach (var item in r.Uploads)
{
Expand Down
Loading

0 comments on commit e13561d

Please sign in to comment.