Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (connected)
            {
                openFileDialog1.ShowHelp = true;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    chatLib.Message message = new chatLib.Message(chatLib.Message.Header.FILE);

                    byte[] bytes    = File.ReadAllBytes(openFileDialog1.FileName);
                    string contents = System.Convert.ToBase64String(bytes);
                    message.addData(kullaniciAdi);
                    message.addData(openFileDialog1.SafeFileName);
                    message.addData(contents);

                    if (send == null || send.IsCompleted)
                    {
                        send = Task.Factory.StartNew(() => Send(message));
                    }
                    else
                    {
                        send.ContinueWith(antecendent => Send(message));
                    }

                    LogWrite(string.Format("{0} - {1} Dosyası Gönderildi.", kullaniciAdi, openFileDialog1.SafeFileName));
                }
            }
            else
            {
                MessageBox.Show("Bağlantı Yok");
            }
        }
Beispiel #2
0
        private void SendTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.Handled          = true;
                e.SuppressKeyPress = true;
                if (sendTextBox.Text != "")
                {
                    String msg = sendTextBox.Text;
                    sendTextBox.Clear();
                    LogWrite("<- You -> " + msg);
                    if (connected)
                    {
                        chatLib.Message message = new chatLib.Message(chatLib.Message.Header.POST);
                        message.addData(kullaniciAdi);
                        message.addData(msg);

                        if (send == null || send.IsCompleted)
                        {
                            send = Task.Factory.StartNew(() => Send(message));
                        }
                        else
                        {
                            send.ContinueWith(antecendent => Send(message));
                        }
                    }
                }
            }
        }
Beispiel #3
0
 public void Send(chatLib.Message msg)
 {
     try
     {
         IFormatter   formatter = new BinaryFormatter();
         MemoryStream memStream = new MemoryStream();
         formatter.Serialize(memStream, msg);
         byte[] buffer = memStream.GetBuffer();
         obj.stream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(Write), null);
     }
     catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
     catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
 }
Beispiel #4
0
        private void Send(chatLib.Message msg, long id = -1)
        {
            foreach (KeyValuePair <long, MyClient> obj in list)
            {
                if (id != obj.Value.id)
                {
                    try
                    {
                        IFormatter   formatter = new BinaryFormatter();
                        MemoryStream memStream = new MemoryStream();
                        formatter.Serialize(memStream, msg);
                        byte[] buffer = memStream.GetBuffer();

                        obj.Value.stream.BeginWrite(buffer, 0, buffer.Length, new AsyncCallback(Write), obj.Value);
                    }
                    catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                    catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                }
            }
        }
Beispiel #5
0
        private void Read(IAsyncResult result)
        {
            int bytes = 0;

            if (obj.client.Connected)
            {
                try
                {
                    bytes = obj.stream.EndRead(result);
                }
                catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
            }
            if (bytes > 0)
            {
                // obj.data.AppendFormat("{0}", Encoding.UTF8.GetString(obj.buffer, 0, bytes));



                bool dataAvailable = false;
                try
                {
                    dataAvailable = obj.stream.DataAvailable;
                }
                catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                if (dataAvailable)
                {
                    try
                    {
                        obj.stream.BeginRead(obj.buffer, 0, obj.buffer.Length, new AsyncCallback(Read), null);
                    }
                    catch (IOException e)
                    {
                        LogWrite(String.Format("[/ {0} /]", e.Message));
                        obj.handle.Set();
                    }
                    catch (ObjectDisposedException e)
                    {
                        LogWrite(String.Format("[/ {0} /]", e.Message));
                        obj.handle.Set();
                    }
                }
                else
                {
                    IFormatter      formatter = new BinaryFormatter();
                    MemoryStream    ms        = new MemoryStream(obj.buffer);
                    chatLib.Message msg       = (chatLib.Message)formatter.Deserialize(ms);

                    switch (msg.Head)
                    {
                    case chatLib.Message.Header.POST:
                        LogWrite(string.Format("{0} - {1}", msg.MessageList[0], msg.MessageList[1]));
                        break;

                    case chatLib.Message.Header.FILE:
                        LogWrite(string.Format("{0} - {1} Dosya Gönderdi.", msg.MessageList[0], msg.MessageList[1]));
                        byte[]     buffer = System.Convert.FromBase64String(msg.MessageList[2]);
                        FileStream fs     = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "download\\" + msg.MessageList[1], FileMode.CreateNew);
                        fs.Write(buffer, 0, buffer.Length);
                        fs.Close();
                        break;

                    case chatLib.Message.Header.REGISTER:
                        msgEvent?.Invoke(msg);
                        break;

                    case chatLib.Message.Header.JOIN:
                        msgEvent?.Invoke(msg);
                        break;
                    }
                    //obj.data.Clear();
                    obj.handle.Set();
                }
            }
            else
            {
                obj.client.Close();
                obj.handle.Set();
            }
        }
Beispiel #6
0
        private void Read(IAsyncResult result)
        {
            MyClient obj   = (MyClient)result.AsyncState;
            int      bytes = 0;

            if (obj.client.Connected)
            {
                try
                {
                    bytes = obj.stream.EndRead(result);
                }
                catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
            }
            if (bytes > 0)
            {
                // obj.data.AppendFormat("{0}", Encoding.UTF8.GetString(obj.buffer, 0, bytes));
                bool dataAvailable = false;
                try
                {
                    dataAvailable = obj.stream.DataAvailable;
                }
                catch (IOException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                catch (ObjectDisposedException e) { LogWrite(String.Format("[/ {0} /]", e.Message)); }
                if (dataAvailable)
                {
                    try
                    {
                        obj.stream.BeginRead(obj.buffer, 0, obj.buffer.Length, new AsyncCallback(Read), obj);
                    }
                    catch (IOException e)
                    {
                        LogWrite(String.Format("[/ {0} /]", e.Message));
                        obj.handle.Set();
                    }
                    catch (ObjectDisposedException e)
                    {
                        LogWrite(String.Format("[/ {0} /]", e.Message));
                        obj.handle.Set();
                    }
                }
                else
                {
                    IFormatter      formatter = new BinaryFormatter();
                    MemoryStream    ms        = new MemoryStream(obj.buffer);
                    chatLib.Message msg       = (chatLib.Message)formatter.Deserialize(ms);

                    switch (msg.Head)
                    {
                    case chatLib.Message.Header.POST:
                        LogWrite(String.Format("{0} -> {1} ", msg.MessageList[0], msg.MessageList[1]));
                        break;

                    case chatLib.Message.Header.FILE:
                        LogWrite(String.Format("{0} -> {1} Adında Dosya Gönderdi ", msg.MessageList[0], msg.MessageList[1]));
                        break;

                    case chatLib.Message.Header.REGISTER:
                        LogWrite(String.Format("{0} -> Kayıt Oldu ", msg.MessageList[0]));
                        break;

                    case chatLib.Message.Header.JOIN:
                        LogWrite(String.Format("{0} -> Giriş Yaptı ", msg.MessageList[0]));

                        break;
                    }


                    //String msg = String.Format("<- Client {0} -> " + obj.data, obj.id);
                    //LogWrite(msg);
                    if (send == null || send.IsCompleted)
                    {
                        switch (msg.Head)
                        {
                        case chatLib.Message.Header.REGISTER:
                            chatLib.Message message = new chatLib.Message(chatLib.Message.Header.REGISTER);
                            if (users.ContainsKey(msg.MessageList[0]))
                            {
                                message.addData("error");
                            }
                            else
                            {
                                users.Add(msg.MessageList[0], MD5Sifrele(msg.MessageList[1]));
                                message.addData("success");

                                string text = "";
                                foreach (DictionaryEntry item in users)
                                {
                                    text += item.Key.ToString() + "|" + item.Value.ToString() + ";";
                                }

                                text = text.Trim(';');

                                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "users.txt", text);
                            }

                            send = Task.Factory.StartNew(() => SendOne(message, obj.id));
                            break;

                        case chatLib.Message.Header.JOIN:
                            chatLib.Message message1 = new chatLib.Message(chatLib.Message.Header.JOIN);
                            if (users.ContainsKey(msg.MessageList[0]))
                            {
                                if (users[msg.MessageList[0]].Equals(MD5Sifrele(msg.MessageList[1])))
                                {
                                    message1.addData("success");
                                    message1.addData(msg.MessageList[0]);
                                }
                                message1.addData("error");
                            }
                            else
                            {
                                message1.addData("error");
                            }

                            send = Task.Factory.StartNew(() => SendOne(message1, obj.id));
                            break;

                        case chatLib.Message.Header.POST:
                            send = Task.Factory.StartNew(() => Send(msg, obj.id));
                            break;

                        case chatLib.Message.Header.FILE:
                            send = Task.Factory.StartNew(() => Send(msg, obj.id));
                            break;
                        }
                    }
                    else
                    {
                        switch (msg.Head)
                        {
                        case chatLib.Message.Header.REGISTER:
                            chatLib.Message message = new chatLib.Message(chatLib.Message.Header.REGISTER);
                            if (users.ContainsKey(msg.MessageList[0]))
                            {
                                message.addData("error");
                            }
                            else
                            {
                                users.Add(msg.MessageList[0], MD5Sifrele(msg.MessageList[1]));
                                message.addData("success");

                                string text = "";
                                foreach (DictionaryEntry item in users)
                                {
                                    text += item.Key.ToString() + "|" + item.Value.ToString() + ";";
                                }

                                text = text.Trim(';');

                                File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "users.txt", text);
                            }
                            send.ContinueWith(antecendent => SendOne(message, obj.id));
                            break;

                        case chatLib.Message.Header.JOIN:
                            chatLib.Message message1 = new chatLib.Message(chatLib.Message.Header.JOIN);
                            if (users.ContainsKey(msg.MessageList[0]))
                            {
                                if (users[msg.MessageList[0]].Equals(MD5Sifrele(msg.MessageList[1])))
                                {
                                    message1.addData("success");
                                    message1.addData(msg.MessageList[0]);
                                }
                                message1.addData("error");
                            }
                            else
                            {
                                message1.addData("error");
                            }

                            send.ContinueWith(antecendent => SendOne(message1, obj.id));
                            break;

                        case chatLib.Message.Header.POST:
                            send.ContinueWith(antecendent => Send(msg, obj.id));
                            break;

                        case chatLib.Message.Header.FILE:
                            send.ContinueWith(antecendent => Send(msg, obj.id));
                            break;
                        }
                    }
                    //obj.data.Clear();
                    obj.handle.Set();
                }
            }
            else
            {
                obj.client.Close();
                obj.handle.Set();
            }
        }