TizzyT

EasyTCP -TizzyT

Aug 1st, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6.  
  7. namespace EasyTCP
  8. {
  9.     public class TCP : IDisposable
  10.     {
  11.         public event BytesReceivedEventHandler BytesReceived;
  12.         public delegate void BytesReceivedEventHandler(byte[] Bytes);
  13.         public event ExceptionOccurredEventHandler ExceptionOccurred;
  14.         public delegate void ExceptionOccurredEventHandler(Exception ex);
  15.         public event SuccessfullySentEventHandler SuccessfullySent;
  16.         public delegate void SuccessfullySentEventHandler(ulong SendID);
  17.  
  18.         private int Port;
  19.         private string Address;
  20.         private ulong SentCount = 0;
  21.         private Socket Connection;
  22.         private Thread SendThread;
  23.         private Thread RecvThread;
  24.         private Mutex ReadMutex = new Mutex(false);
  25.         private ConcurrentQueue<SendMeta> SendQueue;
  26.         private EventWaitHandle SendWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);
  27.  
  28.         public Socket Socket
  29.         {
  30.             get { return Connection; }
  31.         }
  32.  
  33.         public TCP(string Address, int Port)
  34.         {
  35.             this.Address = Address;
  36.             this.Port = Port;
  37.         }
  38.  
  39.         public void Close()
  40.         {
  41.             try
  42.             {
  43.                 RecvThread.Abort();
  44.             }
  45.             catch (Exception) { }
  46.             try
  47.             {
  48.                 SendThread.Abort();
  49.             }
  50.             catch (Exception) { }
  51.             try
  52.             {
  53.                 Connection.Disconnect(false);
  54.             }
  55.             catch (Exception) { }
  56.             try
  57.             {
  58.                 Connection.Close();
  59.             }
  60.             catch (Exception) { }
  61.             try
  62.             {
  63.                 Connection.Dispose();
  64.             }
  65.             catch (Exception) { }
  66.         }
  67.  
  68.         public bool TryConnect(byte[][] InitialSend)
  69.         {
  70.             if (Connection == null || !Connection.Connected)
  71.             {
  72.                 try
  73.                 {
  74.                     Connection = new Socket(SocketType.Stream, ProtocolType.Tcp);
  75.                     Connection.Connect(Address, Port);
  76.                     if (RecvThread != null) RecvThread.Abort();
  77.                     if (SendThread != null) SendThread.Abort();
  78.                     SendQueue = new ConcurrentQueue<SendMeta>();
  79.                     RecvThread = new Thread(() =>
  80.                     {
  81.                         while (Connection.Connected)
  82.                         {
  83.                             try
  84.                             {
  85.                                 List<byte> Data = new List<byte>();
  86.                                 do
  87.                                 {
  88.                                     byte[] Buffer = new byte[512];
  89.                                     Array.Resize(ref Buffer, Connection.Receive(Buffer, Buffer.Length, SocketFlags.None));
  90.                                     Data.AddRange(Buffer);
  91.                                 } while (Connection.Available > 0);
  92.                                 BytesReceived(Data.ToArray());
  93.                             }
  94.                             catch (Exception ex)
  95.                             {
  96.                                 ExceptionOccurred(ex);
  97.                             }
  98.                         }
  99.                     })
  100.                     { IsBackground = true };
  101.                     SendThread = new Thread(() =>
  102.                     {
  103.                         while (Connection.Connected)
  104.                         {
  105.                             SendWaitHandle.WaitOne();
  106.                             while (SendQueue.Count > 0 && Connection.Connected)
  107.                             {
  108.                                 SendMeta Bytes;
  109.                                 if (SendQueue.TryDequeue(out Bytes))
  110.                                 {
  111.                                     try
  112.                                     {
  113.                                         Connection.Send(Bytes.Bytes, Bytes.Bytes.Length, Bytes.Flag);
  114.                                         SuccessfullySent(Bytes.SendID);
  115.                                     }
  116.                                     catch (Exception ex)
  117.                                     {
  118.                                         ExceptionOccurred(ex);
  119.                                     }
  120.                                 }
  121.                             }
  122.                         }
  123.                     })
  124.                     { IsBackground = true };
  125.                     RecvThread.Start();
  126.                     SendThread.Start();
  127.                 }
  128.                 catch (Exception ex)
  129.                 {
  130.                     ExceptionOccurred(ex);
  131.                 }
  132.             }
  133.             return false;
  134.         }
  135.  
  136.         private struct SendMeta
  137.         {
  138.             public readonly ulong SendID;
  139.             public readonly byte[] Bytes;
  140.             public readonly SocketFlags Flag;
  141.             public SendMeta(ulong SendID, byte[] Bytes, SocketFlags Flag)
  142.             {
  143.                 this.SendID = SendID;
  144.                 this.Bytes = Bytes;
  145.                 this.Flag = Flag;
  146.             }
  147.         }
  148.  
  149.         public ulong Send(byte[] Bytes, SocketFlags Flag = SocketFlags.None)
  150.         {
  151.             try
  152.             {
  153.                 ReadMutex.WaitOne();
  154.             }
  155.             catch (Exception) { }
  156.             SendMeta newSendMeta = new SendMeta(SentCount, Bytes, Flag);
  157.             SentCount++;
  158.             SendQueue.Enqueue(newSendMeta);
  159.             ReadMutex.ReleaseMutex();
  160.             SendWaitHandle.Set();
  161.             return newSendMeta.SendID;
  162.         }
  163.  
  164.         public void Dispose()
  165.         {
  166.             Close();
  167.             Connection.Close();
  168.             Connection.Dispose();
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment