Ejemplo n.º 1
0
        private void button_connect_Click(object sender, EventArgs e)
        {
            if (textBox_username.Text.Trim() != "" && textBox_password.Text.Trim() != "")
            {
                PacketWriter pw = new PacketWriter();
                pw.WriteByte(1);
                pw.WriteString(textBox_username.Text.Trim());
                pw.WriteString(textBox_password.Text.Trim());

                byte[] response = client.SendRequest(pw.ToArray());

                PacketReader pr = new PacketReader(response);
                byte r = pr.ReadByte();

                if(r == 0)
                {
                    MessageBox.Show("Connection successful !");
                }
                else if(r == 2)
                {
                    MessageBox.Show("Banned : " + pr.ReadString());
                }
                else
                {
                    MessageBox.Show("Wrong password or username !");
                }

            }
        }
Ejemplo n.º 2
0
        private void Client_OnDataReceived(byte[] buffer)
        {
            PacketReader pr = new PacketReader(buffer);
            Byte ID = pr.ReadByte();

            if(ID == 1)
            {
                int id = pr.ReadInt32();
                string message = pr.ReadString();
                string msg = id + " : " + message;
                listBox_messages.Items.Add(msg);
            }
        }
Ejemplo n.º 3
0
        static void server_OnDataReceived(eSClient client, byte[] buffer)
        {
            PacketReader pr = new PacketReader(buffer);
            Byte ID = pr.ReadByte();

            if(ID == 1)
            {
                string message = pr.ReadString();
                PacketWriter pw = new PacketWriter();
                pw.WriteByte(ID);
                pw.WriteInt32(server.GetIDFromTcpClient(client.GetTcpClient()));
                pw.WriteString(message);
                server.SendToAll(pw.ToArray());

                Console.WriteLine("Message received from " + client.GetID() + " : " + message);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Handle request client
        /// </summary>
        /// <param name="client">Client</param>
        /// <returns></returns>
        private async Task HandleClientRequestAsync(TcpClient client)
        {
            byte[] buffer, bufferSize = new byte[2];
            int size;

            try
            {
                NetworkStream clientStream = client.GetStream();
                while (client.Client.Connected)
                {
                    int bytesRead = await clientStream.ReadAsync(bufferSize, 0, bufferSize.Length);

                    if (bytesRead == 0)
                        break;

                    using (PacketReader pr = new PacketReader(bufferSize))
                    {
                        size = pr.ReadInt16();
                    }

                    buffer = new byte[size];
                    await clientStream.ReadAsync(buffer, 0, buffer.Length);

                    if (OnRequestReceived != null)
                        OnRequestReceived(client, buffer);
                }
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to handle client request : " + ex.Message);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Connect the client
        /// </summary>
        /// <returns>Success</returns>
        public bool Connect()
        {
            try
            {
                Client.Connect(Hostname, Port);

                if (ClientRequest != null)
                    ClientRequest.Connect(Hostname, PortRequest);

                byte[] idBuffer = new byte[4];
                Client.GetStream().Read(idBuffer, 0, idBuffer.Length);

                using (PacketReader pr = new PacketReader(idBuffer))
                {
                    ID = pr.ReadInt32();
                }

                if (OnConnected != null)
                    OnConnected();
                StartHandle();

                DebugMessage("Connected successfully");

                Connected = true;

                return true;
            }
            catch (Exception ex)
            {
                DebugMessage("Failed to connect : " + ex.Message);
                Disconnect();
                return false;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Handle client
        /// </summary>
        /// <returns></returns>
        private async Task HandleAsync()
        {
            byte[] buffer, bufferSize = new byte[2];
            int size;

            try
            {
                NetworkStream clientStream = Client.GetStream();
                while (Client.Connected)
                {
                    int bytesRead = await clientStream.ReadAsync(bufferSize, 0, bufferSize.Length);

                    if (bytesRead == 0)
                        break;

                    using (PacketReader pr = new PacketReader(bufferSize))
                    {
                        size = pr.ReadInt16();
                    }

                    buffer = new byte[size];
                    await clientStream.ReadAsync(buffer, 0, buffer.Length);
                    if (OnDataReceived != null)
                        OnDataReceived(buffer);
                }
            }
            catch(Exception ex)
            {
                //HResult == -2146232800 = Server closed
                if(Connected && ex.HResult != -2146232800)
                    DebugMessage("Failed to handle client : " + ex.Message);
            }
            finally
            {
                Disconnect();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Send a request
        /// </summary>
        /// <param name="buffer">Buffer to send</param>
        /// <returns>Response</returns>
        public byte[] SendRequest(byte[] buffer)
        {
            if (ClientRequest != null)
            {
                try
                {
                    byte[] b = eUtils.GetBuffer(buffer);
                    ClientRequest.Send(b);

                    byte[] result, resultSize = new byte[2];
                    int size;


                    ClientRequest.GetStream().Read(resultSize, 0, resultSize.Length);

                    using (PacketReader pr = new PacketReader(resultSize))
                    {
                        size = pr.ReadInt16();
                    }

                    result = new byte[size];

                    ClientRequest.GetStream().Read(result, 0, result.Length);

                    return result;
                }
                catch (Exception ex)
                {
                    DebugMessage("Failed to send request : " + ex.Message);
                }
            }
            else
            {
                DebugMessage("This client cant send request");
            }
            return null;
        }