我用c#和Unity编写了一个基本的服务器客户端程序,以创建自己的网络。在此过程中,我编写了一个byteBuffer来通过网络发送数据。一切正常,但发送或接收字符串。 (我可以成功发送Integers和其他数据形式)。
我的DLL文件:
public class ByteBuffer : IDisposable
{
private List<byte> Buff;
private byte[] readBuff;
private int readpos;
private bool buffUpdated;
private bool disposedValue;
#region "Helpers"
public ByteBuffer()
{
this.buffUpdated = false;
this.disposedValue = false;
this.Buff = new List<byte>();
this.readpos = 0;
}
public long GetReadPos() =>
((long)this.readpos);
public byte[] ToArray() =>
this.Buff.ToArray();
public int Count() =>
this.Buff.Count;
public int Length() =>
(this.Count() - this.readpos);
public void Clear()
{
this.Buff.Clear();
this.readpos = 0;
}
#endregion
#region"Write Data"
public void WriteByte(byte Inputs)
{
Buff.Add(Inputs);
buffUpdated = true;
}
public void WriteBytes(byte[] Input)
{
this.Buff.AddRange(Input);
this.buffUpdated = true;
}
public void WriteInteger(int Input)
{
this.Buff.AddRange(BitConverter.GetBytes(Input));
this.buffUpdated = true;
}
public void WriteString(string Input)
{
this.Buff.AddRange(BitConverter.GetBytes(Input.Length));
this.Buff.AddRange(Encoding.ASCII.GetBytes(Input));
this.buffUpdated = true;
}
#endregion
#region "read Data"
public string ReadString(bool Peek = true)
{
int count = this.ReadInteger(true);
if (this.buffUpdated)
{
this.readBuff = this.Buff.ToArray();
this.buffUpdated = false;
}
string str = Encoding.ASCII.GetString(this.readBuff, this.readpos, count);
if ((Peek & (this.Buff.Count > this.readpos)) && (str.Length > 0))
{
this.readpos += count;
}
return str;
}
public byte ReadByte(bool Peek = true)
{
if (Buff.Count > readpos)
{
if (buffUpdated)
{
readBuff = Buff.ToArray();
buffUpdated = false;
}
byte ret = readBuff[readpos];
if (Peek & Buff.Count > readpos)
{
readpos += 1;
}
return ret;
}
else
{
throw new Exception("Byte Buffer is past its Limit!");
}
}
public byte[] ReadBytes(int Length, bool Peek = true)
{
if (this.buffUpdated)
{
this.readBuff = this.Buff.ToArray();
this.buffUpdated = false;
}
byte[] buffer = this.Buff.GetRange(this.readpos, Length).ToArray();
if (Peek)
{
this.readpos += Length;
}
return buffer;
}
public int ReadInteger(bool peek = true)
{
if (this.Buff.Count <= this.readpos)
{
throw new Exception("Byte Buffer Past Limit!");
}
if (this.buffUpdated)
{
this.readBuff = this.Buff.ToArray();
this.buffUpdated = false;
}
int num = BitConverter.ToInt32(this.readBuff, this.readpos);
if (peek & (this.Buff.Count > this.readpos))
{
this.readpos += 4;
}
return num;
}
#endregion
//IDisposable
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if(disposing)
{
this.Buff.Clear();
}
this.readpos = 0;
}
this.disposedValue = true;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
}
我的SendData类:
class ServerSendData{
public static ServerSendData instance = new ServerSendData();
public void SendDataToClient(int index, byte[] data)
{
ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer();
buffer.WriteBytes(data);
Network.Clients[index].myStream.BeginWrite(buffer.ToArray(), 0, buffer.ToArray().Length, null, null);
buffer = null;
}
public void SendWelcomeMessage(int index)
{
ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer();
buffer.WriteInteger(1); //paket nummer 1
buffer.WriteByte(2);
buffer.WriteString("A");
SendDataToClient(index, buffer.ToArray());
}
}
我的HandleData类:
公共类ClientHandleData:MonoBehaviour {
public void HandleData(byte[]data)
{
int packetNum;
ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer();
buffer.WriteBytes(data);
packetNum = buffer.ReadInteger();
buffer = null;
if (packetNum == 0)
{
return;
}
else
{
HandleMessages(packetNum, data);
}
}
public void HandleMessages(int packetNum, byte[] data)
{
switch (packetNum)
{
case 1:
HandleWelcomeMessage(data);
break;
}
}
public void HandleWelcomeMessage(byte[] data)
{
ByteBuffer.ByteBuffer buffer = new ByteBuffer.ByteBuffer();
buffer.WriteBytes(data);
Debug.Log("Test");
int nummer = buffer.ReadInteger();
Debug.Log("Test1: "+nummer);
byte bt = buffer.ReadByte();
Debug.Log("Test2: " +bt);
message = buffer.ReadString();
Debug.Log("Test3");
Debug.Log(nummer);
Debug.Log(message);
Debug.Log(bt);
buffer = null;
}
}
我的日志结果如下:
测试
测试1:1
Test2:2
使用Unity 2018.2.11f1个人64位