Skip to content

Commit 3c4ef30

Browse files
bear101hwangsihu
authored andcommitted
Replace DataToObject() by union in TTMessage
1 parent 8bb8a90 commit 3c4ef30

File tree

3 files changed

+137
-173
lines changed

3 files changed

+137
-173
lines changed

Client/ttserverlog.net/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static void Main(string[] args)
122122
{
123123
ttclient.ProcessMsg(msg);
124124
if (msg.nClientEvent == ClientEvent.CLIENTEVENT_CMD_PROCESSING &&
125-
msg.nSource == cur_cmd_id && (bool)msg.DataToObject() /* bActive */ == false)
125+
msg.nSource == cur_cmd_id && msg.bActive == false)
126126
break;
127127
}
128128

Library/TeamTalk.NET/TeamTalk.cs

Lines changed: 67 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3967,94 +3967,58 @@ public enum TTType : uint
39673967
* instance.
39683968
*
39693969
* @see TeamTalkBase.GetMessage */
3970-
[StructLayout(LayoutKind.Sequential)]
3970+
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
39713971
public struct TTMessage
39723972
{
39733973
/** @brief The event's message number @see ClientEvent */
3974+
[FieldOffset(0)]
39743975
public ClientEvent nClientEvent;
39753976
/** @brief The source of the event depends on @c wmMsg */
3977+
[FieldOffset(4)]
39763978
public int nSource;
39773979
/** @brief Specifies which member to access in the union */
3980+
[FieldOffset(8)]
39783981
public TTType ttType;
39793982
/** @brief Reserved. To preserve alignment. */
3983+
[FieldOffset(12)]
39803984
public uint uReserved;
3981-
[MarshalAs(UnmanagedType.ByValArray, SizeConst = TTDLL.SIZEOF_TTMESSAGE_DATA)]
3982-
public byte[] data;
3983-
//UnionData data;
3984-
3985-
public object DataToObject()
3986-
{
3987-
switch (ttType)
3988-
{
3989-
case TTType.__CHANNEL:
3990-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(Channel));
3991-
case TTType.__CLIENTERRORMSG:
3992-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(ClientErrorMsg));
3993-
case TTType.__DESKTOPINPUT:
3994-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(DesktopInput));
3995-
case TTType.__FILETRANSFER:
3996-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(FileTransfer));
3997-
case TTType.__MEDIAFILEINFO:
3998-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(MediaFileInfo));
3999-
case TTType.__REMOTEFILE:
4000-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(RemoteFile));
4001-
case TTType.__SERVERPROPERTIES:
4002-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(ServerProperties));
4003-
case TTType.__SERVERSTATISTICS:
4004-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(ServerStatistics));
4005-
case TTType.__TEXTMESSAGE:
4006-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(TextMessage));
4007-
case TTType.__USER:
4008-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(User));
4009-
case TTType.__USERACCOUNT:
4010-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(UserAccount));
4011-
case TTType.__BANNEDUSER :
4012-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(BannedUser));
4013-
case TTType.__TTBOOL:
4014-
return Marshal.ReadInt32(TTDLL.TT_DBG_GETDATAPTR(ref this)) != 0;
4015-
case TTType.__INT32:
4016-
return Marshal.ReadInt32(TTDLL.TT_DBG_GETDATAPTR(ref this));
4017-
case TTType.__STREAMTYPE :
4018-
return (StreamType)Marshal.ReadInt32(TTDLL.TT_DBG_GETDATAPTR(ref this));
4019-
case TTType.__AUDIOINPUTPROGRESS :
4020-
return Marshal.PtrToStructure(TTDLL.TT_DBG_GETDATAPTR(ref this), typeof(AudioInputProgress));
4021-
default:
4022-
return null;
4023-
}
4024-
}
4025-
}
4026-
4027-
[StructLayout(LayoutKind.Explicit, Size = 5224)]
4028-
struct UnionData
4029-
{
4030-
[FieldOffset(0)]
4031-
public ClientErrorMsg clienterrormsg;
4032-
[FieldOffset(0)]
3985+
[FieldOffset(16)]
40333986
public Channel channel;
4034-
[FieldOffset(0)]
3987+
[FieldOffset(16)]
3988+
public ClientErrorMsg clienterrormsg;
3989+
[FieldOffset(16)]
40353990
public DesktopInput desktopinput;
4036-
[FieldOffset(0)]
3991+
[FieldOffset(16)]
40373992
public FileTransfer filetransfer;
4038-
[FieldOffset(0)]
3993+
[FieldOffset(16)]
40393994
public MediaFileInfo mediafileinfo;
4040-
[FieldOffset(0)]
3995+
[FieldOffset(16)]
40413996
public RemoteFile remotefile;
4042-
[FieldOffset(0)]
3997+
[FieldOffset(16)]
40433998
public ServerProperties serverproperties;
4044-
[FieldOffset(0)]
3999+
[FieldOffset(16)]
4000+
public ServerStatistics serverstatistics;
4001+
[FieldOffset(16)]
40454002
public TextMessage textmessage;
4046-
[FieldOffset(0)]
4003+
[FieldOffset(16)]
40474004
public User user;
4048-
[FieldOffset(0)]
4005+
[FieldOffset(16)]
40494006
public UserAccount useraccount;
4050-
[FieldOffset(0)]
4007+
[FieldOffset(16)]
4008+
public BannedUser banneduser;
4009+
[FieldOffset(16)]
40514010
public bool bActive;
4052-
[FieldOffset(0)]
4011+
[FieldOffset(16)]
40534012
public int nBytesRemain;
4054-
[FieldOffset(0)]
4013+
[FieldOffset(16)]
40554014
public int nStreamID;
4056-
[FieldOffset(0)]
4015+
[FieldOffset(16)]
40574016
public int nPayloadSize;
4017+
[FieldOffset(16)]
4018+
public StreamType nStreamType;
4019+
[FieldOffset(16)]
4020+
public AudioInputProgress audioinputprogress;
4021+
40584022
}
40594023

40604024
/** @}*/
@@ -4525,23 +4489,23 @@ public void ProcessMsg(TTMessage msg)
45254489
break;
45264490
case ClientEvent.CLIENTEVENT_CON_MAX_PAYLOAD_UPDATED :
45274491
if (OnConnectionMaxPayloadUpdated != null)
4528-
OnConnectionMaxPayloadUpdated((int)msg.DataToObject());
4492+
OnConnectionMaxPayloadUpdated(msg.nPayloadSize);
45294493
break;
45304494
case ClientEvent.CLIENTEVENT_CMD_PROCESSING:
45314495
if (OnCmdProcessing != null)
4532-
OnCmdProcessing(msg.nSource, (bool)msg.DataToObject());
4496+
OnCmdProcessing(msg.nSource, msg.bActive);
45334497
break;
45344498
case ClientEvent.CLIENTEVENT_CMD_ERROR:
45354499
if (OnCmdError != null)
4536-
OnCmdError((int)msg.nSource, (ClientErrorMsg)msg.DataToObject());
4500+
OnCmdError((int)msg.nSource, msg.clienterrormsg);
45374501
break;
45384502
case ClientEvent.CLIENTEVENT_CMD_SUCCESS :
45394503
if (OnCmdSuccess != null)
45404504
OnCmdSuccess((int)msg.nSource);
45414505
break;
45424506
case ClientEvent.CLIENTEVENT_CMD_MYSELF_LOGGEDIN:
45434507
if (OnCmdMyselfLoggedIn != null)
4544-
OnCmdMyselfLoggedIn((int)msg.nSource, (UserAccount)msg.DataToObject());
4508+
OnCmdMyselfLoggedIn((int)msg.nSource, msg.useraccount);
45454509
break;
45464510
case ClientEvent.CLIENTEVENT_CMD_MYSELF_LOGGEDOUT:
45474511
if (OnCmdMyselfLoggedOut != null)
@@ -4551,143 +4515,143 @@ public void ProcessMsg(TTMessage msg)
45514515
if (msg.ttType == TTType.__USER)
45524516
{
45534517
if (OnCmdMyselfKicked != null)
4554-
OnCmdMyselfKicked((User)msg.DataToObject());
4518+
OnCmdMyselfKicked(msg.user);
45554519
}
45564520
else if (OnCmdMyselfKicked != null)
45574521
OnCmdMyselfKicked(new User());
45584522
break;
45594523
case ClientEvent.CLIENTEVENT_CMD_USER_LOGGEDIN:
45604524
if (OnCmdUserLoggedIn != null)
4561-
OnCmdUserLoggedIn((User)msg.DataToObject());
4525+
OnCmdUserLoggedIn(msg.user);
45624526
break;
45634527
case ClientEvent.CLIENTEVENT_CMD_USER_LOGGEDOUT:
45644528
if (OnCmdUserLoggedOut != null)
4565-
OnCmdUserLoggedOut((User)msg.DataToObject());
4529+
OnCmdUserLoggedOut(msg.user);
45664530
break;
45674531
case ClientEvent.CLIENTEVENT_CMD_USER_UPDATE:
45684532
if (OnCmdUserUpdate != null)
4569-
OnCmdUserUpdate((User)msg.DataToObject());
4533+
OnCmdUserUpdate(msg.user);
45704534
break;
45714535
case ClientEvent.CLIENTEVENT_CMD_USER_JOINED:
45724536
if (OnCmdUserJoinedChannel != null)
4573-
OnCmdUserJoinedChannel((User)msg.DataToObject());
4537+
OnCmdUserJoinedChannel(msg.user);
45744538
break;
45754539
case ClientEvent.CLIENTEVENT_CMD_USER_LEFT:
45764540
if (OnCmdUserLeftChannel != null)
4577-
OnCmdUserLeftChannel((User)msg.DataToObject());
4541+
OnCmdUserLeftChannel(msg.user);
45784542
break;
45794543
case ClientEvent.CLIENTEVENT_CMD_USER_TEXTMSG:
45804544
if (OnCmdUserTextMessage != null)
4581-
OnCmdUserTextMessage((TextMessage)msg.DataToObject());
4545+
OnCmdUserTextMessage(msg.textmessage);
45824546
break;
45834547
case ClientEvent.CLIENTEVENT_CMD_CHANNEL_NEW:
45844548
if (OnCmdChannelNew != null)
4585-
OnCmdChannelNew((Channel)msg.DataToObject());
4549+
OnCmdChannelNew(msg.channel);
45864550
break;
45874551
case ClientEvent.CLIENTEVENT_CMD_CHANNEL_UPDATE:
45884552
if (OnCmdChannelUpdate != null)
4589-
OnCmdChannelUpdate((Channel)msg.DataToObject());
4553+
OnCmdChannelUpdate(msg.channel);
45904554
break;
45914555
case ClientEvent.CLIENTEVENT_CMD_CHANNEL_REMOVE:
45924556
if (OnCmdChannelRemove != null)
4593-
OnCmdChannelRemove((Channel)msg.DataToObject());
4557+
OnCmdChannelRemove(msg.channel);
45944558
break;
45954559
case ClientEvent.CLIENTEVENT_CMD_SERVER_UPDATE:
45964560
if (OnCmdServerUpdate != null)
4597-
OnCmdServerUpdate((ServerProperties)msg.DataToObject());
4561+
OnCmdServerUpdate(msg.serverproperties);
45984562
break;
45994563
case ClientEvent.CLIENTEVENT_CMD_SERVERSTATISTICS :
46004564
if (OnCmdServerStatistics != null)
4601-
OnCmdServerStatistics((ServerStatistics)msg.DataToObject());
4565+
OnCmdServerStatistics(msg.serverstatistics);
46024566
break;
46034567
case ClientEvent.CLIENTEVENT_CMD_FILE_NEW:
46044568
if (OnCmdFileNew != null)
4605-
OnCmdFileNew((RemoteFile)msg.DataToObject());
4569+
OnCmdFileNew(msg.remotefile);
46064570
break;
46074571
case ClientEvent.CLIENTEVENT_CMD_FILE_REMOVE:
46084572
if (OnCmdFileRemove != null)
4609-
OnCmdFileRemove((RemoteFile)msg.DataToObject());
4573+
OnCmdFileRemove(msg.remotefile);
46104574
break;
46114575
case ClientEvent.CLIENTEVENT_CMD_USERACCOUNT :
46124576
if (OnCmdUserAccount != null)
4613-
OnCmdUserAccount((UserAccount)msg.DataToObject());
4577+
OnCmdUserAccount(msg.useraccount);
46144578
break;
46154579
case ClientEvent.CLIENTEVENT_CMD_BANNEDUSER :
46164580
if (OnCmdBannedUser != null)
4617-
OnCmdBannedUser((BannedUser)msg.DataToObject());
4581+
OnCmdBannedUser(msg.banneduser);
46184582
break;
46194583

46204584
case ClientEvent.CLIENTEVENT_USER_STATECHANGE :
46214585
if (OnUserStateChange != null)
4622-
OnUserStateChange((User)msg.DataToObject());
4586+
OnUserStateChange(msg.user);
46234587
break;
46244588
case ClientEvent.CLIENTEVENT_USER_VIDEOCAPTURE:
46254589
if (OnUserVideoCapture != null)
4626-
OnUserVideoCapture(msg.nSource, (int)msg.DataToObject());
4590+
OnUserVideoCapture(msg.nSource, msg.nStreamID);
46274591
break;
46284592
case ClientEvent.CLIENTEVENT_USER_MEDIAFILE_VIDEO:
46294593
if (OnUserMediaFileVideo != null)
4630-
OnUserMediaFileVideo((int)msg.nSource, (int)msg.DataToObject());
4594+
OnUserMediaFileVideo((int)msg.nSource, msg.nStreamID);
46314595
break;
46324596
case ClientEvent.CLIENTEVENT_USER_DESKTOPWINDOW:
46334597
if (OnUserDesktopWindow != null)
4634-
OnUserDesktopWindow((int)msg.nSource, (int)msg.DataToObject());
4598+
OnUserDesktopWindow((int)msg.nSource, msg.nStreamID);
46354599
break;
46364600
case ClientEvent.CLIENTEVENT_USER_DESKTOPCURSOR:
46374601
if (OnUserDesktopCursor != null)
4638-
OnUserDesktopCursor((int)msg.nSource, (DesktopInput)msg.DataToObject());
4602+
OnUserDesktopCursor((int)msg.nSource, msg.desktopinput);
46394603
break;
46404604
case ClientEvent.CLIENTEVENT_USER_DESKTOPINPUT :
46414605
if (OnUserDesktopInput != null)
4642-
OnUserDesktopInput((int)msg.nSource, (DesktopInput)msg.DataToObject());
4606+
OnUserDesktopInput((int)msg.nSource, msg.desktopinput);
46434607
break;
46444608
case ClientEvent.CLIENTEVENT_USER_RECORD_MEDIAFILE :
46454609
if(OnUserRecordMediaFile != null)
4646-
OnUserRecordMediaFile((int)msg.nSource, (MediaFileInfo)msg.DataToObject());
4610+
OnUserRecordMediaFile((int)msg.nSource, msg.mediafileinfo);
46474611
break;
46484612
case ClientEvent.CLIENTEVENT_USER_AUDIOBLOCK :
46494613
if(OnUserAudioBlock != null)
4650-
OnUserAudioBlock((int)msg.nSource, (StreamType)msg.DataToObject());
4614+
OnUserAudioBlock((int)msg.nSource, msg.nStreamType);
46514615
break;
46524616
case ClientEvent.CLIENTEVENT_INTERNAL_ERROR :
46534617
if(OnInternalError!= null)
4654-
OnInternalError((ClientErrorMsg)msg.DataToObject());
4618+
OnInternalError(msg.clienterrormsg);
46554619
break;
46564620
case ClientEvent.CLIENTEVENT_VOICE_ACTIVATION :
46574621
if(OnVoiceActivation != null)
4658-
OnVoiceActivation((bool)msg.DataToObject());
4622+
OnVoiceActivation(msg.bActive);
46594623
break;
46604624
case ClientEvent.CLIENTEVENT_HOTKEY :
46614625
if(OnHotKeyToggle != null)
4662-
OnHotKeyToggle(msg.nSource, (bool)msg.DataToObject());
4626+
OnHotKeyToggle(msg.nSource, msg.bActive);
46634627
break;
46644628
case ClientEvent.CLIENTEVENT_HOTKEY_TEST :
46654629
if(OnHotKeyTest != null)
4666-
OnHotKeyTest(msg.nSource, (bool)msg.DataToObject());
4630+
OnHotKeyTest(msg.nSource, msg.bActive);
46674631
break;
46684632
case ClientEvent.CLIENTEVENT_FILETRANSFER :
46694633
if(OnFileTransfer != null)
4670-
OnFileTransfer((FileTransfer)msg.DataToObject());
4634+
OnFileTransfer(msg.filetransfer);
46714635
break;
46724636
case ClientEvent.CLIENTEVENT_DESKTOPWINDOW_TRANSFER :
46734637
if(OnDesktopWindowTransfer != null)
4674-
OnDesktopWindowTransfer(msg.nSource, (int)msg.DataToObject());
4638+
OnDesktopWindowTransfer(msg.nSource, msg.nBytesRemain);
46754639
break;
46764640
case ClientEvent.CLIENTEVENT_STREAM_MEDIAFILE :
46774641
if(OnStreamMediaFile != null)
4678-
OnStreamMediaFile((MediaFileInfo)msg.DataToObject());
4642+
OnStreamMediaFile(msg.mediafileinfo);
46794643
break;
46804644
case ClientEvent.CLIENTEVENT_LOCAL_MEDIAFILE:
46814645
if (OnLocalMediaFile != null)
4682-
OnLocalMediaFile((MediaFileInfo)msg.DataToObject());
4646+
OnLocalMediaFile(msg.mediafileinfo);
46834647
break;
46844648
case ClientEvent.CLIENTEVENT_AUDIOINPUT:
46854649
if (OnAudioInput != null)
4686-
OnAudioInput((AudioInputProgress)msg.DataToObject());
4650+
OnAudioInput(msg.audioinputprogress);
46874651
break;
46884652
case ClientEvent.CLIENTEVENT_USER_FIRSTVOICESTREAMPACKET:
46894653
if (OnUserFirstVoiceStreamPacket != null)
4690-
OnUserFirstVoiceStreamPacket((User)msg.DataToObject(), msg.nSource);
4654+
OnUserFirstVoiceStreamPacket(msg.user, msg.nSource);
46914655
break;
46924656
}
46934657
}

0 commit comments

Comments
 (0)