-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36b9c68
commit bcf67b2
Showing
27 changed files
with
2,268 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static System.Runtime.InteropServices.JavaScript.JSType; | ||
|
||
namespace LAN.Core | ||
{ | ||
public class UltraClient | ||
{ | ||
public event EventHandler<byte[]>? DataRecieved; | ||
public TcpClient clientSocket { get; set; } = new TcpClient(); | ||
private NetworkStream serverStream = default; | ||
public IPAddress IP { get; private set; } | ||
|
||
public async void Connect(IPAddress ip, int port) | ||
{ | ||
IP = ip; | ||
await clientSocket.ConnectAsync(ip, port); | ||
serverStream = clientSocket.GetStream(); | ||
var ctThread = new Thread(GetMessage); | ||
|
||
ctThread.Start(); | ||
|
||
} | ||
public void Disconnect() | ||
{ | ||
clientSocket.Close(); | ||
} | ||
private void GetMessage() | ||
{ | ||
while (clientSocket.Connected) | ||
{ | ||
try | ||
{ | ||
serverStream = clientSocket.GetStream(); | ||
|
||
byte[] inStream = new byte[clientSocket.ReceiveBufferSize]; | ||
serverStream.Read(inStream, 0, inStream.Length); | ||
|
||
DataRecieved?.Invoke(this, inStream); | ||
} | ||
catch { } | ||
} | ||
|
||
} | ||
public async Task SendMessage(byte[] outStream) | ||
{ | ||
await serverStream.WriteAsync(outStream, 0, outStream.Length); | ||
serverStream.Flush(); | ||
await serverStream.FlushAsync(); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace LAN.Core; | ||
internal class ClientHandler | ||
{ | ||
public TcpClient ClientSocket; | ||
public event EventHandler<BytesRecievedEventArgs> BytesRecieved = delegate { }; | ||
public event EventHandler Disconnected = delegate { }; | ||
public ClientHandler(TcpClient clientSocket, bool start = false) | ||
{ | ||
ClientSocket = clientSocket; | ||
if (start) | ||
{ | ||
Thread ctThread = new(GetData); | ||
ctThread.Start(); | ||
} | ||
} | ||
public void Start() | ||
{ | ||
Thread ctThread = new(GetData); | ||
ctThread.Start(); | ||
} | ||
private void GetData() | ||
{ | ||
byte[] bytesFrom = new byte[10025]; | ||
|
||
while (ClientSocket.Connected) | ||
{ | ||
try | ||
{ | ||
NetworkStream networkStream = ClientSocket.GetStream(); | ||
int l = networkStream.Read(bytesFrom, 0, bytesFrom.Length); | ||
|
||
this.BytesRecieved(this, new BytesRecievedEventArgs(bytesFrom, l)); | ||
|
||
} | ||
catch | ||
{ | ||
} | ||
} | ||
Disconnected(this, new EventArgs()); | ||
} | ||
internal class BytesRecievedEventArgs : EventArgs | ||
{ | ||
public byte[] Bytes { get; set; } | ||
public int Length { get; set; } | ||
public BytesRecievedEventArgs(byte[] bytes, int length) | ||
{ | ||
Bytes = bytes; | ||
Length = length; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System.Collections; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Net; | ||
using System.Net.Sockets; | ||
using System.Threading.Channels; | ||
using static System.Runtime.InteropServices.JavaScript.JSType; | ||
|
||
namespace LAN.Core; | ||
public class UltraServer | ||
{ | ||
public IPAddress IP { get; private set; } | ||
public int Port { get; private set; } | ||
|
||
public Hashtable ClientsList = new(); | ||
private bool IsRunning = true; | ||
|
||
public TcpListener ServerSocket; | ||
public void TryCloseServer() | ||
{ | ||
IsRunning = false; | ||
} | ||
public void Host(IPAddress address, int port) | ||
{ | ||
IP = address; | ||
Port = port; | ||
|
||
IsRunning = true; | ||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; | ||
|
||
ServerSocket = new TcpListener(address, port); | ||
ServerSocket.Start(); | ||
|
||
var t = new Thread(Reciver); | ||
t.Start(); | ||
|
||
} | ||
public void BroadcastAll(byte[] broadcastBytes) | ||
{ | ||
foreach (DictionaryEntry Item in ClientsList) | ||
{ | ||
TcpClient broadcastSocket; | ||
broadcastSocket = (TcpClient)Item.Value; | ||
|
||
if (broadcastSocket.Connected) | ||
{ | ||
NetworkStream broadcastStream = broadcastSocket.GetStream(); | ||
|
||
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length); | ||
broadcastStream.Flush(); | ||
} | ||
} | ||
} | ||
private int ClientsCount = 0; | ||
private string memb = ""; | ||
private void Reciver() | ||
{ | ||
//TcpClient clientSocket = default; | ||
//while (IsRunning) | ||
//{ | ||
// clientSocket = ServerSocket.AcceptTcpClient(); | ||
|
||
// byte[] bytesFrom = new byte[10025]; | ||
// Data dataFromClient; | ||
|
||
// NetworkStream networkStream = clientSocket.GetStream(); | ||
// networkStream.Read(bytesFrom, 0, bytesFrom.Length); | ||
// dataFromClient = bytesFrom.ToData(); | ||
|
||
// if (dataFromClient.DataType == DataType.InfoMessage) | ||
// { | ||
// if (dataFromClient.InfoCode == InfoCodes.Join) | ||
// { | ||
// ClientsCount++; | ||
// memb += dataFromClient.Message + ","; | ||
|
||
// var client = new ClientHandler(clientSocket, dataFromClient.ClientName, ClientsCount, false, dataFromClient.GUID); | ||
// client.Disconnected += (sender, e) => | ||
// { | ||
// try | ||
// { | ||
// foreach (DictionaryEntry Item in ClientsList) | ||
// { | ||
// if ((Guid)Item.Key == ((ClientHandler)sender).ClientId) | ||
// { | ||
// ClientsList.Remove(Item); | ||
// return; | ||
// } | ||
// } | ||
// } | ||
// catch { } | ||
// BroadcastAll(new Data(((ClientHandler)sender).ClientName, ((ClientHandler)sender).ClientName + " Left.", dataType: DataType.InfoMessage, infoCode: InfoCodes.Left)); | ||
// }; | ||
// client.BytesRecieved += (sender, e) => BroadcastAll(e.Bytes, e.Length); | ||
// ClientsList.Add(dataFromClient.GUID, clientSocket); | ||
// client.Start(); | ||
|
||
// var channelPorts = Channels.Select(x => x.Port.ToString()); | ||
// BroadcastAll(string.Join(",", channelPorts.ToArray()), ServerName, DataType.InfoMessage, MsgCode: InfoCodes.AddChannels); | ||
|
||
// } | ||
// else if (dataFromClient.InfoCode == InfoCodes.ChannelsRequest) | ||
// { | ||
// var channelPorts = Channels.Select(x => x.Port.ToString()); | ||
// BroadcastAll(string.Join(",", channelPorts.ToArray()), ServerName, DataType.InfoMessage, MsgCode: InfoCodes.AddChannels); | ||
// } | ||
// } | ||
//} | ||
//clientSocket.Close(); | ||
//ServerSocket.Stop(); | ||
} | ||
public void Broadcast(TcpClient broadcastSocket, byte[] broadcastBytes) | ||
{ | ||
if (broadcastSocket.Connected) | ||
{ | ||
NetworkStream broadcastStream = broadcastSocket.GetStream(); | ||
|
||
broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length); | ||
broadcastStream.Flush(); | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Platforms>AnyCPU;ARM32;ARM64;x64;x86</Platforms> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="SuperSimpleTcp" Version="3.0.14" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Solitaire.Games.Omi.Core | ||
{ | ||
public class Action(string code,string data,bool sendAll) | ||
{ | ||
public string Code { get; private set; } = code; | ||
public string Data { get; private set; } = data; | ||
public bool SendAll { get; set; } = sendAll; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Solitaire.Games.Omi.Core | ||
{ | ||
public static class ActionCodes | ||
{ | ||
public const string UpdatePlayers = "AC1"; | ||
public const string RequestJoinAsPlayer = "AC2"; | ||
public const string JoinPlayerFailed = "AC3"; | ||
public const string JoinPlayerSuccess = "AC4"; | ||
public const string ShuffleCards = "AC5"; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Solitaire.Games.Enums; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Numerics; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using static System.Runtime.InteropServices.JavaScript.JSType; | ||
|
||
namespace Solitaire.Games.Omi.Core | ||
{ | ||
public class Card | ||
{ | ||
public Types Type { get; private set; } | ||
public Values Value { get; private set; } | ||
public Types TrumpType { get; set; } | ||
public int Owner { get; set; } | ||
public Card() : this(Types.Undefined,Values.Undefined) { } | ||
public Card(Types type, Values value) : this(type, value, Types.Undefined) { } | ||
public Card(Types type, Values value,Types trump) | ||
{ | ||
Type = type; | ||
Value = value; | ||
TrumpType = trump; | ||
} | ||
|
||
public static Card[] AllDiamond => Enum.GetValues<Values>().Where(x=> x != Values.Joker && x != Values.Undefined).Select(x => new Card(Types.Diamond, x)).ToArray(); | ||
public static Card[] AllHeart => Enum.GetValues<Values>().Where(x => x != Values.Joker && x != Values.Undefined).Select(x => new Card(Types.Heart, x)).ToArray(); | ||
public static Card[] AllScope => Enum.GetValues<Values>().Where(x => x != Values.Joker && x != Values.Undefined).Select(x => new Card(Types.Spade, x)).ToArray(); | ||
public static Card[] AllCalibri => Enum.GetValues<Values>().Where(x => x != Values.Joker && x != Values.Undefined).Select(x => new Card(Types.Club, x)).ToArray(); | ||
public static Card[] AllCards | ||
{ | ||
get | ||
{ | ||
var l = new List<Card>(); | ||
|
||
l.AddRange(AllDiamond); | ||
l.AddRange(AllHeart); | ||
l.AddRange(AllScope); | ||
l.AddRange(AllCalibri); | ||
|
||
return l.ToArray(); | ||
} | ||
} | ||
|
||
public static bool operator >(Card c1, Card c2) | ||
{ | ||
if (c1.Type == c2.Type) | ||
return c1.Value > c2.Value; | ||
else | ||
{ | ||
if (c1.TrumpType == c2.TrumpType) | ||
{ | ||
if (c1.Type == c1.TrumpType) | ||
return true; | ||
else if (c2.Type == c2.TrumpType) | ||
return false; | ||
else | ||
return true; | ||
} | ||
else | ||
return true; | ||
} | ||
} | ||
public static bool operator <(Card c1, Card c2) | ||
=> !(c1 > c2); | ||
|
||
public override string ToString() | ||
{ | ||
return Type + " " + Value; | ||
} | ||
} | ||
} |
Oops, something went wrong.