Esempio n. 1
0
 public override byte[] ToByteArray()
 {
     Builder<byte> b = new Builder<byte>();
     b.Append(PacketID);
     b.Append(Packet.PackString(Reason));
     return b.ToArray();
 }
Esempio n. 2
0
 public override byte[] ToByteArray()
 {
     Builder<byte> b = new Builder<byte>();
     b.Append(PacketID);
     b.Append(PlayerID);
     return b.ToArray();
 }
Esempio n. 3
0
 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();
 }
Esempio n. 4
0
 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);
     }
 }
Esempio n. 5
0
 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();
 }
Esempio n. 6
0
 public override byte[] ToByteArray()
 {
     Builder<Byte> builder = new Builder<byte>();
     builder.Append(PacketID);
     builder.Append(PlayerID);
     builder.Append(Packet.PackShort(X));
     builder.Append(Packet.PackShort(Y));
     builder.Append(Packet.PackShort(Z));
     builder.Append(Heading);
     builder.Append(Pitch);
     return builder.ToArray();
 }
Esempio n. 7
0
 public override byte[] ToByteArray()
 {
     Builder<Byte> builder = new Builder<byte>();
     builder.Append(PacketID);
     builder.Append(Version);
     builder.Append(Packet.PackString(Username));
     builder.Append(Packet.PackString(Key));
     builder.Append(Unused);
     return builder.ToArray();
 }
Esempio n. 8
0
 public override byte[] ToByteArray()
 {
     Builder<Byte> b = new Builder<byte>();
     b.Append(PacketID);
     b.Append(Unused);
     b.Append(Packet.PackString(Message));
     return b.ToArray();
 }
Esempio n. 9
0
        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());
            }
        }
Esempio n. 10
0
        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;
                }
            }
        }
Esempio n. 11
0
        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());
        }
Esempio n. 12
0
 public override byte[] ToByteArray()
 {
     Builder<byte> b = new Builder<byte>();
     b.Append(PacketID);
     b.Append(Packet.PackShort(ChunkLength));
     b.Append(ChunkData);
     b.Append(PercentComplete);
     return b.ToArray();
 }
Esempio n. 13
0
 public override byte[] ToByteArray()
 {
     Builder<byte> b = new Builder<byte>();
     b.Append(PacketID);
     b.Append(Version);
     b.Append(Packet.PackString(Name));
     b.Append(Packet.PackString(MOTD));
     b.Append(Type);
     return b.ToArray();
 }