/// <summary> /// Note: msgType and msgId are left to zero /// </summary> internal unsafe TrinityMessage(byte *buffer, int offset, int size) { Size = TrinityProtocol.MsgHeader + size; Buffer = (byte *)CMemory.C_malloc((ulong)Size); *(int *)Buffer = TrinityProtocol.TrinityMsgHeader + size; Memory.Copy(buffer, offset, Buffer + TrinityProtocol.MsgHeader, 0, size); }
/// <summary> /// Used for constructing a Response message. /// </summary> /// <param name="errorCode">A 32-bit Trinity error code.</param> internal TrinityMessage(TrinityErrorCode errorCode) { Size = TrinityProtocol.MsgHeader; Buffer = (byte *)CMemory.C_malloc((ulong)Size); *(int *)Buffer = TrinityProtocol.TrinityMsgHeader; *(int *)(Buffer + TrinityProtocol.TrinityMsgHeader) = (int)errorCode; }
internal TrinityMessage(TrinityMessageType msgType, ushort msgId, int size) { Size = TrinityProtocol.MsgHeader + size; Buffer = (byte *)CMemory.C_malloc((ulong)Size); *(int *)Buffer = TrinityProtocol.TrinityMsgHeader + size; *(Buffer + TrinityProtocol.MsgTypeOffset) = (byte)msgType; *(ushort *)(Buffer + TrinityProtocol.MsgIdOffset) = msgId; }
/// <inheritdoc/> public void SendMessage(byte **message, int *sizes, int count, out TrinityResponse response) { byte *buf; int len; _serialize(message, sizes, count, out buf, out len); SendMessage(buf, len, out response); CMemory.C_free(buf); }
/// <inheritdoc/> public void SendMessage(byte **message, int *sizes, int count) { byte *buf; int len; _serialize(message, sizes, count, out buf, out len); TrinityMessageType msgType = *(TrinityMessageType *)(buf + TrinityProtocol.MsgTypeOffset); ushort msgId = *(ushort *)(buf + TrinityProtocol.MsgIdOffset); // For async messages, we omit the buffer copy, use the serialized buffer directly. switch (msgType) { case TrinityMessageType.ASYNC: { AsynReqArgs aut_request = new AsynReqArgs( MessageHandlers.DefaultParser.async_handlers[msgId], buf, TrinityProtocol.MsgHeader, len - TrinityProtocol.MsgHeader); if (aut_request.AsyncProcessMessage() == TrinityErrorCode.E_RPC_EXCEPTION) { throw new IOException("Local message handler throws an exception."); } } break; case TrinityMessageType.ASYNC_WITH_RSP: { AsynReqRspArgs async_rsp_args = new AsynReqRspArgs( MessageHandlers.DefaultParser.async_rsp_handlers[msgId], buf, TrinityProtocol.MsgHeader, len - TrinityProtocol.MsgHeader); if (async_rsp_args.AsyncProcessMessage() == TrinityErrorCode.E_RPC_EXCEPTION) { throw new IOException("Local message handler throws an exception."); } } break; default: { SendMessage(buf, len); CMemory.C_free(buf); } break; } }
static void MainSequencedRun() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #if EXTERNAL_CAMERA CCamera pCamera = new CCamera(); pCamera.Name = "Фотоаппарат №1"; pCamera.Brand = "Зенит-М"; pCamera.Memory.Amount = 7; // заполнение внешней карты памяти CMemory pMemory = new CMemory(); CPhoto pPhoto = new CPhoto() { Title = "adsf" }; pMemory.PhotoList.Add(pPhoto); // копирование с карты памяти на внутреннюю память pCamera.CopyFromMemory(pMemory); // проверка, что есть хотя бы одна фотография во внутренней памяти if (!pCamera.GetPhotoList().Any()) // pCamera.GetPhotoList().Count() == 0) { MessageBox.Show(@"Нет ни одной фотографии"); } else { // запуск формы Form1 pMainForm = new Form1(pCamera); pMainForm.Text = "1"; Application.Run( pMainForm ); Form1 pMainForm2 = new Form1(pCamera); pMainForm2.Text = "2"; Application.Run( pMainForm2 ); } #else // запуск формы Form1 pMainForm = new Form1(pCamera); Application.Run(pMainForm); #endif }
internal static void _serialize(byte **message, int *sizes, int count, out byte *buf, out int len) { len = 0; for (int i = 0; i < count; ++i) { len += sizes[i]; } buf = (byte *)CMemory.C_malloc((ulong)len); byte *p = buf; for (int i = 0; i < count; ++i) { CMemory.C_memcpy((void *)p, (void *)*message, (ulong)*sizes); p += *sizes; ++message; ++sizes; } }
internal TrinityResponse(int size) { Buffer = (byte *)CMemory.C_malloc((ulong)size); Size = size; Offset = TrinityProtocol.TrinityMsgHeader; }
/// <summary> /// Allocate a TrinityMessage whose buffer length is the specified size /// </summary> /// <param name="size">Message buffer length</param> internal unsafe TrinityMessage(int size) { Size = size; Buffer = (byte *)CMemory.C_malloc((ulong)size); *(int *)Buffer = size - TrinityProtocol.SocketMsgHeader; }
/// <summary> /// Releases the unmanaged memory buffer used by the TrinityMessage. /// </summary> public void Dispose() { CMemory.C_free(Buffer); }