Пример #1
0
        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();
                }
            }
        }
Пример #2
0
        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);
            }
        }
Пример #3
0
        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);
                }
            }
        }