Esempio n. 1
0
 // 构造器,需要指明数据映射工具
 public BinaryFilePersistence(string dirPath, Action <T, IWriteableBuffer> data2BufferHandler, Func <IReadableBuffer, T> buff2DataHandler)
     : base((T obj) =>
 {
     WriteBuffer buff = new WriteBuffer();
     data2BufferHandler(obj, buff);
     return(buff.Data);
 },
            (byte[] buff) =>
 {
     RingBuffer data = new RingBuffer();
     if (buff == null)
     {
         return(buff2DataHandler(null));
     }
     else
     {
         data.Write(buff, 0, buff.Length);
         return(buff2DataHandler(data));
     }
 }
            )
 {
     dir  = dirPath;
     hb2d = (byte[] buff) =>
     {
         RingBuffer data = new RingBuffer();
         if (buff == null)
         {
             return(buff2DataHandler(null));
         }
         else
         {
             data.Write(buff, 0, buff.Length);
             return(buff2DataHandler(data));
         }
     };
     if (!Directory.Exists(dir))
     {
         Directory.CreateDirectory(dir);
     }
 }
Esempio n. 2
0
        // 时间驱动处理所有已经接收到的网络消息,并分发到其它各个组件对象
        public void OnTimeElapsed(int te)
        {
            // 每帧发送一次网络数据
            p.ProcessDoSend();

            // 处理所有未完成的连接操作
            p.ProcessPendingConnecting();

            // 处理所有网络消息
            NetConnection[] connections = p.AllConnections;
            foreach (NetConnection nc in connections)
            {
                IReadableBuffer data = nc.ReceivedData;

                // 处理已经完整接收的部分
                while (true)
                {
                    // 判断是否接收到重复上一条消息的指示头
                    if (data.Available < sizeof(bool))
                    {
                        break;
                    }

                    var repeatLastOne = false;
                    if (!data.PeekBool(ref repeatLastOne))
                    {
                        break;
                    }

                    Connection connWrap = GetConnectionWrap(nc);

                    if (repeatLastOne)
                    {
                        data.Skip(sizeof(bool));
                        var componentName = nc.LastData2Component;
                        if (componentName == null)
                        {
                            LogWarning("componentName repeated is null");
                            LogWarning("connected: " + nc.IsConnected + " : " + ((IPEndPoint)nc.Socket.RemoteEndPoint).Address.ToString());
                            nc.Close("componentName repeated is null");
                            break;
                        }

                        var          msgBody = new RingBuffer(true, true);
                        NetComponent c       = GetCom(componentName) as NetComponent;
                        msgBody.Write(nc.LastGetData, 0, nc.LastGetData.Length);
                        c.OnMessage(connWrap, msgBody);
                    }
                    else
                    {
                        // 判断消息是否已经完整接收
                        if (data.Available < sizeof(bool) + sizeof(int))
                        {
                            break;
                        }

                        int len = 0;
                        if (!data.PeekInt(sizeof(bool), ref len))
                        {
                            break;
                        }

                        if (len < 0 || len > NetConnection.MaxRecieveBufferSize)
                        {
                            string ex = "invalid message length: " + len;
                            nc.Close(ex);
                            throw new Exception(ex);
                            // break;
                        }

                        if (data.Available < len + sizeof(bool) + sizeof(int))
                        {
                            break;
                        }

                        data.Skip(sizeof(bool));
                        data.Skip(sizeof(int));

                        // 对该条消息解密
                        data.TravelReplaceBytes4Read(0, len, (byte d) => { return(connWrap.Decrypt(d)); });

                        if (OnMessageRecieved != null)
                        {
                            OnMessageRecieved();
                        }

                        // 消息序号,正数为普通消息,负数为应答消息
                        bool hasNo  = data.ReadBool();
                        long no     = hasNo ? data.ReadLong() : 0;
                        int  offset = hasNo ? sizeof(long) + sizeof(bool) : sizeof(bool);

                        if (no >= 0)
                        {
                            helper.responseID = -no;
                            string componentName = data.ReadString();
                            if (componentName == null)
                            {
                                throw new Exception("componentName try to get is null");
                            }
                            NetComponent c       = GetCom(componentName) as NetComponent;
                            byte[]       msgData = data.ReadBytes(len - offset - sizeof(int) - componentName.Length);

                            nc.LastGetData        = msgData;
                            nc.LastData2Component = componentName;

                            // 没找到对应的模块则忽略
                            if (c == null)
                            {
                                string ex = "no such a component named: " + componentName;
                                nc.Close(ex);
                                throw new Exception(ex);
                                // break;
                            }

                            // 投递消息
                            var msgBody = new RingBuffer(true, true);
                            msgBody.Write(msgData, 0, msgData.Length);
                            c.OnMessage(connWrap, msgBody);
                        }
                        else
                        {
                            byte[] msgData = data.ReadBytes(len - offset);
                            var    msgBody = new RingBuffer(true, true);
                            msgBody.Write(msgData, 0, msgData.Length);

                            // 投递消息
                            CallbackNode tcb;
                            if (callbacks.TryGetValue(no, out tcb))
                            {
                                Action <IReadableBuffer> cb = tcb.cb;
                                callbacks.Remove(no);
                                usrDefinedExpireProcess.Remove(no);

                                if (cb != null)
                                {
                                    cb(msgBody);
                                }
                            }
                            else
                            {
                                string ex = "no request callback for " + no;
                                nc.Close(ex);
                                throw new Exception(ex);
                                // break;
                            }
                        }
                    }
                }
            }

            // 检查请求超时
            CheckNextCallbackTimeout();
        }
Esempio n. 3
0
        // 时间驱动处理所有已经接收到的网络消息,并分发到其它各个组件对象
        public void OnTimeElapsed(int te)
        {
            // 每帧发送一次网络数据
            p.ProcessDoSend();

            // 处理所有未完成的连接操作
            p.ProcessPendingConnecting();

            // 处理所有网络消息
            NetConnection[] connections = p.AllConnections;
            foreach (NetConnection nc in connections)
            {
                IReadableBuffer data = nc.ReceivedData;

                // 处理已经完整接收的部分
                while (true)
                {
                    // 判断消息是否已经完整接收
                    if (data.Available < sizeof(int))
                    {
                        break;
                    }

                    int len = 0;
                    if (!data.PeekInt(ref len))
                    {
                        break;
                    }

                    if (len < 0 || len > NetConnection.MaxRecieveBufferSize)
                    {
                        string ex = "invalid message length: " + len;
                        nc.Close(ex);
                        throw new Exception(ex);
                        // break;
                    }

                    if (data.Available < len + sizeof(int))
                    {
                        break;
                    }
                    else
                    {
                        data.Skip(sizeof(int));
                    }

                    Connection connWrap = GetConnectionWrap(nc);

                    // 对该条消息解密
                    data.TravelReplaceBytes4Read(0, len, (byte d) => { return(connWrap.Decrypt(d)); });

                    if (OnMessageRecieved != null)
                    {
                        OnMessageRecieved();
                    }

                    // 消息序号,正数为普通消息,负数为应答消息
                    long no = data.ReadLong();

                    if (no > 0)
                    {
                        helper.responseID = -no;
                        string       componentName = data.ReadString();
                        NetComponent c             = GetComponent(componentName) as NetComponent;
                        byte[]       msgData       = data.ReadBytes(len - sizeof(long) - sizeof(int) - componentName.Length);

                        // 没找到对应的模块则忽略
                        if (c == null)
                        {
                            string ex = "no such a component named: " + componentName;
                            nc.Close(ex);
                            throw new Exception(ex);
                            // break;
                        }

                        // 投递消息
                        RingBuffer msgBody = new RingBuffer(true, true);
                        msgBody.Write(msgData, 0, msgData.Length);
                        c.OnMessage(connWrap, msgBody);
                    }
                    else if (no < 0)
                    {
                        byte[]     msgData = data.ReadBytes(len - sizeof(long));
                        RingBuffer msgBody = new RingBuffer(true, true);
                        msgBody.Write(msgData, 0, msgData.Length);

                        // 投递消息
                        CallbackNode tcb;
                        if (callbacks.TryGetValue(no, out tcb))
                        {
                            Action <IReadableBuffer> cb = tcb.cb;
                            callbacks.Remove(no);
                            usrDefinedExpireProcess.Remove(no);

                            if (cb != null)
                            {
                                cb(msgBody);
                            }
                        }
                        else
                        {
                            string ex = "no request callback for " + no;
                            nc.Close(ex);
                            throw new Exception(ex);
                            // break;
                        }
                    }
                    else
                    {
                        string ex = "0 is an invalid message sequence number";
                        nc.Close(ex);
                        throw new Exception(ex);
                        // break;
                    }
                }
            }

            // 检查请求超时
            CheckNextCallbackTimeout();
        }