|
| 1 | +#region License |
| 2 | +// ==================================================== |
| 3 | +// AsyncServerClient Copyright(C) 2015-2019 Furkan Türkal |
| 4 | +// This program comes with ABSOLUTELY NO WARRANTY; This is free software, |
| 5 | +// and you are welcome to redistribute it under certain conditions; See |
| 6 | +// file LICENSE, which is part of this source code package, for details. |
| 7 | +// ==================================================== |
| 8 | +#endregion |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Net; |
| 13 | +using System.Net.Sockets; |
| 14 | +using System.Threading; |
| 15 | +using ServerFramework; |
| 16 | + |
| 17 | +namespace ClientConsole |
| 18 | +{ |
| 19 | + sealed public class PacketProtocol |
| 20 | + { |
| 21 | + |
| 22 | + private Socket m_socket; |
| 23 | + |
| 24 | + private Security m_localSecurity = null; |
| 25 | + private TransferBuffer m_localTransferBuffer = null; |
| 26 | + private List<Packet> m_recvPackets = null; |
| 27 | + private List<KeyValuePair<TransferBuffer, Packet>> m_sendBuffer = null; |
| 28 | + public Thread m_Updater; |
| 29 | + |
| 30 | + private object m_Locker = null; |
| 31 | + |
| 32 | + public bool m_shouldExit = false; |
| 33 | + |
| 34 | + private int m_Buffer; |
| 35 | + |
| 36 | + private string m_ClientName = null; |
| 37 | + |
| 38 | + public delegate void OnPacketRecivedEventHandler(Packet packet); |
| 39 | + public event OnPacketRecivedEventHandler OnCommandReceived = null; |
| 40 | + |
| 41 | + |
| 42 | + public Socket GetSocket() { return this.m_socket; } |
| 43 | + |
| 44 | + public PacketProtocol() |
| 45 | + { |
| 46 | + m_Locker = new object(); |
| 47 | + |
| 48 | + m_Buffer = 8192; |
| 49 | + |
| 50 | + m_ClientName = "AsyncServerClient"; |
| 51 | + |
| 52 | + m_localSecurity = new Security(); |
| 53 | + m_localSecurity.ChangeIdentity(m_ClientName, 0); |
| 54 | + |
| 55 | + m_localTransferBuffer = new TransferBuffer(m_Buffer); |
| 56 | + } |
| 57 | + |
| 58 | + public void Connect(IPEndPoint endPoint) { |
| 59 | + m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
| 60 | + |
| 61 | + try { |
| 62 | + m_socket.Connect(endPoint); |
| 63 | + |
| 64 | + Console.WriteLine("[PacketProtocol::Connect()]: Connected to {0}", m_socket.RemoteEndPoint.ToString()); |
| 65 | + |
| 66 | + if (m_socket.Connected) { |
| 67 | + m_socket.BeginReceive(m_localTransferBuffer.Buffer, m_localTransferBuffer.Offset, m_Buffer, SocketFlags.None, new AsyncCallback(WaitForData), m_socket); |
| 68 | + } |
| 69 | + |
| 70 | + } catch (Exception ex) { |
| 71 | + m_shouldExit = true; |
| 72 | + Console.WriteLine("[PacketProtocol::Connect()]: {0}", ex.Message); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void SendPacket(Packet pck) |
| 77 | + { |
| 78 | + try |
| 79 | + { |
| 80 | + m_localSecurity.Send(pck); |
| 81 | + } |
| 82 | + catch (Exception ex) |
| 83 | + { |
| 84 | + Console.WriteLine("[PacketProtocol::Send()]: Exception: {0}", ex.Message); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void Update() |
| 89 | + { |
| 90 | + while (!m_shouldExit) |
| 91 | + { |
| 92 | + try |
| 93 | + { |
| 94 | + ProcessIncoming(); |
| 95 | + ProcessOutgoing(); |
| 96 | + } |
| 97 | + catch |
| 98 | + { |
| 99 | + Disponse(); |
| 100 | + } |
| 101 | + Thread.Sleep(1); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void DoRecvFromClient() |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + m_socket.BeginReceive(m_localTransferBuffer.Buffer, m_localTransferBuffer.Offset, m_Buffer, SocketFlags.None, new AsyncCallback(WaitForData), m_socket); |
| 110 | + } |
| 111 | + catch |
| 112 | + { |
| 113 | + Disponse(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void WaitForData(IAsyncResult i_AR) |
| 118 | + { |
| 119 | + lock (m_Locker) |
| 120 | + { |
| 121 | + if (m_shouldExit) |
| 122 | + { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + try |
| 128 | + { |
| 129 | + var socket = i_AR.AsyncState as Socket; |
| 130 | + m_localTransferBuffer.Size = socket.EndReceive(i_AR); |
| 131 | + |
| 132 | + if (m_localTransferBuffer.Size > 0) |
| 133 | + { |
| 134 | + m_localSecurity.Recv(m_localTransferBuffer); |
| 135 | + socket.BeginReceive(m_localTransferBuffer.Buffer, m_localTransferBuffer.Offset, m_Buffer, SocketFlags.None, new AsyncCallback(WaitForData), socket); |
| 136 | + } |
| 137 | + else |
| 138 | + { |
| 139 | + Console.WriteLine("[PacketProtocol::WaitForData()]: Warning: 0 Bytes recived!"); |
| 140 | + Thread.Sleep(1); |
| 141 | + Disponse(); |
| 142 | + } |
| 143 | + } |
| 144 | + catch (SocketException sex) |
| 145 | + { |
| 146 | + if (sex.SocketErrorCode == SocketError.ConnectionReset) |
| 147 | + { |
| 148 | + Disponse(); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + Console.WriteLine("[PacketProtocol::WaitForData()]: SocketException: {0}", sex.Message); |
| 153 | + } |
| 154 | + } |
| 155 | + catch (Exception ex) |
| 156 | + { |
| 157 | + Console.WriteLine("[PacketProtocol::WaitForData()]: Exception: {0}", ex.Message); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void ProcessIncoming() |
| 162 | + { |
| 163 | + //if(!disposed) |
| 164 | + if (m_socket == null) |
| 165 | + { |
| 166 | + Console.WriteLine("m_ClientSocket == null"); |
| 167 | + return; |
| 168 | + } |
| 169 | + m_recvPackets = m_localSecurity.TransferIncoming(); |
| 170 | + if (m_recvPackets != null) |
| 171 | + { |
| 172 | + foreach (var packet in m_recvPackets) |
| 173 | + { |
| 174 | + PacketToConsole(packet, Process.Incoming); |
| 175 | + |
| 176 | + if (packet.Opcode == 0x5000 || packet.Opcode == 0x9000 || packet.Opcode == 0x2002) |
| 177 | + { |
| 178 | + continue; |
| 179 | + } |
| 180 | + |
| 181 | + OnCommandReceived?.Invoke(packet); |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private void ProcessOutgoing() |
| 187 | + { |
| 188 | + if (m_socket == null) |
| 189 | + { |
| 190 | + Disponse(); |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + m_sendBuffer = m_localSecurity.TransferOutgoing(); |
| 195 | + if (m_sendBuffer != null) |
| 196 | + { |
| 197 | + foreach (var kvp in m_sendBuffer) |
| 198 | + { |
| 199 | + Packet packet = kvp.Value; |
| 200 | + PacketToConsole(packet, Process.Outgoing); |
| 201 | + m_socket.Send(kvp.Key.Buffer); |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private void Disponse() |
| 207 | + { |
| 208 | + try |
| 209 | + { |
| 210 | + m_shouldExit = true; |
| 211 | + |
| 212 | + if (m_socket != null) |
| 213 | + { |
| 214 | + m_socket.Close(); |
| 215 | + } |
| 216 | + |
| 217 | + m_Updater.Abort(); |
| 218 | + |
| 219 | + m_socket = null; |
| 220 | + } |
| 221 | + catch (Exception e_Ex) |
| 222 | + { |
| 223 | + Console.WriteLine("[PacketProtocol::Disponse()] -> Disponse() error. : " + e_Ex.Message); |
| 224 | + } |
| 225 | + |
| 226 | + } |
| 227 | + |
| 228 | + private enum Process { |
| 229 | + Incoming, |
| 230 | + Outgoing |
| 231 | + }; |
| 232 | + |
| 233 | + private void PacketToConsole(Packet packet, Process process) { |
| 234 | + var buffer = packet.GetBytes(); |
| 235 | + |
| 236 | + if (process == Process.Incoming) { |
| 237 | + Console.ForegroundColor = ConsoleColor.Green; |
| 238 | + Console.Write("[C->S]"); |
| 239 | + } else { |
| 240 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 241 | + Console.Write("[S->C]"); |
| 242 | + } |
| 243 | + Console.ForegroundColor = ConsoleColor.Blue; |
| 244 | + Console.Write("[{0:X4}]", packet.Opcode); |
| 245 | + Console.ForegroundColor = ConsoleColor.DarkCyan; |
| 246 | + Console.Write("[{0} bytes]", buffer.Length); |
| 247 | + Console.ForegroundColor = ConsoleColor.Red; |
| 248 | + Console.Write(packet.Encrypted ? "[Encrypted]" : ""); |
| 249 | + Console.ForegroundColor = ConsoleColor.DarkRed; |
| 250 | + Console.Write(packet.Massive ? "[Massive]" : ""); |
| 251 | + Console.WriteLine(); |
| 252 | + Console.ForegroundColor = ConsoleColor.White; |
| 253 | + Console.Write(Utility.HexDump(buffer)); |
| 254 | + Console.WriteLine(); |
| 255 | + Console.ResetColor(); |
| 256 | + } |
| 257 | + |
| 258 | + public static void Log(string msg, params object[] values) |
| 259 | + { |
| 260 | + msg = string.Format(msg, values); |
| 261 | + Console.WriteLine(msg + "\t"); |
| 262 | + } |
| 263 | + |
| 264 | + } |
| 265 | +} |
0 commit comments