public void SendAuthRequest(Server server)
 {
     Log.Debug("SendAuthRequest");
     Packet sp = new Packet(Packet.Opcodes.Auth);
     sp.AddString(server.AuthKey);
     _Client.SendPacket(server,sp);
 }
 public void SendNativeFunction(Server server, string guid,string name, string args, DataStream data)
 {
     Log.Debug("SendNativeFunction: "+name);
     Packet sp = new Packet(Packet.Opcodes.FunctionRequest);
     sp.AddString(guid);
     sp.AddString(name); // name
     sp.AddInt32(0); // response
     sp.AddString(args);
     sp.AddData(data.Data,data.Length);
     _Client.SendPacket(server, sp);
 }
        static void Main(string[] args)
        {
            Log l = new Log("DotnetClient", true);
            Log.Message(Name + " " + Version);
            Log.Message(Website);

            Client.Client client = new Client.Client();
            Server server = new Server();
            while (true)
            {
                if (!server.IsConnected)
                {
                    client.Connect(server);
                    System.Threading.Thread.Sleep(1000);
                    if (!server.IsAuthenticated) client.Disconnect(server);
                    else API.APIMain.Init();
                }
                System.Threading.Thread.Sleep(1000);
				//client.PakSender.SendTest(server);
            }
        }
 public void SendTest(Server server)
 {
     Log.Debug("Sending Test packet");
     Packet sp = new Packet(Packet.Opcodes.Test);
     sp.AddString("TEST");
     sp.AddInt32(300);
     sp.AddByte((byte)32);
     sp.AddFloat32(1.234F);
     sp.AddString("TEST2");
     _Client.SendPacket(server, sp);
 }
 public void SendPingReply(Server server)
 {
     Log.Debug("SendPingReply");
     Packet sp = new Packet(Packet.Opcodes.Ping);
     _Client.SendPacket(server,sp);
 }
 public Server()
 {
     Instance = this;
     LoadConfig();
 }
        public bool Connect(Server server)
        {
            Log.Debug("Connecting to server: " + server.Address);
            _Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            System.Net.IPAddress ipAdd = System.Net.IPAddress.Parse(server.Address);
            RemoteEP = new IPEndPoint(ipAdd, server.Port);
            try
            {
                _Socket.Connect(RemoteEP);
            }
            catch (Exception ex)
            {
                Log.Warning("Connection failed");
                return false;
            }
            _Server = server;
            _Server.IsConnected = true;

            Thread t = new Thread(new ParameterizedThreadStart(WaitReceive));
            t.Start(_Server);
            t = new Thread(new ParameterizedThreadStart(WaitSend));
            t.Start(_Server);
            t = new Thread(new ParameterizedThreadStart(CheckConnectionTimeout));
            t.Start(_Server);
            Log.Debug("Connection successful");

            Thread.Sleep(100);
            PakSender.SendAuthRequest(server);

            return true;
        }
        public void SendPacket(Server server, Packet packet)
        {
            // just adds the packet data to clients send buffer
            if (packet == null) return;
            if (packet.Opcode == 0x00) return;
            //packet.SetLength();
            packet.Length += packet.headerlength;
            if (packet.Length <= 0) return;
            if (server.sbufpos + packet.Length >= Server.MAX_BUFF) return;
            while (server.sbuflock) { Thread.Sleep(1); }
            server.sbuflock = true;

            byte[] bar = BitConverter.GetBytes(packet.Length);
            server.sendbuf[server.sbufpos + 0] = bar[0];
            server.sendbuf[server.sbufpos + 1] = bar[1];
            server.sendbuf[server.sbufpos + 2] = packet.Opcode;
            for (int i = 0; i < packet.Length-packet.headerlength; i++) { server.sendbuf[server.sbufpos + packet.headerlength + i] = packet.Data[i]; }

            server.sbufpos += packet.Length;
            server.sbuflock = false;
            packet.Length -= packet.headerlength;

            InternalEvents.FireOnPacketSent(this, new OnPacketSentEventArgs(server, this, packet));
        }
 public bool Disconnect(Server server)
 {
     server.IsAuthenticated = false;
     server.IsConnected = false;
     Thread.Sleep(100);
     _Socket.Close();
     return true;
 }
 public OnCallbackReceivedEventArgs(Server server, Client client, string callbackname, DataStream data)
 {
     _Server = server;
     _Client = client;
     CallbackName = callbackname;
     Data = data;
 }
 public OnPacketSentEventArgs(Server server, Client client, Packet pak)
 {
     _Server = server;
     _Client = client;
     Pak = pak;
 }
 public OnFunctionRequestReceivedEventArgs(Server server, Client client, string funcname, string callbackname, string paramtypes, Object[] args)
 {
     Server = server;
     Client = client;
     FunctionName = funcname;
     CallbackName = callbackname;
     Paramtypes = paramtypes;
     Args = args;
 }
        public NativeFunction RequestFunctionWithArgs(Server server,NativeFunction function)
        {
            request = function;
            response = null;
            function.Guid = System.Guid.NewGuid().ToString();

            InternalEvents.OnPacketReceived += OnPacketReceived;

            _Client.PakSender.SendNativeFunction(server, function.Guid, function.Name, function.Args, function.Data);

            int i=0;
            while ((response == null) && (i < MAX_RESPONSETIME))
            {
                System.Threading.Thread.Sleep(1);
                i++;
            }
            if ((response == null) || (i >= MAX_RESPONSETIME))
            {
                Log.Warning("Native function request failed.");
                InternalEvents.OnPacketReceived -= OnPacketReceived;
                return null;
            }
            InternalEvents.OnPacketReceived -= OnPacketReceived;
            response.Data.Pos = 0;
            return response;
        }