Exemplo n.º 1
0
        public AddonConnection(string address, int port, string sessionId, EndlessClient client)
        {
            this.Client       = client;
            this.Serializer   = new BinarySerializer();
            this.Deserializer = new BinaryDeserializer();
            this.SessionId    = sessionId;

            this.Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.Socket.Connect(address, port);

            this.Stream = new NetworkStream(this.Socket);
            this.Stream.BeginRead(this.Buffer, 0, this.Buffer.Length, new AsyncCallback(this.ReceiveCallback), null);

            this.Deserializer.OnDeserializedMessage += (e) =>
            {
                this.OnMessage?.Invoke(this, e);
            };

            this.Send("init", Program.AddonVersion, this.SessionId);
        }
Exemplo n.º 2
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                if (!this.Socket.Connected)
                {
                    if (this.Stream != null)
                    {
                        Program.Console.Error("ProxyClient connection forcibly reset by peer.");
                    }
                    return;
                }

                var length   = this.Stream.EndRead(ar);
                var received = this.Buffer.Take(length).ToArray();

                if (length == 0)
                {
                    Program.Console.Error("ProxyClient connection forcibly reset by peer. (receiveBytes == 0)");
                    return;
                }

                if (this.State != ClientState.Initialized)
                {
                    var packet = new Packet(received.Skip(2).ToArray());

                    if (packet.Family == PacketFamily.Init && packet.Action == PacketAction.Init)
                    {
                        this.State = ClientState.Initialized;

                        packet.Skip(3);
                        var recv_multi = packet.GetByte();
                        var send_multi = packet.GetByte();

                        this.PacketProcessor.SetMulti(recv_multi, send_multi);
                    }
                }

                var decode_buffer = received.Skip(2).ToArray();
                this.PacketProcessor.Decode(ref decode_buffer);
                var decode_packet = new Packet(decode_buffer);

                Program.Console.Information("({0}) {1} {2} | (length: {3})", "server->client",
                                            decode_packet.Family, decode_packet.Action, decode_packet.Length);

                if (decode_packet.Family == PacketFamily.Talk && decode_packet.Action == PacketAction.Announce)
                {
                    var name    = decode_packet.GetBreakString();
                    var message = decode_packet.GetBreakString();

                    if (name == "LunaAddons")
                    {
                        var port      = int.Parse(message.Split(' ')[0]);
                        var sessionId = message.Split(' ')[1];

                        var memory = new MemorySharp(Process.GetCurrentProcess());
                        var client = new EndlessClient(memory);

                        client.SetupAddonConnection(this.EOAddress, port, sessionId);

                        goto skip_packet;
                    }
                }

                // send the EO client the received bytes from EO server.
                this.Session.Send(received);

skip_packet:
                this.Stream?.BeginRead(this.Buffer, 0, this.Buffer.Length, new AsyncCallback(this.ReceiveCallback), null);
            }
            catch (IOException)
            {
                if (this.Session != null && this.Session.IsConnected)
                {
                    this.Session.Disconnect();
                }

                if (this.Socket != null && this.Socket.Connected)
                {
                    this.Socket.Disconnect(true);
                }
            }
            catch (SocketException)
            {
                if (this.Session != null && this.Session.IsConnected)
                {
                    this.Session.Disconnect();
                }

                if (this.Socket != null && this.Socket.Connected)
                {
                    this.Socket.Disconnect(true);
                }
            }
        }