Example #1
0
        private async static Task HandleDeviceNegotiations(InformationContext iCtx, WebSocketContext wsCtx, WebSocket socket, byte[] binaryMessage, string textMessage)
        {
            bool playBob = false;
            INegotiationProtocolMember protocolParty = null;

            if (binaryMessage != null)
            {
                iCtx.AccessLockedItems(dict =>
                {
                    if (dict.ContainsKey("EKENEGOTIATIONPARTY") == false)
                    {
                        TheBallEKE protocolInstance = new TheBallEKE();
                        protocolInstance.InitiateCurrentSymmetricFromSecret("testsecretXYZ33");
                        if (playBob)
                        {
                            protocolParty = new TheBallEKE.EKEBob(protocolInstance, true);
                        }
                        else
                        {
                            protocolParty = new TheBallEKE.EKEAlice(protocolInstance, true);
                        }
                        dict.Add("EKENEGOTIATIONPARTY", protocolParty);
                    }
                    else
                    {
                        protocolParty = (INegotiationProtocolMember)dict["EKENEGOTIATIONPARTY"];
                    }
                });
                if (protocolParty.SendMessageToOtherPartyAsync == null)
                {
                    protocolParty.SendMessageToOtherPartyAsync = async bytes => { await SendBinaryMessage(socket, bytes); };
                    if (playBob) // if we play bob we put the current message already to the pipeline
                    {
                        protocolParty.LatestMessageFromOtherParty = binaryMessage;
                    }
                }
                else
                {
                    // Alice plays first, so only after the second message we start putting messages from Bob
                    protocolParty.LatestMessageFromOtherParty = binaryMessage;
                }
                while (protocolParty.IsDoneWithProtocol == false && protocolParty.WaitForOtherParty == false)
                {
                    await protocolParty.PerformNextActionAsync();
                }
            }
            else
            {
                iCtx.AccessLockedItems(dict =>
                {
                    if (dict.ContainsKey("EKENEGOTIATIONPARTY"))
                    {
                        protocolParty = (INegotiationProtocolMember)dict["EKENEGOTIATIONPARTY"];
                    }
                });
                if (protocolParty != null && protocolParty.IsDoneWithProtocol) // Perform POST EKE operations
                {
                    iCtx.AccessLockedItems(dict => dict.Remove("EKENEGOTIATIONPARTY"));
                    FinishDeviceNegotiation(iCtx, protocolParty, textMessage);
                }
                //await SendTextMessage(socket, echoString);
            }
        }