Exemplo n.º 1
0
 public void Send(int id, LuaBuffer buffer)
 {
     if (_socket == null)
     {
         string errorMessage = "session not initialize.";
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
             return;
         }
         throw new Exception(errorMessage);
     }
     byte[] bufferBytes = buffer.ToBytes();
     if (bufferBytes == null)
     {
         string errorMessage = "luaBuff is null.";
         if (ErrorHandler != null)
         {
             ErrorHandler(this, SessionErrorCode.StateError, errorMessage);
             return;
         }
         throw new Exception(errorMessage);
     }
     try
     {
         int    length       = 0;
         int    packetLength = 0;
         byte[] packetBuffer = new byte[_defaultMaxPacketLength];//TODO:内存池;
         using (MemoryStream memoryStream = new MemoryStream(packetBuffer, true))
         {
             memoryStream.Seek(_defaultPacketLength, SeekOrigin.Begin);
             byte[] 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;
     }
 }
 static int ToBytes(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         Framework.LuaBuffer obj = (Framework.LuaBuffer)ToLua.CheckObject <Framework.LuaBuffer>(L, 1);
         byte[] o = obj.ToBytes();
         ToLua.Push(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Exemplo n.º 3
0
    static int ToBytes(IntPtr L)
    {
#if UNITY_EDITOR
        ToluaProfiler.AddCallRecord("Framework.LuaBuffer.ToBytes");
#endif
        try
        {
            ToLua.CheckArgsCount(L, 1);
            Framework.LuaBuffer obj = (Framework.LuaBuffer)ToLua.CheckObject <Framework.LuaBuffer>(L, 1);
            byte[] o = obj.ToBytes();
            ToLua.Push(L, o);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }