Esempio n. 1
0
 public Task<bool> InstallAsync(string host, int port)
 {
     this.Host = host;
     this.Port = port;          
     RingBuffer = new ZYNetRingBufferPool(MaxBufferLength);
     return  CheckSockConnectAsync();
 }
Esempio n. 2
0
 public RPCUserInfo(SocketAsyncEventArgs asyn) : base(asyn, 1024 * 64)
 {
     QueueScheduler              = new QueuedTaskScheduler();
     RPC_Call                    = new RPC();
     RPC_Call.CallBufferOutSend += RPC_OBJ_CallBufferOutSend;
     Stream = new ZYNetRingBufferPool(1024 * 64);//64K
 }
Esempio n. 3
0
 public RPCUserInfo(SocketAsyncEventArgs asyn, int maxSize)
 {
     RPC_Call = new RPC();
     RPC_Call.CallBufferOutSend += RPC_OBJ_CallBufferOutSend;
     Stream    = new ZYNetRingBufferPool(maxSize);
     this.Asyn = asyn;
 }
Esempio n. 4
0
        public bool Connection(string host, int port)
        {
            if (!IsConnect)
            {
                Stream               = new ZYNetRingBufferPool(1024 * 1024 * 2); //2M
                Client               = new SocketClient();
                Client.BinaryInput  += Client_BinaryInput;
                Client.MessageInput += Client_MessageInput;

                if (Client.Connect(host, port))
                {
                    IsConnect = true;
                    Client.StartRead();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(true);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static void BinaryInputHandler(byte[] data, int offset, int count, SocketAsyncEventArgs socketAsync)
        {
            if (socketAsync.UserToken == null) //如果此SOCKET绑定的对象为NULL
            {
                //注意这里为了 简单 所以就绑定了个 BuffList 类,本来这里应该绑定用户类对象,
                //并在用户类里面建立 初始化 一个 BuffList 类,这样就能通过用户类保存更多的信息了。
                //比如用户名,权限等等


                socketAsync.UserToken = new ZYNetRingBufferPool(409600);
            }

            //BuffList 数据包组合类 如果不想丢数据就用这个类吧
            ZYNetRingBufferPool buff = socketAsync.UserToken as ZYNetRingBufferPool;


            buff.Write(data, offset, count); //呵呵带offset的 可以省一次copy


            byte[] pdata;
            while (buff.Read(out pdata))
            {
                DataOn(pdata, socketAsync);
            }
        }
Esempio n. 6
0
 public UserInfo(SocketAsyncEventArgs asyn)
     : base(asyn.AcceptSocket)
 {
     Stream            = new ZYNetRingBufferPool(1024 * 1024);
     Asyn              = asyn;
     base.BufferLenght = 4096; // <=0 Send All OR for Send value length
 }
Esempio n. 7
0
 public ASyncToken(SocketAsyncEventArgs asynca, CloudServer server, int MaxBuffsize)
 {
     this.Asyn      = asynca;
     Stream         = new ZYNetRingBufferPool(MaxBuffsize);
     CurrentServer  = server;
     this.dataExtra = server.EcodeingHandler;
     sendobj        = new AsyncSend(asynca.AcceptSocket);
 }
Esempio n. 8
0
 public RPCUserInfo(SocketAsyncEventArgs asyn, int maxSize, bool isCallReturn)
 {
     Scheduler = new QueuedTaskScheduler(4);
     RPC_Call  = new RPC(isCallReturn);
     RPC_Call.CallBufferOutSend += SendData;
     Stream    = new ZYNetRingBufferPool(maxSize);
     this.Asyn = asyn;
 }
Esempio n. 9
0
 static SocketManager()
 {
     //初始化数据包缓冲区,并设置了最大数据包尽可能的大
     BuffListManger        = new ZYNetRingBufferPool(400000);
     client                = new SocketClient();
     client.DataOn        += new DataOn(client_DataOn);
     client.Disconnection += new ExceptionDisconnection(client_Disconnection);
 }
Esempio n. 10
0
        /*
         * public void CallAsyn<Mode>(Expression<Action<Mode>> action)
         * {
         *  RPC_Call.CallAsyn<Mode>(action);
         * }
         * public void CallAsyn<Mode, Result>(Expression<Func<Mode, Result>> action, Action<AsynReturn> Callback)
         * {
         *   RPC_Call.CallAsyn<Mode, Result>(action, Callback);
         * }
         *
         * public void CallAsyn<Mode>(Expression<Action<Mode>> action, Action<AsynReturn> Callback)
         * {
         *  RPC_Call.CallAsyn<Mode>(action, Callback);
         * }
         *
         * public Result Call<Mode, Result>(Expression<Func<Mode, Result>> action)
         * {
         *  return RPC_Call.Call<Mode, Result>(action);
         * }
         *
         * public void Call<Mode>(Expression<Action<Mode>> action)
         * {
         *  RPC_Call.Call<Mode>(action);
         * }
         */
        #endregion


        public RPCUserInfo(SocketAsyncEventArgs asyn)
        {
            Scheduler = new QueuedTaskScheduler(4);
            RPC_Call  = new RPC();
            RPC_Call.CallBufferOutSend += RPC_OBJ_CallBufferOutSend;
            this.Asyn = asyn;
            Stream    = new ZYNetRingBufferPool(1024 * 1024 * 2);//2MB
        }
Esempio n. 11
0
        /// <summary>
        /// 创建注册 ASyncToken
        /// </summary>
        /// <param name="socketAsync"></param>
        /// <returns></returns>
        private ASyncToken NewASyncToken(SocketAsyncEventArgs socketAsync, ZYNetRingBufferPool stream, long sessionkey)
        {
            ASyncToken tmp = new ASyncToken(this.LoggerFactory, socketAsync, this, sessionkey, stream)
            {
                ExceptionOut = this.ExceptionOut
            };

            return(tmp);
        }
Esempio n. 12
0
        private void DataOn(byte[] data, SocketAsyncEventArgs socketAsync, ZYNetRingBufferPool stream)
        {
            ReadBytes read = new ReadBytes(data);

            if (read.Length >= 4)
            {
                if (read.ReadInt32(out int lengt) && lengt == read.Length)
                {
                    if (read.ReadByte() == 0xED &&
                        read.ReadByte() == 0xCE &&
                        read.ReadByte() == 0xFE &&
                        read.ReadByte() == 0x10)
                    {
                        long sessionId = read.ReadInt64();

                        if (sessionId == 0)
                        {
                            var token = MakeNewToken(socketAsync, stream, ref sessionId);
                            if (token != null)
                            {
                                BufferFormat session = new BufferFormat(0x10FECEED);
                                session.AddItem(sessionId);
                                Send(token.Sendobj, session.Finish());
                            }
                        }
                        else
                        {
                            if (TokenList.TryGetValue(sessionId, out ASyncToken token))
                            {
                                token.SetSocketEventAsync(socketAsync);
                                socketAsync.UserToken = token;
                                Log.Debug($"ReUse Token {token.SessionKey}");
                            }
                            else
                            {
                                var _token = MakeNewToken(socketAsync, stream, ref sessionId);
                                if (_token != null)
                                {
                                    BufferFormat session = new BufferFormat(0x10FECEED);
                                    session.AddItem(sessionId);
                                    Send(_token.Sendobj, session.Finish());
                                }
                            }
                        }
                    }
                    else
                    {
                        Server.Disconnect(socketAsync.AcceptSocket);
                    }
                }
                else
                {
                    Server.Disconnect(socketAsync.AcceptSocket);
                }
            }
Esempio n. 13
0
        /// <summary>
        /// 数据包输入
        /// </summary>
        /// <param name="data">输入数据</param>
        /// <param name="socketAsync">该数据包的通讯SOCKET</param>
        static void BinaryInputHandler(byte[] data, int offset, int count, SocketAsyncEventArgs socketAsync)
        {
            try
            {
                if (socketAsync.UserToken == null) //如果此SOCKET绑定的对象为NULL
                {
                    //注意这里为了 简单 所以就绑定了个 BuffList 类,本来这里应该绑定用户类对象,
                    //并在用户类里面建立 初始化 一个 BuffList 类,这样就能通过用户类保存更多的信息了。
                    //比如用户名,权限等等


                    socketAsync.UserToken = new ZYNetRingBufferPool(409600);
                }



                //BuffList 数据包组合类 如果不想丢数据就用这个类吧
                ZYNetRingBufferPool buff = socketAsync.UserToken as ZYNetRingBufferPool;


                if (buff != null)
                {
                    buff.Write(data, offset, count); //呵呵带offset的 可以省一次copy


                    byte[] pdata;
                    while (buff.Read(out pdata))
                    {
                        DataOn(pdata, socketAsync);
                    }
                }
                else
                {
                    UserInfo user = socketAsync.UserToken as UserInfo;

                    if (user != null)
                    {
                        user.BuffPool.Write(data, offset, count); //呵呵带offset的 可以省一次copy


                        byte[] pdata;
                        while (user.BuffPool.Read(out pdata))
                        {
                            DataOn(pdata, user);
                        }
                    }
                }
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
            }
        }
Esempio n. 14
0
 public ASyncToken(ILoggerFactory loggerFactory, SocketAsyncEventArgs asynca, CloudServer server, long sessionKey, ZYNetRingBufferPool stream)
 {
     this.Asyn          = asynca;
     Stream             = stream;
     CurrentServer      = server;
     this.DataExtra     = server.EcodeingHandler;
     Sendobj            = Container.Resolve <ISend>(new NamedParameter("sock", asynca.AcceptSocket), new NamedParameter("bufferLength", Container.Resolve <BufferSizeOptions>().SendBufferSize));
     AsyncWaitTimeOut   = new List <KeyValuePair <long, DateTime> >();
     FodyDir            = new Dictionary <Type, Type>();
     SessionKey         = sessionKey;
     this.LoggerFactory = loggerFactory;
     Log = new DefaultLog(LoggerFactory?.CreateLogger <ASyncToken>());
 }
Esempio n. 15
0
 public CloudClient(ISyncClient client, int millisecondsTimeout, int maxBufferLength)
 {
     SyncWaitDic         = new ConcurrentDictionary <long, ReturnEventWaitHandle>(10, 10000);
     AsyncCallDiy        = new ConcurrentDictionary <long, AsyncCalls>();
     CallBackDiy         = new ConcurrentDictionary <long, AsyncCalls>();
     AsyncRunDiy         = new ConcurrentDictionary <long, AsyncRun>();
     Client              = client;
     MillisecondsTimeout = millisecondsTimeout;
     RingBuffer          = new ZYNetRingBufferPool(maxBufferLength);
     Sync                = new ZYSync();
     Sync.SyncSend       = SendData;
     Sync.SyncSendAsWait = SendDataAsWait;
     Module              = new ModuleDictionary();
 }
Esempio n. 16
0
        private ASyncToken MakeNewToken(SocketAsyncEventArgs socketAsync, ZYNetRingBufferPool stream, ref long sessionId)
        {
            sessionId = MakeSessionId();
            var token = NewASyncToken(socketAsync, stream, sessionId);

            socketAsync.UserToken = token;
            if (!TokenList.TryAdd(sessionId, token))
            {
                Server.Disconnect(socketAsync.AcceptSocket);
                return(null);
            }

            Log.Debug($"Create Token {token.SessionKey}");

            return(token);
        }
Esempio n. 17
0
        private void BinaryInputOffsetHandler(byte[] data, int offset, int count, SocketAsyncEventArgs socketAsync)
        {
            try
            {
                if (socketAsync.UserToken is ASyncToken tmp)
                {
                    tmp.Write(data, offset, count);
                }
                else
                {
                    ZYNetRingBufferPool stream;
                    if (socketAsync.UserToken != null)
                    {
                        stream = socketAsync.UserToken as ZYNetRingBufferPool;
                    }
                    else
                    {
                        stream = new ZYNetRingBufferPool(MaxPacksize);
                        socketAsync.UserToken = stream;
                    }

                    stream.Write(data, offset, count);

                    if (stream.Read(out byte[] pdata))
                    {
                        DataOn(pdata, socketAsync, stream);
                    }
                }
            }
            catch (Exception er)
            {
                var b = ExceptionOut?.Invoke(er);
                if (b is null)
                {
                    Log.Error(er.Message, er);
                }
                else if (b.Value)
                {
                    Log.Error(er.Message, er);
                }
            }
        }
Esempio n. 18
0
 public ZYNetSession(long Id, SocketAsyncEventArgs asyn, ZYNetRingBufferPool bufferQueue)
 {
     this.Id          = Id;
     this.Asyn        = asyn;
     this.BufferQueue = bufferQueue;
 }
Esempio n. 19
0
 public UserInfo(SocketAsyncEventArgs asyn)
     : base(asyn)
 {
     Stream = new ZYNetRingBufferPool(1024 * 1024);
 }
Esempio n. 20
0
 public UserInfo()
 {
     BuffManger = new ZYNetRingBufferPool(RConfig.ReadInt("MaxBuffSize"));
 }
Esempio n. 21
0
 public UserInfo()
 {
     BuffManger = new ZYNetRingBufferPool();
 }