Esempio n. 1
0
        public Boolean TryConnectAndInitialize(TlsSettings tlsSettings, Buf sendBuffer, ServerInfo serverInfo /*, SelectTunnelsThread tunnelsThread*/)
        {
            if (Connected)
            {
                throw new InvalidOperationException(String.Format(
                                                        "You called Connect() on accessor '{0}' but its already connected", accessorHostString));
            }

            try
            {
                accessorSocket = NewSocketConnection(ref accessorHost);

                //
                // Send initial connection information
                //
                Boolean setupTls = tlsSettings.requireTlsForTmpConnections;

                Byte[] connectionInfoPacket = new Byte[] { Tmp.CreateConnectionInfoFromTmpServerToAccessor(
                                                               tlsSettings.requireTlsForTmpConnections, false) };
                accessorSocket.Send(connectionInfoPacket);

                //
                // Only receive packet if tls was not required
                //
                if (!tlsSettings.requireTlsForTmpConnections)
                {
                    int bytesRead = accessorSocket.Receive(connectionInfoPacket, 0, 1, SocketFlags.None);
                    if (bytesRead <= 0)
                    {
                        throw new SocketException();
                    }

                    setupTls = Tmp.ReadTlsRequirementFromAccessorToTmpServer(connectionInfoPacket[0]);
                }


                DataHandler accessorSendHandler = new SocketSendDataHandler(accessorSocket).HandleData;
                this.accessorReceiveHandler = new DataFilterHandler(new FrameProtocolFilter(),
                                                                    new TmpServerHandler(this, tlsSettings /*, tunnelsThread*/));

                //
                // Initiate a tls connection if required
                //
                if (setupTls)
                {
                    throw new NotImplementedException("Tls not yet implemented");
                }

                //
                // Send server info
                //
                UInt32 commandLength = Tmp.SerializeCommand(ServerInfo.Serializer, Tmp.ToAccessorServerInfoID, serverInfo, sendBuffer, 0);
                accessorSendHandler(sendBuffer.array, 0, commandLength);

                return(true);
            }
            catch (Exception)
            {
                this.accessorSocket         = null;
                this.accessorReceiveHandler = null;
                return(false);
            }
        }