예제 #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);
                }
            }
        }
예제 #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);
            }
        }
예제 #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;
            }
        }
예제 #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++;
        }
예제 #5
0
 public void Send(SoketinUser client, SoketinData data)
 {
     Send(client, data.GetBytes());
 }
예제 #6
0
 public virtual void OnDataRecieved(SoketinUser user, byte[] data)
 {
     onDataRecieved?.Invoke(user, data);
 }
예제 #7
0
 public virtual void OnClientDisconnected(SoketinUser user)
 {
     onClientDisconnected?.Invoke(user);
 }