private bool Initialize(uint connectionHandle)
        {
            bool returnResult = false;

            if (connectionHandle != PluginUtils.InvalidHandle)
            {
                this.Handle = connectionHandle;
                this.State  = ConnectionState.Connected;

                thisObject = GCHandle.Alloc(this, GCHandleType.Normal);
                IntPtr thisObjectPtr = GCHandle.ToIntPtr(this.thisObject);

                this.disconnectedHandler = new PluginCallbackHandler(Connection_PluginCallbackWrapper.OnDisconnected_Callback);
                PluginUtils.CheckHResult(
                    Wrapper.exAddDisconnected(this.Handle, this.disconnectedHandler, thisObjectPtr, ref this.disconnectedToken),
                    "Connection.AddDisconnected");

                this.dataReceivedHandler = new Wrapper.DataReceivedHandler(Connection_PluginCallbackWrapper.OnDataReceived_Callback);
                PluginUtils.CheckHResult(
                    Wrapper.exAddReceived(this.Handle, this.dataReceivedHandler, thisObjectPtr, ref this.dataReceivedToken),
                    "Connection.AddRecevied");

                returnResult = true;
            }

            return(returnResult);
        }
        private Connection()
        {
            this.Handle = PluginUtils.InvalidHandle;
            this.State  = ConnectionState.Idle;

            this.disconnectedToken   = 0;
            this.disconnectedHandler = null;

            this.dataReceivedToken   = 0;
            this.dataReceivedHandler = null;
        }
        private Connection()
        {
            this.Handle = Plugin.InvalidHandle;

            this.disconnectedToken          = 0;
            this.disconnectedHandler        = null;
            this.disconnectedCallbackHandle = default(GCHandle);

            this.dataReceivedToken          = 0;
            this.dataReceivedHandler        = null;
            this.dataReceivedCallbackHandle = default(GCHandle);
        }
        private bool Initialize(uint connectionHandle)
        {
            bool returnResult = false;

            if (connectionHandle != Plugin.InvalidHandle)
            {
                this.Handle = connectionHandle;

                this.disconnectedHandler = (handle, result, message) =>
                {
                    if (handle != this.Handle)
                    {
                        return;
                    }

                    if (this.Disconnected != null)
                    {
                        this.Disconnected(this, EventArgs.Empty);
                    }
                };
                this.disconnectedCallbackHandle = GCHandle.Alloc(this.disconnectedHandler);
                Plugin.CheckResult(
                    Wrapper.exAddDisconnected(this.Handle, this.disconnectedHandler, ref this.disconnectedToken),
                    "Connection.AddDisconnected");

                this.dataReceivedHandler = (handle, type, length, buffer) =>
                {
                    if (handle != this.Handle || buffer == IntPtr.Zero)
                    {
                        return;
                    }

                    var packetType = Enum.ToObject(typeof(DataType), type) as DataType?;
                    if (packetType != null && packetType.Value != DataType.Unknown)
                    {
                        if (this.DataReceived == null)
                        {
                            return;
                        }

                        this.DataReceived(this, new DataReceivedArgs(packetType.Value, buffer, length));
                    }
                };
                this.dataReceivedCallbackHandle = GCHandle.Alloc(this.dataReceivedHandler);
                Plugin.CheckResult(
                    Wrapper.exAddReceived(this.Handle, this.dataReceivedHandler, ref this.dataReceivedToken),
                    "Connection.AddRecevied");

                returnResult = true;
            }

            return(returnResult);
        }