コード例 #1
0
        static NetworkProtocol()
        {
            _messageTypes   = new Dictionary <ushort, Type>();
            _messageHookers = new Dictionary <Type, NetworkMessageCaller>();

            var assemblys = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblys)
            {
                var types = assembly.GetTypes();
                foreach (var type in types)
                {
                    if (!type.IsClass || type.IsAbstract)
                    {
                        continue;
                    }

                    if (type.IsSubclassOf(typeof(NetworkMessage)))
                    {
                        var message = CreateMessage(type);

                        if (!_messageTypes.ContainsKey(message.MessageId))
                        {
                            _messageTypes.Add(message.MessageId, type);
                            VerifyReceivers(type);
                        }
                    }
                    else if (type.IsSealed && typeof(IMessageHooker).IsAssignableFrom(type))
                    {
                        VerifyReceivers(type);
                    }
                }
            }

            _current = new DefaultNetworkProtocol();

            void VerifyReceivers(Type type)
            {
                var sMethods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic);

                if (sMethods.Length > 0)
                {
                    foreach (var smethod in sMethods)
                    {
                        var ps = smethod.GetParameters();
                        if (ps.Length >= 2 && ps[0].ParameterType.IsSubclassOf(typeof(NetworkMessage)) && (ps[1].ParameterType.IsSubclassOf(typeof(NetworkClientEntity)) || ps[1].ParameterType.IsSubclassOf(typeof(NetworkConnectionEntity))))
                        {
                            var msgType    = ps[0].ParameterType;
                            var entityType = ps[1].ParameterType;
                            if (!_messageHookers.ContainsKey(entityType))
                            {
                                _messageHookers.Add(entityType, new NetworkMessageCaller());
                            }

                            _messageHookers[entityType].RegisterReference(msgType, smethod);
                        }
                    }
                }
            }
        }
コード例 #2
0
ファイル: UdpClientEntity.cs プロジェクト: Etamiin/Inertia
        private void OnReceiveData(IAsyncResult iar)
        {
            if (IsConnected)
            {
                try
                {
                    IPEndPoint endPoint = null;
                    var        data     = ((UdpClient)iar.AsyncState).EndReceive(iar, ref endPoint);

                    NetworkProtocol.ProcessParsing(this, new BasicReader(data));
                }
                catch (Exception e)
                {
                    if (e is SocketException || e is ObjectDisposedException)
                    {
                        Disconnect(NetworkDisconnectReason.ConnectionLost);
                        return;
                    }
                }

                if (IsConnected)
                {
                    _client.BeginReceive(new AsyncCallback(OnReceiveData), _client);
                }
            }
        }
コード例 #3
0
        private void OnReceiveData(IAsyncResult iar)
        {
            if (IsConnected)
            {
                try
                {
                    var received = ((Socket)iar.AsyncState).EndReceive(iar);
                    if (received == 0)
                    {
                        throw new SocketException();
                    }

                    var data = new byte[received];
                    Array.Copy(_buffer, data, received);

                    NetworkProtocol.ProcessParsing(this, _reader.Fill(data));
                }
                catch (Exception ex)
                {
                    if (ex is SocketException || ex is ObjectDisposedException)
                    {
                        if (!IsDisposed)
                        {
                            Disconnect(NetworkDisconnectReason.ConnectionLost);
                            return;
                        }
                    }
                }

                if (IsConnected)
                {
                    _socket.BeginReceive(_buffer, 0, _buffer.Length, SocketFlags.None, new AsyncCallback(OnReceiveData), _socket);
                }
            }
        }
コード例 #4
0
ファイル: UdpServerEntity.cs プロジェクト: Etamiin/Inertia
        private void OnReceiveData(IAsyncResult iar)
        {
            try
            {
                IPEndPoint endPoint = null;
                var        data     = ((UdpClient)iar.AsyncState).EndReceive(iar, ref endPoint);

                if (!_connections.ContainsKey(endPoint))
                {
                    var connection = new UdpConnectionEntity((uint)_idProvider.GetId(), this, endPoint);
                    _connections.Add(endPoint, connection);

                    OnConnectionAdded(connection);
                }

                NetworkProtocol.ProcessParsing(_connections[endPoint], new BasicReader(data));
            }
            catch (Exception ex)
            {
                if (ex is SocketException || ex is ObjectDisposedException)
                {
                    return;
                }
            }

            if (IsInitialized)
            {
                _client.BeginReceive(new AsyncCallback(OnReceiveData), _client);
            }
        }
コード例 #5
0
ファイル: UdpServerEntity.cs プロジェクト: Etamiin/Inertia
        public void SendTo(UdpConnectionEntity connection, NetworkMessage message)
        {
            if (connection.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(UdpConnectionEntity));
            }

            SendTo(connection.EndPoint, NetworkProtocol.GetCurrentProtocol().OnSerializeMessage(message));
        }
コード例 #6
0
 public void Send(NetworkMessage message)
 {
     Send(NetworkProtocol.GetCurrentProtocol().OnSerializeMessage(message));
 }
コード例 #7
0
 internal TcpConnectionEntity(Socket socket, uint id) : base(id)
 {
     _socket = socket;
     _buffer = new byte[NetworkProtocol.GetCurrentProtocol().NetworkBufferLength];
     _reader = new BasicReader();
 }
コード例 #8
0
ファイル: TcpClientEntity.cs プロジェクト: Etamiin/Inertia
 protected TcpClientEntity(string ip, int port) : base(ip, port)
 {
     _buffer = new byte[NetworkProtocol.GetCurrentProtocol().NetworkBufferLength];
     _reader = new BasicReader();
 }
コード例 #9
0
 public static void SetProtocol(NetworkProtocol protocol)
 {
     _current = protocol;
 }