first commit

This commit is contained in:
Digi
2025-02-23 20:09:12 +01:00
commit f3da4b9c3e
175 changed files with 2729 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace YapperClient.Net.IO
{
class PacketBuilder
{
MemoryStream _ms;
public PacketBuilder()
{
_ms = new MemoryStream();
}
public void WriteOpCode(byte opcode)
{
_ms.WriteByte(opcode);
}
public void WriteString(string msg)
{
var msgLength = msg.Length;
_ms.Write(BitConverter.GetBytes(msgLength));
_ms.Write(Encoding.ASCII.GetBytes(msg));
}
public byte[] GetPacketBytes()
{
return _ms.ToArray();
}
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace YapperClient.Net.IO
{
class PacketReader : BinaryReader
{
private NetworkStream _ns;
public PacketReader(NetworkStream ns) : base(ns)
{
_ns = ns;
}
public string ReadMessage()
{
byte[] msgBuffer;
var length = ReadInt32();
msgBuffer = new byte[length];
_ns.Read(msgBuffer, 0, length);
var msg = Encoding.ASCII.GetString(msgBuffer);
return msg;
}
}
}

59
Yapper/Net/Server.cs Normal file
View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using YapperClient.Net.IO;
namespace YapperClient.Net
{
class Server
{
TcpClient _client;
public PacketReader PacketReader;
public event Action connectedEvent;
public Server() {
_client = new TcpClient();
}
public void ConnectToServer(string userName)
{
if (!_client.Connected)
{
_client.Connect("127.0.0.1", 7891);
PacketReader = new PacketReader(_client.GetStream());
if (!string.IsNullOrEmpty(userName))
{
var connectPacket = new PacketBuilder();
connectPacket.WriteOpCode(0);
connectPacket.WriteString(userName);
_client.Client.Send(connectPacket.GetPacketBytes());
}
ReadPackets();
}
}
private void ReadPackets()
{
Task.Run(() => {
while (true)
{
var opcode = PacketReader.ReadByte();
switch (opcode)
{
case 1:
connectedEvent?.Invoke();
break;
default:
Console.WriteLine("ah yes ...");
break;
}
}
});
}
}
}