private void _onBeginReceive(IAsyncResult ar) { var client = (Socket)ar.AsyncState; var user = m_clients.Find((e) => { return(e._socket == client); }); try { int byteReaded = client.EndReceive(ar); if (byteReaded > 0) { var packs = SoketinUtility.SplitRawPacket(m_buffer, byteReaded); foreach (var pack in packs) { _execute((Action <SoketinUser, byte[]>)onEvent.OnDataRecieved, client, pack); } } if (!m_signalStop) { client.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginReceive), client); } } catch (Exception e) { _execute((Action <Exception, object>)onEvent.OnError, e, user); client.Close(); m_clients.Remove(user); Console.WriteLine("Client Disconnected"); _execute((Action <SoketinUser>)onEvent.OnClientDisconnected, user); } }
private void _onBeginRecieve(IAsyncResult ar) { if (m_stopSignal) { return; } var socket = (Socket)ar.AsyncState; try { int byteReaded = socket.EndReceive(ar); if (byteReaded > 0) { var packs = SoketinUtility.SplitRawPacket(m_buffer, byteReaded); foreach (var pack in packs) { _execute((Action <SoketinUser, byte[]>)onEvent.OnDataRecieved, server, pack); } } m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginRecieve), m_socket); } catch (Exception e) { _execute((Action <Exception, object>)onEvent.OnError, e, null); if (autoReconnect) { socket.BeginDisconnect(true, new AsyncCallback(_onReconnect), socket); } else { Disconnect(); } } }
//Read Function #region Read Function public string ReadString(bool isCompressed = false) { //Read 4 byte var len = ReadInt(); var buffer = new byte[len]; m_stream.Read(buffer, 0, len); return(isCompressed ? SoketinUtility.UnzipString(buffer) : SoketinUtility.BytesToString(buffer)); }
public void Broadcast(byte[] data) { var packedData = SoketinUtility.PackRawData(data); foreach (var client in m_clients) { Send(client, packedData); } }
public void Send(byte[] data) { if (m_stopSignal) { return; } var packedData = SoketinUtility.PackRawData(data); m_socket.BeginSend(packedData, 0, packedData.Length, 0, new AsyncCallback(_onBeginSend), m_socket); }
//Write Function #region Write Function public void WriteString(string value, bool compressed = false) { if (string.IsNullOrEmpty(value)) { throw new ArgumentNullException("Value Cannot Be Null"); } byte[] strData = compressed ? SoketinUtility.ZipString(value) : SoketinUtility.StringToBytes(value); byte[] packedData = SoketinUtility.PackRawData(strData); m_stream.Write(packedData, 0, packedData.Length); }
public void Send(SoketinUser client, byte[] data) { var user = m_clients.Find((c) => { return(c._socket == client._socket); }); if (user != null) { var packedData = SoketinUtility.PackRawData(data); client._socket.BeginSend(packedData, 0, packedData.Length, 0, new AsyncCallback(_onBeginSend), client); } }
public void Send(byte[] data, params SoketinUser[] addressess) { if (addressess != null && addressess.Length > 0) { var packedFile = SoketinUtility.PackRawData(data); foreach (var address in addressess) { var endPoint = new IPEndPoint(IPAddress.Parse(address._ipAddress), address._port); m_socket.BeginSendTo(packedFile, 0, packedFile.Length, 0, endPoint, new AsyncCallback(_onBeginSend), address); } } }
private void _onBeginRecieve(IAsyncResult ar) { var socket = (Socket)ar.AsyncState; try { var readedBytes = socket.EndReceiveFrom(ar, ref m_endPoint); var splitted = SoketinUtility.SplitRawPacket(m_buffer, readedBytes); foreach (var data in splitted) { _execute((Action <byte[]>)onEvent.OnDataRecieved, data); } } catch (Exception e) { Console.WriteLine(e); } finally { if (!m_signalStop) { socket.BeginReceiveFrom(m_buffer, 0, m_buffer.Length, 0, ref m_endPoint, new AsyncCallback(_onBeginRecieve), socket); } } }