06-13-2019, 02:45 AM
Решил перейти с фраемфорка на NET CORE 2.1 ПОСЛЕ ПЕРЕНОСА ПЕРЕСТАЛ ЧИТАТЬ ПАКЕТ ПЕРВЫЙ ПАКЕТ АВТОРИЗАЦИИ НЕ ВИДИТ НИ ЛОГИНА НИ ПАРОЛЯ КОТОРЫЙ ПРИХОДИТ ЧТО ТОЛЬКО НЕ ПРОБЫВАЛ МОЖЕТ КТО ПОДСКАЖЕТ В ЧЕМ ПРОБЛЕМА НА ФРАЕМВОРКЕ ВСЕ РАБОТАЛО
[SRC="csharp"]using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Commons.Network
{
public class ClientPacket
{
public IClientConnection Client;
MemoryStream stream;
BinaryReader reader;
private int offset;
private byte[] buffer;
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
public void Init(byte[] _buffer)
{
//stream = new MemoryStream(buffer);
//stream = new MemoryStream(buffer, 2, buffer.Length);
//reader = new BinaryReader(stream);
buffer = _buffer;
offset = 2;
ReadImpl();
RunImpl();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
///
protected internal byte[] readB(int Length)
{
byte[] numArray = new byte[Length];
Array.Copy((Array)this.buffer, this.offset, (Array)numArray, 0, Length);
this.offset += Length;
return numArray;
}
protected internal byte readC()
{
byte num = this.buffer[this.offset];
++this.offset;
return num;
}
protected internal int readD()
{
int int32 = BitConverter.ToInt32(this.buffer, this.offset);
this.offset += 4;
return int32;
}
protected internal double readF()
{
double num = BitConverter.ToDouble(this.buffer, this.offset);
this.offset += 8;
return num;
}
protected internal short readH()
{
short int16 = BitConverter.ToInt16(this.buffer, this.offset);
this.offset += 2;
return int16;
}
protected internal long readQ()
{
long int64 = BitConverter.ToInt64(this.buffer, this.offset);
this.offset += 8;
return int64;
}
protected internal string readS()
{
string str = "";
try
{
str = Encoding.Unicode.GetString(this.buffer, this.offset, this.buffer.Length - this.offset);
int length = str.IndexOf(char.MinValue);
if (length != -1)
str = str.Substring(0, length);
this.offset += str.Length + 1;
}
catch (Exception ex)
{
//CLogger.getInstance().error("while reading string from packet, " + ex.Message + " " + ex.StackTrace);
}
return str;
}
protected internal string readS(int Length)
{
string str = "";
try
{
str = Encoding.GetEncoding(1251).GetString(this.buffer, this.offset, Length);
int length = str.IndexOf(char.MinValue);
if (length != -1)
str = str.Substring(0, length);
this.offset += Length;
}
catch (Exception ex)
{
//CLogger.getInstance().error("while reading string from packet, " + ex.Message + " " + ex.StackTrace);
}
return str;
}
public virtual void ReadImpl()
{
throw new Exception("Cannot read empty packet!");
}
public virtual void RunImpl()
{
throw new Exception("Cannot run empty packet!");
}
}
}
[/SRC]
[SRC="csharp"]using Auth_Server.Enums;
using Auth_Server.Network.Packets.Server;
using Commons.Network;
using Commons.Utils;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Auth_Server.Network
{
public class ClientConnection: IClientConnection
{
public delegate void MethodContainer(ClientConnection session);
public event MethodContainer OnDisconnected;
byte[] buffer = new byte[2048];
public NetworkStream Stream { get; set; }
public TcpClient Client { get; set; }
public int Id { get; set; }
public byte[] RemoteIPAddress { get; set; }
public int RemoteUdpPort { get; set; }
public void Accept(TcpClient client)
{
Client = client;
Stream = client.GetStream();
RemoteIPAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.GetAddressBytes();
RemoteUdpPort = 29890;
SendPacket(new PROTOCOL_BASE_CONNECT_ACK(this));
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, null);
}
public void BeginRead(IAsyncResult asyncResult)
{
try
{
int received = Stream.EndRead(asyncResult);
if (received != 0)
{
while (received >= 6)
{
int length = BitConverter.ToUInt16(buffer, 0) & 0x7FFF;
byte[] temp = new byte[length + 2];
Array.Copy(buffer, 2, temp, 0, temp.Length);
int bits = Id % 7 + 1;
BitShift.Unshift(temp, bits);
byte[] opcode = new byte[] { temp[0], temp[1] };
RecvOpcode packet = (RecvOpcode)BitConverter.ToUInt16(opcode, 0);
Type t = Type.GetType("Auth_Server.Network.Packets.Client." + packet.ToString());
Logger.Debug("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
if (t != null)
{
ClientPacket clientpacket = (ClientPacket)Activator.CreateInstance(t);
clientpacket.Client = this;
clientpacket.Init(temp);
Logger.Debug("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
}
else
{
Logger.Info("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
// Logger.Trace(temp.ToHex());
}
received -= length + 4;
Array.Copy(buffer, length + 4, buffer, 0, received); // << Копируем оставшиеся данные в начало буфера
}
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, Stream);
}
else
{
OnDisconnected(this);
}
}
catch (IOException)
{
OnDisconnected(this);
}
catch (Exception ex)
{
OnDisconnected(this);
//Log.Error(ex);
Logger.Error($"{ex.Message}\n{ex.StackTrace}");
}
}
public void SendPacket(ServerPacket packet)
{
packet.Client = this;
packet.WriteImpl();
byte[] data = packet.ToByteArray();
byte[] lenght = BitConverter.GetBytes((short)data.Length);
byte[] opcode = BitConverter.GetBytes(Convert.ToInt16((int)Enum.Parse(typeof(SendOpcode), packet.GetType().Name)));
byte[] buff = new byte[data.Length + 4];
Array.Copy(lenght, 0, buff, 0, lenght.Length);
Array.Copy(opcode, 0, buff, 2, opcode.Length);
Array.Copy(data, 0, buff, 4, data.Length);
//Stream.Write(buff, 0, buff.Length);
Stream.BeginWrite(buff, 0, buff.Length, new AsyncCallback(EndSendCallBackStatic), 0);
}
public void EndSendCallBackStatic(IAsyncResult result)
{
Stream.EndWrite(result);
Stream.Flush();
}
public static void runNewThread(Thread t) { t.Start(); }
}
}
[/SRC]
[SRC="csharp"]using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Commons.Network
{
public class ClientPacket
{
public IClientConnection Client;
MemoryStream stream;
BinaryReader reader;
private int offset;
private byte[] buffer;
/// <summary>
///
/// </summary>
/// <param name="buffer"></param>
public void Init(byte[] _buffer)
{
//stream = new MemoryStream(buffer);
//stream = new MemoryStream(buffer, 2, buffer.Length);
//reader = new BinaryReader(stream);
buffer = _buffer;
offset = 2;
ReadImpl();
RunImpl();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
///
protected internal byte[] readB(int Length)
{
byte[] numArray = new byte[Length];
Array.Copy((Array)this.buffer, this.offset, (Array)numArray, 0, Length);
this.offset += Length;
return numArray;
}
protected internal byte readC()
{
byte num = this.buffer[this.offset];
++this.offset;
return num;
}
protected internal int readD()
{
int int32 = BitConverter.ToInt32(this.buffer, this.offset);
this.offset += 4;
return int32;
}
protected internal double readF()
{
double num = BitConverter.ToDouble(this.buffer, this.offset);
this.offset += 8;
return num;
}
protected internal short readH()
{
short int16 = BitConverter.ToInt16(this.buffer, this.offset);
this.offset += 2;
return int16;
}
protected internal long readQ()
{
long int64 = BitConverter.ToInt64(this.buffer, this.offset);
this.offset += 8;
return int64;
}
protected internal string readS()
{
string str = "";
try
{
str = Encoding.Unicode.GetString(this.buffer, this.offset, this.buffer.Length - this.offset);
int length = str.IndexOf(char.MinValue);
if (length != -1)
str = str.Substring(0, length);
this.offset += str.Length + 1;
}
catch (Exception ex)
{
//CLogger.getInstance().error("while reading string from packet, " + ex.Message + " " + ex.StackTrace);
}
return str;
}
protected internal string readS(int Length)
{
string str = "";
try
{
str = Encoding.GetEncoding(1251).GetString(this.buffer, this.offset, Length);
int length = str.IndexOf(char.MinValue);
if (length != -1)
str = str.Substring(0, length);
this.offset += Length;
}
catch (Exception ex)
{
//CLogger.getInstance().error("while reading string from packet, " + ex.Message + " " + ex.StackTrace);
}
return str;
}
public virtual void ReadImpl()
{
throw new Exception("Cannot read empty packet!");
}
public virtual void RunImpl()
{
throw new Exception("Cannot run empty packet!");
}
}
}
[/SRC]
[SRC="csharp"]using Auth_Server.Enums;
using Auth_Server.Network.Packets.Server;
using Commons.Network;
using Commons.Utils;
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Auth_Server.Network
{
public class ClientConnection: IClientConnection
{
public delegate void MethodContainer(ClientConnection session);
public event MethodContainer OnDisconnected;
byte[] buffer = new byte[2048];
public NetworkStream Stream { get; set; }
public TcpClient Client { get; set; }
public int Id { get; set; }
public byte[] RemoteIPAddress { get; set; }
public int RemoteUdpPort { get; set; }
public void Accept(TcpClient client)
{
Client = client;
Stream = client.GetStream();
RemoteIPAddress = ((IPEndPoint)client.Client.RemoteEndPoint).Address.GetAddressBytes();
RemoteUdpPort = 29890;
SendPacket(new PROTOCOL_BASE_CONNECT_ACK(this));
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, null);
}
public void BeginRead(IAsyncResult asyncResult)
{
try
{
int received = Stream.EndRead(asyncResult);
if (received != 0)
{
while (received >= 6)
{
int length = BitConverter.ToUInt16(buffer, 0) & 0x7FFF;
byte[] temp = new byte[length + 2];
Array.Copy(buffer, 2, temp, 0, temp.Length);
int bits = Id % 7 + 1;
BitShift.Unshift(temp, bits);
byte[] opcode = new byte[] { temp[0], temp[1] };
RecvOpcode packet = (RecvOpcode)BitConverter.ToUInt16(opcode, 0);
Type t = Type.GetType("Auth_Server.Network.Packets.Client." + packet.ToString());
Logger.Debug("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
if (t != null)
{
ClientPacket clientpacket = (ClientPacket)Activator.CreateInstance(t);
clientpacket.Client = this;
clientpacket.Init(temp);
Logger.Debug("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
}
else
{
Logger.Info("PacketId = {0}", BitConverter.ToUInt16(opcode, 0), buffer.Length);
// Logger.Trace(temp.ToHex());
}
received -= length + 4;
Array.Copy(buffer, length + 4, buffer, 0, received); // << Копируем оставшиеся данные в начало буфера
}
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, Stream);
}
else
{
OnDisconnected(this);
}
}
catch (IOException)
{
OnDisconnected(this);
}
catch (Exception ex)
{
OnDisconnected(this);
//Log.Error(ex);
Logger.Error($"{ex.Message}\n{ex.StackTrace}");
}
}
public void SendPacket(ServerPacket packet)
{
packet.Client = this;
packet.WriteImpl();
byte[] data = packet.ToByteArray();
byte[] lenght = BitConverter.GetBytes((short)data.Length);
byte[] opcode = BitConverter.GetBytes(Convert.ToInt16((int)Enum.Parse(typeof(SendOpcode), packet.GetType().Name)));
byte[] buff = new byte[data.Length + 4];
Array.Copy(lenght, 0, buff, 0, lenght.Length);
Array.Copy(opcode, 0, buff, 2, opcode.Length);
Array.Copy(data, 0, buff, 4, data.Length);
//Stream.Write(buff, 0, buff.Length);
Stream.BeginWrite(buff, 0, buff.Length, new AsyncCallback(EndSendCallBackStatic), 0);
}
public void EndSendCallBackStatic(IAsyncResult result)
{
Stream.EndWrite(result);
Stream.Flush();
}
public static void runNewThread(Thread t) { t.Start(); }
}
}
[/SRC]