本文整理汇总了C#中Builder.ToArray方法的典型用法代码示例。如果您正苦于以下问题:C# Builder.ToArray方法的具体用法?C# Builder.ToArray怎么用?C# Builder.ToArray使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Builder
的用法示例。
在下文中一共展示了Builder.ToArray方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ToByteArray
public override byte[] ToByteArray()
{
Builder<byte> b = new Builder<byte>();
b.Append(PacketID);
b.Append(Packet.PackString(Reason));
return b.ToArray();
}
示例2: ToByteArray
public override byte[] ToByteArray()
{
Builder<Byte> b = new Builder<byte>();
b.Append(PacketID);
b.Append(Packet.PackShort(X));
b.Append(Packet.PackShort(Y));
b.Append(Packet.PackShort(Z));
b.Append(Mode);
b.Append(Type);
return b.ToArray();
}
示例3: GetChunk
public Chunk GetChunk(int chunkX, int chunkZ)
{
Builder<byte> b = new Builder<byte>();
b.Append(BitConverter.GetBytes(chunkX));
b.Append(BitConverter.GetBytes(chunkZ));
long index = BitConverter.ToInt64(b.ToArray(), 0);
if (_Chunks.ContainsKey(index)) {
return _Chunks[index];
} else {
return _Chunks[index] = new Chunk(chunkX, chunkZ, this);
}
}
示例4: GetBytes
public byte[] GetBytes()
{
Builder<Byte> builder = new Builder<Byte>();
builder.Append((byte[]) _Structure["Level"]["Blocks"].Payload);
builder.Append((byte[]) _Structure["Level"]["Data"].Payload);
builder.Append((byte[]) _Structure["Level"]["BlockLight"].Payload);
builder.Append((byte[]) _Structure["Level"]["SkyLight"].Payload);
return builder.ToArray();
}
示例5: Transmit
public void Transmit(PacketType type, params object[] args)
{
// Spacecraft.Log("Transmitting: " + type + "(" + (byte)type + ")");
string structure = (type == PacketType.Disconnect ? "bt" : PacketStructure.Data[(byte) type]);
Builder<Byte> packet = new Builder<Byte>();
packet.Append((byte) type);
byte[] bytes;
int current = 1;
try {
for (int i = 1; i < structure.Length; ++i) {
current = i;
switch (structure[i]) {
case 'b': // sbyte(1)
packet.Append((byte) (sbyte) args[i-1]);
break;
case 's': // short(2)
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short) args[i-1])));
break;
case 'f': // float(4)
bytes = BitConverter.GetBytes((float) args[i-1]);
for (int j = 3; j >= 0; --j) {
packet.Append(bytes[j]);
}
//packet.Append(bytes);
break;
case 'i': // int(4)
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int) args[i-1])));
break;
case 'd': // double(8)
bytes = BitConverter.GetBytes((double) args[i-1]);
for (int j = 7; j >= 0; --j) {
packet.Append(bytes[j]);
}
//packet.Append(bytes);
break;
case 'l': // long(8)
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((long) args[i-1])));
break;
case 't': // string
bytes = Encoding.UTF8.GetBytes((string) args[i-1]);
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short) bytes.Length)));
packet.Append(bytes);
break;
case 'x': // byte array
packet.Append((byte[]) args[i-1]);
break;
case 'I': // inventory entity
// short; if nonnegative, byte then short.
InventoryItem item = (InventoryItem) args[i - 1];
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(item.Type)));
if (item.Type >= 0) {
packet.Append(item.Count);
packet.Append(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(item.Damage)));
}
break;
default:
throw new NotImplementedException("Unimplemented data type (transmit)");
}
}
}
catch (InvalidCastException) {
Spacecraft.Log("[Error] Transmitting " + type + ": expected '" + structure[current] +
"', got " + args[current - 1].GetType().ToString() + " for argument " + current + " (format: " + structure + ")");
throw;
}
lock (_TransmitQueue) {
_TransmitQueue.Enqueue(packet.ToArray());
}
}
示例6: IncomingData
private void IncomingData()
{
NetworkStream stream = _Client.GetStream();
Builder<byte> buffer = new Builder<byte>();
buffer.Append(_Buffer);
while (stream.DataAvailable) {
buffer.Append((byte) stream.ReadByte());
}
_Buffer = buffer.ToArray();
buffer = null;
while (_Buffer.Length > 0) {
Pair<int, object[]> pair = CheckCompletePacket();
int length = pair.First;
if (length > 0) {
//byte[] packet = new byte[length];
//Array.Copy(_Buffer, packet, length);
byte[] newBuffer = new byte[_Buffer.Length - length];
Array.Copy(_Buffer, length, newBuffer, 0, _Buffer.Length - length);
_Buffer = newBuffer;
ProcessPacket(pair.Second);
} else {
break;
}
}
}
示例7: CheckCompletePacket
private Pair<int, object[]> CheckCompletePacket()
{
Pair<int, object[]> nPair = new Pair<int, object[]>(0, null);
PacketType type = (PacketType) _Buffer[0];
if (_Buffer[0] >= PacketStructure.Data.Length && _Buffer[0] != 0xFF) {
Spacecraft.Log("Got invalid packet: " + _Buffer[0]);
return nPair;
}
string structure = (type == PacketType.Disconnect ? "bt" : PacketStructure.Data[_Buffer[0]]);
int bufPos = 0;
Builder<object> data = new Builder<object>();
byte[] bytes = new byte[8];
for (int i = 0; i < structure.Length; ++i) {
switch (structure[i]) {
case 'b': // sbyte(1)
if ((bufPos + 1) > _Buffer.Length) return nPair;
if (i == 0)
data.Append((byte) _Buffer[bufPos]);
else
data.Append((sbyte) _Buffer[bufPos]);
bufPos += 1;
break;
case 's': // short(2)
if ((bufPos + 2) > _Buffer.Length) return nPair;
data.Append((short) IPAddress.NetworkToHostOrder(BitConverter.ToInt16(_Buffer, bufPos)));
bufPos += 2;
break;
case 'f': // float(4)
if ((bufPos + 4) > _Buffer.Length) return nPair;
for (int j = 0; j < 4; ++j) {
bytes[j] = _Buffer[bufPos + 3 - j];
}
data.Append((float) BitConverter.ToSingle(bytes, 0));
bufPos += 4;
break;
case 'i': // int(4)
if ((bufPos + 4) > _Buffer.Length) return nPair;
data.Append((int) IPAddress.NetworkToHostOrder(BitConverter.ToInt32(_Buffer, bufPos)));
bufPos += 4;
break;
case 'd': // double(8)
if ((bufPos + 8) > _Buffer.Length) return nPair;
for (int j = 0; j < 8; ++j) {
bytes[j] = _Buffer[bufPos + 7 - j];
}
data.Append((double) BitConverter.ToDouble(bytes, 0));
bufPos += 8;
break;
case 'l': // long(8)
if ((bufPos + 8) > _Buffer.Length) return nPair;
data.Append((long) IPAddress.NetworkToHostOrder(BitConverter.ToInt64(_Buffer, bufPos)));
bufPos += 8;
break;
case 't': // string
if ((bufPos + 2) > _Buffer.Length) return nPair;
short len = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(_Buffer, bufPos));
if ((bufPos + 2 + len) > _Buffer.Length) return nPair;
data.Append((string) Encoding.UTF8.GetString(_Buffer, bufPos + 2, len));
bufPos += (2 + len);
break;
case 'I': // inventory entity
// short; if nonnegative, byte then short.
InventoryItem item;
if ((bufPos + 2) > _Buffer.Length) return nPair;
item.Type = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(_Buffer, bufPos));
if (item.Type >= 0) {
if ((bufPos + 3) > _Buffer.Length) return nPair;
item.Count = _Buffer[bufPos + 2];
item.Damage = (short) IPAddress.NetworkToHostOrder(BitConverter.ToInt16(_Buffer, bufPos + 3));
bufPos += 5;
} else {
item.Count = 0;
item.Damage = 0;
bufPos += 2;
}
data.Append(item);
break;
default:
throw new NotImplementedException("Unimplemented data type (recieve)");
}
}
return new Pair<int, object[]>(bufPos, data.ToArray());
}