Пример #1
0
            // 消息请求结束或发送
            public void End(NetConnection nc, IWriteableBuffer buff)
            {
                WriteBuffer wb = (buff as WriteBuffer);

                // 如果没有被使用说明这个buff已经被还回
                // 说明有什么地方调用了多次end
                if (!wb.IsUsing)
                {
                    throw new Exception("call end more than once");
                }

                int len = buff.Available - sizeof(int);

                wb.Unreserve(0, BitConverter.GetBytes(IPAddress.HostToNetworkOrder(len)));

                // if it's a request
                long         no = -wb.PeekLong(sizeof(int));
                CallbackNode cbn;

                if (callbacks.TryGetValue(no, out cbn))
                {
                    cbn.nc = nc;
                }

                // 长度信息不加密,余下加密
                for (int i = sizeof(int); i < wb.Available; i++)
                {
                    wb.Data[i] = nc.Encrypt(wb.Data[i]);
                }

                nc.SendData(wb.Data, 0, wb.Available);
                WriteBufferPool.Enqueue(wb);
                wb.IsUsing = false;

                if (OnSendMsg != null)
                {
                    OnSendMsg(len);
                }
            }
Пример #2
0
            // 消息请求结束或发送
            public void End(NetConnection nc, IWriteableBuffer buff)
            {
                WriteBuffer wb = (buff as WriteBuffer);

                // 如果没有被使用说明这个buff已经被还回
                // 说明有什么地方调用了多次end
                if (!wb.IsUsing)
                {
                    throw new Exception("call end more than once");
                }

                // 检查是否和上一条消息完全一致
                var identifyWithLastOne = false;
                var rawData             = wb.Data;
                var rawDataLen          = wb.Available;

                if (nc.LastSent != null && nc.LastSent.Available == rawDataLen)
                {
                    identifyWithLastOne = true;
                    for (var i = 0; i < rawDataLen; i++)
                    {
                        if (nc.LastSent.Data[i] != rawData[i])
                        {
                            identifyWithLastOne = false;
                            break;
                        }
                    }
                }

                if (identifyWithLastOne)
                {
                    nc.SendData(TrueBytes, 0, TrueBytes.Length);
                }
                else
                {
                    wb.Unreserve(0, FalseByte);
                    int len = buff.Available - sizeof(bool) - sizeof(int);
                    wb.Unreserve(sizeof(bool), BitConverter.GetBytes(IPAddress.HostToNetworkOrder(len)));

                    // if it's a request
                    long         no = -wb.PeekLong(FalseByte.Length + sizeof(int));
                    CallbackNode cbn;
                    if (callbacks.TryGetValue(no, out cbn))
                    {
                        cbn.nc = nc;
                    }

                    // 长度信息不加密,余下加密
                    for (int i = sizeof(int); i < wb.Available; i++)
                    {
                        wb.Data[i] = nc.Encrypt(wb.Data[i]);
                    }

                    nc.SendData(wb.Data, 0, wb.Available);

                    Dealloc(nc.LastSent);

                    nc.LastSent = wb;
                }

                wb.IsUsing = false;
            }