From 5463beab4a335d0a91f741e3d304acd86a11dc66 Mon Sep 17 00:00:00 2001 From: antiduh Date: Tue, 8 Jul 2014 19:08:38 +0000 Subject: [PATCH] Marked the code at the 0.1 release. --- TestClient/ClientForm.Designer.cs | 8 ++--- TestProtocol/CustomConnection.cs | 30 ++++--------------- TestProtocol/CustomServer.cs | 49 ++++++++++++++----------------- TestServer/ServerForm.Designer.cs | 22 +++++++------- TestServer/ServerForm.cs | 2 +- 5 files changed, 42 insertions(+), 69 deletions(-) diff --git a/TestClient/ClientForm.Designer.cs b/TestClient/ClientForm.Designer.cs index 0c18e90..3e462cc 100644 --- a/TestClient/ClientForm.Designer.cs +++ b/TestClient/ClientForm.Designer.cs @@ -148,7 +148,6 @@ private void InitializeComponent() this.sendTextbox.Location = new System.Drawing.Point(6, 19); this.sendTextbox.Multiline = true; this.sendTextbox.Name = "sendTextbox"; - this.sendTextbox.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.sendTextbox.Size = new System.Drawing.Size(302, 298); this.sendTextbox.TabIndex = 7; // @@ -191,7 +190,6 @@ private void InitializeComponent() this.receiveTextbox.Location = new System.Drawing.Point(3, 16); this.receiveTextbox.Multiline = true; this.receiveTextbox.Name = "receiveTextbox"; - this.receiveTextbox.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.receiveTextbox.Size = new System.Drawing.Size(308, 338); this.receiveTextbox.TabIndex = 10; // @@ -233,7 +231,7 @@ private void InitializeComponent() this.disconnectButton.Text = "Disconnect"; this.disconnectButton.UseVisualStyleBackColor = true; // - // ClientForm + // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; @@ -246,8 +244,8 @@ private void InitializeComponent() this.Controls.Add(this.serverTextBox); this.Controls.Add(this.label2); this.Controls.Add(this.label1); - this.Name = "ClientForm"; - this.Text = "Client - SSPI Demo"; + this.Name = "Form1"; + this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.portNumeric)).EndInit(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); diff --git a/TestProtocol/CustomConnection.cs b/TestProtocol/CustomConnection.cs index 1f26baf..8a68881 100644 --- a/TestProtocol/CustomConnection.cs +++ b/TestProtocol/CustomConnection.cs @@ -112,10 +112,7 @@ private void ReadLoop() byte[] readBuffer = new byte[65536]; ProtocolOp operation; - int messageLength; - int remaining; - int chunkLength; - int position; + int length; while( this.running ) { @@ -135,24 +132,10 @@ private void ReadLoop() // Read the length this.socket.Receive( readBuffer, 4, SocketFlags.None ); - messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 ); - - if( readBuffer.Length < messageLength ) - { - readBuffer = new byte[messageLength]; - } + length = ByteWriter.ReadInt32_BE( readBuffer, 0 ); // Read the data - // Keep in mind that Socket.Receive may return less data than asked for. - remaining = messageLength; - chunkLength = 0; - position = 0; - while( remaining > 0 ) - { - chunkLength = this.socket.Receive( readBuffer, position, remaining, SocketFlags.None ); - remaining -= chunkLength; - position += chunkLength; - } + this.socket.Receive( readBuffer, length, SocketFlags.None ); } catch( SocketException e ) @@ -160,8 +143,7 @@ private void ReadLoop() if( e.SocketErrorCode == SocketError.ConnectionAborted || e.SocketErrorCode == SocketError.Interrupted || e.SocketErrorCode == SocketError.OperationAborted || - e.SocketErrorCode == SocketError.Shutdown || - e.SocketErrorCode == SocketError.ConnectionReset ) + e.SocketErrorCode == SocketError.Shutdown ) { // Shutting down. break; @@ -176,8 +158,8 @@ private void ReadLoop() if( this.Received != null ) { - byte[] dataCopy = new byte[messageLength]; - Array.Copy( readBuffer, 0, dataCopy, 0, messageLength ); + byte[] dataCopy = new byte[length]; + Array.Copy( readBuffer, 0, dataCopy, 0, length ); Message message = new Message( operation, dataCopy ); try diff --git a/TestProtocol/CustomServer.cs b/TestProtocol/CustomServer.cs index 318b09a..103407d 100644 --- a/TestProtocol/CustomServer.cs +++ b/TestProtocol/CustomServer.cs @@ -1,12 +1,22 @@ using System; -using System.Net; -using System.Net.Sockets; -using System.Threading; -using System.Windows.Forms; -using NSspi; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace TestProtocol { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Net.Sockets; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Windows.Forms; + using NSspi; + public class CustomServer { private Thread receiveThread; @@ -139,9 +149,7 @@ private void ReadLoop() byte[] readBuffer = new byte[65536]; ProtocolOp operation; - int messageLength; - int position; - int remaining; + int length; while( this.running ) { @@ -149,7 +157,7 @@ private void ReadLoop() { // |--4 bytes--|--4 bytes--|---N--| // Every command is a TLV - | Operation | Length | Data | - int chunkLength; + // Read the operation. this.readSocket.Receive( readBuffer, 4, SocketFlags.None ); @@ -161,24 +169,11 @@ private void ReadLoop() // Read the length this.readSocket.Receive( readBuffer, 4, SocketFlags.None ); - messageLength = ByteWriter.ReadInt32_BE( readBuffer, 0 ); - - if( readBuffer.Length < messageLength ) - { - readBuffer = new byte[messageLength]; - } + length = ByteWriter.ReadInt32_BE( readBuffer, 0 ); // Read the data - // Keep in mind that Socket.Receive may return less data than asked for. - remaining = messageLength; - chunkLength = 0; - position = 0; - while( remaining > 0 ) - { - chunkLength = this.readSocket.Receive( readBuffer, position, remaining, SocketFlags.None ); - remaining -= chunkLength; - position += chunkLength; - } + this.readSocket.Receive( readBuffer, length, SocketFlags.None ); + } catch( SocketException e ) { @@ -201,8 +196,8 @@ private void ReadLoop() if( this.Received != null ) { - byte[] dataCopy = new byte[messageLength]; - Array.Copy( readBuffer, 0, dataCopy, 0, messageLength ); + byte[] dataCopy = new byte[length]; + Array.Copy( readBuffer, 0, dataCopy, 0, length ); Message message = new Message( operation, dataCopy ); try diff --git a/TestServer/ServerForm.Designer.cs b/TestServer/ServerForm.Designer.cs index 5314b64..953b7df 100644 --- a/TestServer/ServerForm.Designer.cs +++ b/TestServer/ServerForm.Designer.cs @@ -38,13 +38,13 @@ private void InitializeComponent() this.label2 = new System.Windows.Forms.Label(); this.serverUsernameTextbox = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); - this.impersonateButton = new System.Windows.Forms.Button(); this.signButton = new System.Windows.Forms.Button(); this.encryptButton = new System.Windows.Forms.Button(); this.sendTextbox = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); this.receivedTextbox = new System.Windows.Forms.TextBox(); this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); + this.impersonateButton = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.portNumeric)).BeginInit(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); @@ -168,15 +168,6 @@ private void InitializeComponent() this.groupBox2.TabStop = false; this.groupBox2.Text = "Send a message to the client"; // - // impersonateButton - // - this.impersonateButton.Location = new System.Drawing.Point(262, 350); - this.impersonateButton.Name = "impersonateButton"; - this.impersonateButton.Size = new System.Drawing.Size(116, 23); - this.impersonateButton.TabIndex = 4; - this.impersonateButton.Text = "Test impersonation"; - this.impersonateButton.UseVisualStyleBackColor = true; - // // signButton // this.signButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); @@ -205,7 +196,6 @@ private void InitializeComponent() this.sendTextbox.Location = new System.Drawing.Point(6, 19); this.sendTextbox.Multiline = true; this.sendTextbox.Name = "sendTextbox"; - this.sendTextbox.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.sendTextbox.Size = new System.Drawing.Size(400, 323); this.sendTextbox.TabIndex = 0; // @@ -226,7 +216,6 @@ private void InitializeComponent() this.receivedTextbox.Location = new System.Drawing.Point(3, 16); this.receivedTextbox.Multiline = true; this.receivedTextbox.Name = "receivedTextbox"; - this.receivedTextbox.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.receivedTextbox.Size = new System.Drawing.Size(407, 370); this.receivedTextbox.TabIndex = 0; // @@ -247,6 +236,15 @@ private void InitializeComponent() this.tableLayoutPanel1.Size = new System.Drawing.Size(838, 395); this.tableLayoutPanel1.TabIndex = 7; // + // impersonateButton + // + this.impersonateButton.Location = new System.Drawing.Point(262, 350); + this.impersonateButton.Name = "impersonateButton"; + this.impersonateButton.Size = new System.Drawing.Size(116, 23); + this.impersonateButton.TabIndex = 4; + this.impersonateButton.Text = "Test impersonation"; + this.impersonateButton.UseVisualStyleBackColor = true; + // // ServerForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); diff --git a/TestServer/ServerForm.cs b/TestServer/ServerForm.cs index 7985b4d..3d96b55 100644 --- a/TestServer/ServerForm.cs +++ b/TestServer/ServerForm.cs @@ -173,7 +173,7 @@ private void server_Received( Message message ) private void server_Disconnected() { - this.running = true; + this.running = false; this.initializing = true; this.connected = false;