Esempio n. 1
0
        // Must be balanced with a call to ReleaseDnsConnection()!
        internal static DNSContract.Imp !GetDnsConnection()
        {
            LocalDataStoreSlot slot = SafeSlotFetchOrInitialize(ref DnsSlot, DnsSlotLock);

            if (Thread.GetData(slot) == null)
            {
                // We haven't created a channel for this thread yet. Create one.
                DNSContract.Imp !dnsImp;
                DNSContract.Exp !dnsExp;
                DirectoryServiceContract.Imp epNS = DirectoryService.NewClientEndpoint();

                DNSContract.NewChannel(out dnsImp, out dnsExp);

                try {
                    epNS.SendBind(Bitter.FromString2(DNSContract.ModuleName), dnsExp);

                    switch receive {
                    case epNS.NakBind(ServiceContract.Exp: Start rejectedEP, error) :
                        if (rejectedEP != null)
                    {
                            delete rejectedEP;
                    }
                        delete dnsImp;

                        // Do nothing; we will return null below.
                        break;

                    case epNS.AckBind():
                        // Success; put our remaining end of the channel
                        // into thread-local storage.
                        dnsImp.RecvReady();
                        TRef <DNSContract.Imp : ReadyState> dnsConnHolder = new TRef <DNSContract.Imp : ReadyState>(dnsImp);

                        Thread.SetData(slot, dnsConnHolder);
                        break;
                    }
                }
                finally {
                    delete epNS;
                }
            }

            // By now there should definitely be a channel in our thread-local storage.
            TRef <DNSContract.Imp : ReadyState> !connHolder = (TRef <DNSContract.Imp : ReadyState> !)Thread.GetData(slot);
            return(connHolder.Acquire());
        }
Esempio n. 2
0
        public static KeyboardDeviceContract.Imp OpenKeyboard(string! devName)
        {
            KeyboardDeviceContract.Exp! exp;
            KeyboardDeviceContract.Imp! imp;
            KeyboardDeviceContract.NewChannel(out imp, out exp);

            // get NS endpoint
            DirectoryServiceContract.Imp ns = DirectoryService.NewClientEndpoint();

            bool success = false;
            ns.SendBind(Bitter.FromString2(devName),exp);
            switch receive {
                case ns.AckBind():
                    success = true;
                    break;
                case ns.NakBind(exp):
                    delete exp;
                    break;
                case ns.ChannelClosed():
                    break;
            }

            if (!success) {
                DebugStub.Print("OpenKeyboard lookup of {0} failed.\n",
                                __arglist(devName);

                delete imp;
                delete ns;
                return null;
            }

            switch receive {
                case imp.Success():
                    break;
                case unsatisfiable:
                    throw new Exception("Didn't imp.RecvAckConnect");
                    break;
            }

            delete ns;
            return imp;
        }