public static bool Send(Packet p) { if ((m_Server == null) || (p == null)) { return(false); } byte[] buffer = p.Compile(); if (OnSend != null) { OnSend(buffer.Length); } if (p.Encode) { m_CryptoProvider.Encrypt(buffer, 0, buffer.Length); } try { if (buffer.Length > (m_SendBuffer.Length - m_SendLength)) { if (m_SendLength > 0) { m_Server.Send(m_SendBuffer, 0, m_SendLength, SocketFlags.None); } m_SendLength = 0; if (buffer.Length >= m_SendBuffer.Length) { m_Server.Send(buffer, 0, buffer.Length, SocketFlags.None); } else { Buffer.BlockCopy(buffer, 0, m_SendBuffer, 0, buffer.Length); m_SendLength += buffer.Length; } } else { Buffer.BlockCopy(buffer, 0, m_SendBuffer, m_SendLength, buffer.Length); m_SendLength += buffer.Length; } m_LastNetworkActivity.Reset(); } catch (SocketException exception) { Debug.Trace("SocketException caught in Network.Send()"); Debug.Trace("Error Code: 0x{0:X}", exception.ErrorCode); Debug.Trace("Native Error Code: 0x{0:X}", exception.NativeErrorCode); Debug.Error(exception); Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); Disconnect(); Cursor.Hourglass = false; m_SoftDisconnect = false; Engine.amMoving = false; p.Dispose(); return(false); } p.Dispose(); return(true); }
public static int Read(byte[] buffer, int offset) { int num; if (m_UnpackBuffer == null) { m_UnpackBuffer = new byte[0x800]; } try { num = m_Server.Receive(m_UnpackBuffer, 0, 0x800, SocketFlags.None); } catch (SocketException exception) { Debug.Trace("SocketException caught in Network.Read()"); Debug.Trace("Error Code: 0x{0:X}", exception.ErrorCode); Debug.Trace("Native Error Code: 0x{0:X}", exception.NativeErrorCode); Debug.Error(exception); Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); Disconnect(); Cursor.Hourglass = false; m_SoftDisconnect = false; Engine.amMoving = false; return(-1); } if (num <= 0) { if (num == 0) { Debug.Trace("Soft disconnect caught in Network.Read()"); } else { Debug.Trace("Hard disconnect caught in Network.Read()"); Debug.Trace("Return value: {0} (0x{0:X})", num); } Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); Disconnect(); Cursor.Hourglass = false; m_SoftDisconnect = false; Engine.amMoving = false; return(-1); } return(m_CryptoProvider.Decrypt(m_UnpackBuffer, 0, num, buffer, offset)); }
public static bool Connect(BaseCrypto cryptoProvider, IPEndPoint ipEndPoint) { if (cryptoProvider == null) { throw new ArgumentNullException("cryptoProvider"); } if (ipEndPoint == null) { throw new ArgumentNullException("ipEndPoint"); } Network.Disconnect(); try { Socket socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Debug, true); } catch (Exception ex) { Debug.Trace("SetSocketOption failed."); Debug.Error(ex); } Network.ProcessAsyncConnect(socket, socket.BeginConnect((EndPoint)ipEndPoint, (AsyncCallback)null, (object)null)); Network._networkContext = new NetworkContext(socket, cryptoProvider); Network._networkContext.ConnectionLostCallback = (Callback)(() => { Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); Network._networkContext = (NetworkContext)null; Cursor.Hourglass = false; Engine.amMoving = false; }); return(true); } catch (SocketException ex) { return(false); } }
public static void Flush() { try { if ((m_Server != null) && (m_SendLength > 0)) { m_Server.Send(m_SendBuffer, 0, m_SendLength, SocketFlags.None); } } catch (SocketException exception) { Debug.Trace("SocketException caught in Network.Flush()"); Debug.Trace("Error Code: 0x{0:X}", exception.ErrorCode); Debug.Trace("Native Error Code: 0x{0:X}", exception.NativeErrorCode); Debug.Error(exception); Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); Disconnect(false); Cursor.Hourglass = false; m_SoftDisconnect = false; Engine.amMoving = false; } m_SendLength = 0; }
public static unsafe bool Slice() { if (m_Server == null) { Thread.Sleep(1); return(true); } if ((m_LastNetworkActivity.Elapsed && m_Server.Poll(1, SelectMode.SelectRead)) && (m_Server.Available == 0)) { Debug.Trace("Disconnected"); if (!m_SoftDisconnect) { Gumps.MessageBoxOk("Connection lost", true, new OnClick(Engine.DestroyDialogShowAcctLogin_OnClick)); } Disconnect(); Cursor.Hourglass = false; m_SoftDisconnect = false; Engine.amMoving = false; return(true); } if (m_Server.Available > 0) { if (m_Buffer == null) { m_Buffer = new byte[0x10000]; } int num = Read(m_Buffer, m_CurrFilled); if (num == -1) { return(true); } m_CurrFilled += num; int currFilled = m_CurrFilled; int index = 0; while (index < currFilled) { byte num4 = m_Buffer[index]; PacketHandler ph = PacketHandlers.m_Handlers[num4]; if (ph == null) { PacketReader.Initialize(m_Buffer, index, m_CurrFilled - index, true, num4, "Unknown").Trace(); m_CurrFilled = 0; return(true); } int length = ph.Length; if (length == -1) { if ((currFilled - index) < 3) { break; } length = m_Buffer[index + 2] | (m_Buffer[index + 1] << 8); } if ((currFilled - index) < length) { break; } if (OnRecv != null) { OnRecv(length); } PacketReader pvSrc = PacketReader.Initialize(m_Buffer, index, length, ph.Length != -1, num4, ph.Name); ph.Handle(pvSrc); m_LastNetworkActivity.Reset(); if (OnPacketHandle != null) { OnPacketHandle(ph); } index += length; if (m_CurrFilled != currFilled) { return(true); } } m_CurrFilled -= index; if (m_CurrFilled > 0) { fixed(byte *numRef = m_Buffer) { byte *numPtr = numRef; byte *numPtr2 = numRef + index; byte *numPtr3 = numPtr + m_CurrFilled; while (numPtr < numPtr3) { *(numPtr++) = *(numPtr2++); } } } } return(true); }