Example #1
0
        public IPacket Decode(byte packetId, byte[] payload)
        {
            StarboundStream stream = new StarboundStream(payload);

            IPacket packet;

            /*
             * string val = null;
             * foreach (var s in payload)
             * {
             *  val += s;
             * }
             * SharpStarLogger.DefaultLogger.Info(string.Format("{0}{1}","Payload : ",val));
             */

            if (RegisteredPacketTypes.ContainsKey(packetId))
            {
                packet = RegisteredPacketTypes[packetId]();
            }
            else
            {
                packet = new UnknownPacket(Compressed, payload.Length, packetId);
            }

            if (packet != null)
            {
                packet.IsReceive = true;

                try
                {
                    packet.Read(stream);
                }
                catch (Exception e)
                {
                    SharpStarLogger.DefaultLogger.Error("Packet {0} caused an error!", packet.GetType().Name);

                    e.LogError();
                }
            }

            stream.Dispose();

            return(packet);
        }
Example #2
0
        public async Task FlushPackets()
        {
            while (PacketQueue.Count > 0)
            {
                try
                {
                    IPacket next;

                    if (!PacketQueue.TryDequeue(out next))
                    {
                        continue;
                    }

                    if (!next.IsReceive)
                    {
                        foreach (var handler in Server.PacketHandlers)
                        {
                            if (next.PacketId == handler.PacketId)
                            {
                                await handler.Handle(next, OtherClient);
                            }
                        }
                    }

                    if (next.Ignore)
                    {
                        continue;
                    }

                    var stream = new StarboundStream();
                    next.Write(stream);

                    byte[] buffer     = stream.ToArray();
                    bool   compressed = stream.Length >= 512;

                    if (compressed)
                    {
                        buffer = ZlibStream.CompressBuffer(buffer);
                    }

                    stream.Dispose();

                    int length = compressed ? -buffer.Length : buffer.Length;

                    var finalStream = new StarboundStream();

                    finalStream.WriteUInt8(next.PacketId);
                    finalStream.WriteSignedVLQ(length);
                    finalStream.Write(buffer, 0, buffer.Length);

                    byte[] toSend = finalStream.ToArray();

                    finalStream.Dispose();

                    if (Socket == null)
                    {
                        return;
                    }

                    var token = new AsyncUserToken();
                    token.Socket = Socket;

                    SocketAsyncEventArgs writeArgs = new SocketAsyncEventArgs();
                    writeArgs.RemoteEndPoint = Socket.RemoteEndPoint;
                    writeArgs.UserToken      = token;
                    writeArgs.SetBuffer(toSend, 0, toSend.Length);
                    writeArgs.Completed += IO_Completed;

                    Socket.SendAsync(writeArgs);
                }
                catch
                {
                    CloseClientSocket(readEventArgs);
                }
            }
        }