Exemple #1
0
        /// <summary>
        /// Connect to the specified NJE server.
        /// </summary>
        public void Connect()
        {
            ControlRecord sendRecord;
            ControlRecord receiveRecord;

            byte[] buffer;

            log.InfoFormat("Connecting to {0}:{1}", this.ServerHost, this.ServerPort);
            tcpClient.Connect(this.ServerHost, this.ServerPort);
            tcpStream = tcpClient.GetStream();

            sendRecord = new ControlRecord("OPEN", this.ClientNodeID, "127.0.0.1", this.ServerNodeID, this.ServerHost, 0);
            buffer     = sendRecord.GetBytes();

            tcpStream.Write(buffer, 0, 33);

            tcpStream.Read(buffer, 0, 33);

            receiveRecord = new ControlRecord(buffer);

            log.DebugFormat("Reason code: {0}, response type: {1}.", receiveRecord.ReasonCode, receiveRecord.RequestType);

            if (receiveRecord.RequestType != "ACK")
            {
                log.ErrorFormat("Could not log into server {0}:{1} with Node ID {2}, server responded: {3}.", this.ServerHost, this.ServerPort, this.ServerNodeID, receiveRecord.RequestType);
            }
            else
            {
                this._isSuccessfullyConnected = true;
                log.InfoFormat("Connected to {0}", this.ServerNodeID);
            }

            // send SOH ENQ

            log.InfoFormat("Sending SOH ENQ to {0}", this.ServerNodeID);
            byte[] sohenq = MakeTTB(MakeTTR(new byte[] { 0x01, 0x2d }));
            tcpStream.Write(sohenq, 0, sohenq.Length);

            while (!tcpStream.DataAvailable)
            {
                log.InfoFormat("Waiting for response to SOH ENQ from {0}...", this.ServerNodeID);
                Thread.Sleep(500);
            }

            MemoryStream responseMS = new MemoryStream();

            while (tcpStream.DataAvailable)
            {
                byte[] rbuffer = new byte[256];
                int    nR      = tcpStream.Read(rbuffer, 0, 256);
                responseMS.Write(rbuffer, 0, nR);
            }

            List <IRecord> result = ProcessData(responseMS.ToArray());


            if (result[0].Data[0] == 0x10 && result[0].Data[1] == 0x70)
            {
                log.InfoFormat("Received DLE ACK0 from {0}.", this.ServerNodeID);
            }
            else
            {
                log.InfoFormat("Did not DLE ACK0 from {0}!", this.ServerNodeID);
            }

            // log in now
            this.SignIn();
        }