示例#1
0
        public void Send(int id, LuaBuffer buffer)
        {
            if (_socket == null)
            {
                var errorMessage = "session not initialize.";
                if (ErrorHandler != null)
                {
                    ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
                    return;
                }
                throw new Exception(errorMessage);
            }
            var bufferBytes = buffer.ToBytes();

            if (bufferBytes == null)
            {
                var errorMessage = "luaBuff is null.";
                if (ErrorHandler != null)
                {
                    ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
                    return;
                }
                throw new Exception(errorMessage);
            }
            try
            {
                var length       = 0;
                var packetLength = 0;
                var packetBuffer = new byte[_defaultMaxPacketLength];//TODO:内存池;
                using (var memoryStream = new MemoryStream(packetBuffer, true))
                {
                    memoryStream.Seek(_defaultPacketLength, SeekOrigin.Begin);
                    var idBytes = ConvertHelper.GetBytes(id);
                    memoryStream.Write(idBytes, 0, idBytes.Length);
                    memoryStream.Write(bufferBytes, 0, bufferBytes.Length);
                    length = (int)memoryStream.Position;
                }
                packetLength = length - _defaultPacketLength;
                ConvertHelper.GetBytes(packetLength).CopyTo(packetBuffer, 0);
                Send(packetBuffer, 0, length);
            }
            catch (Exception exception)
            {
                Active = false;
                if (ErrorHandler != null)
                {
                    ErrorHandler(this, SessionErrorCode.SerializeError, exception.ToString());
                    return;
                }
                throw;
            }
        }
示例#2
0
    static int ToBytes(IntPtr L)
    {
#if UNITY_EDITOR
        ToluaProfiler.AddCallRecord("Framework.NetModule.LuaBuffer.ToBytes");
#endif
        try
        {
            ToLua.CheckArgsCount(L, 1);
            Framework.NetModule.LuaBuffer obj = (Framework.NetModule.LuaBuffer)ToLua.CheckObject <Framework.NetModule.LuaBuffer>(L, 1);
            byte[] o = obj.ToBytes();
            ToLua.Push(L, o);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }