Ejemplo n.º 1
0
        /// <summary>
        /// 目标对象
        /// </summary>
        public async Task <MemberValue <valueType> > ValueAsync()
        {
            switch (state)
            {
            case MemberState.Unknown:
                if (Size == 0)
                {
                    return new MemberValue <valueType> {
                               State = state = MemberState.Remote
                    }
                }
                ;
                Server.TcpInternalClient client = ClientPool.Get(Index);
                if (client == null)
                {
                    return new MemberValue <valueType> {
                               State = MemberState.NoClient
                    }
                }
                ;
                SubBuffer.PoolBufferFull buffer = default(SubBuffer.PoolBufferFull);
                SubBuffer.Pool.GetBuffer(ref buffer, Size);
                try
                {
                    AutoCSer.Net.TcpServer.ReturnValue <ClientBuffer> clientBuffer = await client.readAwaiter(new ClientBuffer { Buffer = new SubArray <byte>(buffer.StartIndex, Size, buffer.Buffer), IsClient = true }, Index);

                    onRead(ref clientBuffer);
                }
                finally { buffer.Free(); }
                break;
            }
            return(new MemberValue <valueType> {
                Value = value, State = state
            });
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
#if DotNetStandard
            Console.WriteLine("WARN : Linux .NET Core not support name EventWaitHandle");
#else
            bool            createdProcessWait;
            EventWaitHandle processWait = new EventWaitHandle(false, EventResetMode.ManualReset, "AutoCSer.TestCase.Nuget", out createdProcessWait);
            if (createdProcessWait)
            {
                using (processWait)
                {
#endif
            Console.WriteLine(@"http://www.AutoCSer.com/TcpServer/InterfaceServer.html
");

            using (Server.TcpInternalServer server = new Server.TcpInternalServer())
            {
                if (server.IsListen)
                {
                    using (Server.TcpInternalClient client = new Server.TcpInternalClient())
                    {
                        Console.WriteLine(client.add(2, 3) == 2 + 3);
                    }
                }
            }
            Console.WriteLine("Over");
            Console.ReadKey();
#if !DotNetStandard
        }
    }
#endif
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 设置远程对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="blockIndex">磁盘块编号</param>
        /// <param name="bufferSize">序列化缓冲区大小</param>
        /// <returns></returns>
        public unsafe bool Set(valueType value, int blockIndex, SubBuffer.Size bufferSize = SubBuffer.Size.Kilobyte4)
        {
            if (value == null)
            {
                SetNull();
                return(true);
            }
            Server.TcpInternalClient client = ClientPool.Get(blockIndex);
            if (client != null)
            {
                BinarySerialize.Serializer serializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer();
                SubBuffer.PoolBufferFull   buffer     = default(SubBuffer.PoolBufferFull);
                SubBuffer.Pool.GetPool(bufferSize).Get(ref buffer);
                try
                {
                    fixed(byte *bufferFixed = buffer.Buffer)
                    {
                        byte *start = bufferFixed + buffer.StartIndex;

                        serializer.SerializeNotNull(value, start, buffer.PoolBuffer.Pool.Size, ClientConfig.BinarySerializeConfig);
                        AutoCSer.Net.TcpServer.ReturnValue <ulong> index;
                        int size = serializer.Stream.ByteSize;

                        if (serializer.Stream.Data.Data == start)
                        {
                            index = client.append(new AppendBuffer {
                                Buffer = new SubArray <byte> {
                                    Array = buffer.Buffer, Start = buffer.StartIndex, Length = size
                                }, Index = size == Size ? Index : 0, BlockIndex = (ushort)blockIndex
                            });
                        }
                        else
                        {
                            index = client.append(new AppendBuffer {
                                Buffer = new SubArray <byte> {
                                    Array = serializer.Stream.GetArray(), Length = size
                                }, Index = size == Size ? Index : 0, BlockIndex = (ushort)blockIndex
                            });
                        }
                        if (index.Type == Net.TcpServer.ReturnType.Success && index.Value != 0)
                        {
                            Index = index.Value;
                            Size  = size;
                            value = Value;
                            state = MemberState.Remote;
                            return(true);
                        }
                    }
                }
                finally
                {
                    buffer.Free();
                    serializer.Free();
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 部署服务客户端
        /// </summary>
        /// <param name="client"></param>
        /// <param name="name"></param>
        /// <param name="attribute"></param>
        internal TcpClient(Client client, string name, AutoCSer.Net.TcpInternalServer.ServerAttribute attribute)
        {
            this.Client = client;
            this.name   = name;
#if NoAutoCSer
            throw new Exception();
#else
            TcpInternalClient = new Server.TcpInternalClient(attribute);
            TcpInternalClient._TcpClient_.OnSetSocket(onClientSocket);
#endif
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 部署服务客户端
        /// </summary>
        /// <param name="client"></param>
        /// <param name="name"></param>
        /// <param name="attribute"></param>
        /// <param name="verifyMethod"></param>
        internal TcpClient(Client client, string name, AutoCSer.Net.TcpInternalServer.ServerAttribute attribute, Func <Server.TcpInternalClient, AutoCSer.Net.TcpInternalServer.ClientSocketSender, bool> verifyMethod)
        {
            this.Client = client;
            this.name   = name;
#if NoAutoCSer
            throw new Exception();
#else
            TcpInternalClient  = new Server.TcpInternalClient(attribute, verifyMethod);
            checkSocketVersion = TcpInternalClient._TcpClient_.CreateCheckSocketVersion(onClientSocket);
#endif
        }
Ejemplo n.º 6
0
        private void checkRegisterClient()
        {
#if NoAutoCSer
            throw new Exception();
#else
            if (registerClient == null)
            {
                registerClient = new Server.TcpInternalClient(TcpInternalServer.ServerAttribute.GetConfig(serviceName, typeof(AutoCSer.Net.TcpRegister.Server)));
            }
#endif
        }
Ejemplo n.º 7
0
        /// <summary>
        /// TCP 注册服务客户端
        /// </summary>
        /// <param name="serviceName">TCP 注册服务服务名称</param>
        private Client(ref HashString serviceName)
        {
#if NoAutoCSer
            throw new Exception();
#else
            this.serviceName   = serviceName;
            servers            = HashSetCreator.CreateAny <IServer>();
            registerClient     = new Server.TcpInternalClient(TcpInternalServer.ServerAttribute.GetConfig(serviceName, typeof(AutoCSer.Net.TcpRegister.Server)));
            logHandle          = onLog;
            checkSocketVersion = registerClient._TcpClient_.CreateCheckSocketVersion(onNewSocket);
#endif
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 关闭 TCP 注册服务客户端
        /// </summary>
        private void close()
        {
#if NoAutoCSer
            throw new Exception();
#else
            readerClient.Dispose();
            if (registerClient != null)
            {
                registerClient.Dispose();
                registerClient = null;
            }
#endif
            clientId.Tick = 0;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// TCP 注册服务客户端
        /// </summary>
        /// <param name="serviceName">TCP 注册服务服务名称</param>
        private Client(string serviceName)
        {
#if NoAutoCSer
            throw new Exception();
#else
            //attribute = AutoCSer.config.pub.LoadConfig(new AutoCSer.code.cSharp.tcpServer(), serviceName);
            //attribute.IsIdentityCommand = true;
            //attribute.TcpRegister = null;
            this.serviceName = serviceName;
            createWait.Set(0);
            registerClient = new Server.TcpInternalClient(TcpInternalServer.ServerAttribute.GetConfig(serviceName, typeof(AutoCSer.Net.TcpRegister.Server)));
            //isNewClientErrorLog = true;
            logHandle          = onLog;
            checkSocketVersion = registerClient._TcpClient_.CreateCheckSocketVersion(onNewSocket);
#endif
        }
Ejemplo n.º 10
0
        static ClientPool()
        {
            ClientConfig config = ConfigLoader.GetUnion(typeof(ClientConfig)).ClientConfig ?? new ClientConfig();

            (clients = new Server.TcpInternalClient[Math.Max(config.Count, 1)])[0] = new Server.TcpInternalClient();
            AutoCSer.Net.TcpInternalServer.ServerAttribute attribute = (AutoCSer.Net.TcpInternalServer.ServerAttribute)clients[0]._TcpClient_.Attribute;
            for (var index = clients.Length; index != 1;)
            {
                Net.TcpInternalServer.ServerAttribute copyAttribute = AutoCSer.MemberCopy.Copyer <Net.TcpInternalServer.ServerAttribute> .MemberwiseClone(attribute);

                copyAttribute.Name = attribute.Name + (--index).toString();
                copyAttribute.Host = null;
                copyAttribute.Port = 0;
                clients[index]     = new Server.TcpInternalClient(copyAttribute);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 设置远程对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="blockIndex">磁盘块编号</param>
        /// <param name="bufferSize">序列化缓冲区大小</param>
        /// <returns></returns>
        public async Task <bool> SetAsync(valueType value, int blockIndex, SubBuffer.Size bufferSize = SubBuffer.Size.Kilobyte4)
        {
            if (value == null)
            {
                SetNull();
                return(true);
            }
            Server.TcpInternalClient client = ClientPool.Get(blockIndex);
            if (client != null)
            {
                BinarySerialize.Serializer serializer = BinarySerialize.Serializer.YieldPool.Default.Pop() ?? new BinarySerialize.Serializer();
                SubBuffer.PoolBufferFull   buffer     = default(SubBuffer.PoolBufferFull);
                SubBuffer.Pool.GetPool(bufferSize).Get(ref buffer);
                try
                {
                    int          size;
                    AppendBuffer appendBuffer = getAppendBuffer(ref value, serializer, ref buffer, out size);
                    appendBuffer.BlockIndex = (ushort)blockIndex;
                    AutoCSer.Net.TcpServer.ReturnValue <ulong> index = await client.appendAwaiter(appendBuffer);

                    if (index.Type == Net.TcpServer.ReturnType.Success && index.Value != 0)
                    {
                        Index = index.Value;
                        Size  = size;
                        value = Value;
                        state = MemberState.Remote;
                        return(true);
                    }
                }
                finally
                {
                    buffer.Free();
                    serializer.Free();
                }
            }
            return(false);
        }