Exemplo n.º 1
0
        private void _onBeginConnect(IAsyncResult ar)
        {
            if (m_stopSignal)
            {
                return;
            }
            var socket = (Socket)ar.AsyncState;

            try {
                socket.EndConnect(ar);
                server = new SoketinUser()
                {
                    _ipAddress = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString(),
                    _port      = ((IPEndPoint)socket.RemoteEndPoint).Port,
                    _socket    = socket,
                };
                _execute((Action)onEvent.OnServerConnected);
                if (!m_stopSignal)
                {
                    m_socket.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginRecieve), m_socket);
                }
            }
            catch (Exception e) {
                Console.WriteLine(e);
                _execute((Action <Exception, object>)onEvent.OnError, e, null);
                if (autoReconnect)
                {
                    socket.BeginConnect(m_ip, m_port, new AsyncCallback(_onBeginConnect), socket);
                }
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        private void _onBeginDisconnect(IAsyncResult ar)
        {
            var socket = (Socket)ar.AsyncState;

            try
            {
                socket.EndDisconnect(ar);
                _execute((Action)onEvent.OnServerDisconnected);
                socket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception e)
            {
                Console.WriteLine();
                _execute((Action <Exception, object>)onEvent.OnError, e, null);
            }
            finally {
                socket.Close();
                _execute((Action)onEvent.OnServiceStop);
                server = null;
            }
        }
Exemplo n.º 4
0
        private void _onBeginAccept(IAsyncResult ar)
        {
            m_signalAccept.Set();
            if (m_signalStop)
            {
                return;
            }
            var client    = ((Socket)ar.AsyncState).EndAccept(ar);
            var newClient = new SoketinUser()
            {
                _id        = m_userID,
                _ipAddress = ((IPEndPoint)client.RemoteEndPoint).Address.ToString(),
                _port      = ((IPEndPoint)client.RemoteEndPoint).Port,
                _socket    = client,
            };

            m_clients.Add(newClient);
            client.BeginReceive(m_buffer, 0, m_buffer.Length, 0, new AsyncCallback(_onBeginReceive), client);
            _execute((Action <SoketinUser>)onEvent.OnClientConnected, newClient);
            m_userID++;
        }
Exemplo n.º 5
0
 public void Send(SoketinUser client, SoketinData data)
 {
     Send(client, data.GetBytes());
 }
Exemplo n.º 6
0
 public virtual void OnDataRecieved(SoketinUser user, byte[] data)
 {
     onDataRecieved?.Invoke(user, data);
 }
Exemplo n.º 7
0
 public virtual void OnClientDisconnected(SoketinUser user)
 {
     onClientDisconnected?.Invoke(user);
 }