Skip to content

Commit

Permalink
bugfix: tcp packet broken while client send config to server
Browse files Browse the repository at this point in the history
  • Loading branch information
tmoonlight committed Nov 26, 2019
1 parent 11d0a15 commit cf1d4d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/NSmartProxy.ClientRouter/ServerConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ private async Task<ClientModel> SendConfigRequest()
var config = ClientConfig;
Router.Logger.Debug("Reading Config From Provider..");
TcpClient configClient = new TcpClient();
configClient.NoDelay = true;//配置协议不使用nagle
bool isConnected = false;
bool isReconn = (this.ClientID != 0); //TODO XXX如果clientid已经分配到了id 则算作重连
for (int j = 0; j < 3; j++) //连接服务端
Expand Down
28 changes: 25 additions & 3 deletions src/NSmartProxy.Infrastructure/Extensions/StreamExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static async Task WriteAndFlushAsync(this Stream stream, byte[] buffer, i
/// <param name="count"></param>
/// <param name="TimeOut"></param>
/// <returns></returns>
public static async Task<int> ReadAsync(this NetworkStream stream, byte[] buffer, int offset, int count, int timeOut)
public static async Task<int> ReadAsync(this Stream stream, byte[] buffer, int offset, int count, int timeOut)
{
var receiveCount = 0;
var receiveTask = Task.Run(async () => { receiveCount = await stream.ReadAsync(buffer, offset, count); });
Expand All @@ -57,10 +57,32 @@ public static async Task<int> ReadAsync(this NetworkStream stream, byte[] buffer
return receiveCount;
}

/// <summary>
/// 读取接下来N字节的定长数据,如果服务端没有发那么多信息,
/// 可能会出现读不全的情况,也有可能出现阻塞超时的情况
/// </summary>
public static async Task<int> ReadNextSTLengthBytes(this Stream stream, byte[] buffer)
{
int restBufferLength = buffer.Length;
int totalReceivedBytes = 0;
while (restBufferLength > 0)
{
int receivedBytes = await stream.ReadAsyncEx(buffer, totalReceivedBytes, restBufferLength);
if (receivedBytes <= 0) return -1;//没有接收满则断开返回-1
totalReceivedBytes += receivedBytes;
restBufferLength -= receivedBytes;
}
return totalReceivedBytes;
}

public static async Task<int> ReadAsyncEx(this Stream stream, byte[] buffer, int offset, int count)
{
return await stream.ReadAsync(buffer, offset, count, Global.DefaultConnectTimeout);
}

public static async Task<int> ReadAsyncEx(this NetworkStream stream, byte[] buffer)
public static async Task<int> ReadAsyncEx(this Stream stream, byte[] buffer)
{
return await stream.ReadAsync(buffer, 0, buffer.Length, Global.DefaultConnectTimeout);
return await stream.ReadAsyncEx(buffer, 0, buffer.Length);
}

public static Stream ProcessSSL(this Stream clientStream, X509Certificate cert)
Expand Down
4 changes: 2 additions & 2 deletions src/NSmartProxy.Infrastructure/Shared/NSPVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
public sealed class NSPVersion
{
public const string NO_TOKEN_STRING = "notoken";
public const string NSmartProxyClientName = "NSmartProxy Client v1.2.7102.9914";
public const string NSmartProxyServerName = "NSmartProxy Server v1.2.7102.9914";
public const string NSmartProxyClientName = "NSmartProxy Client v1.2.7103.8377";
public const string NSmartProxyServerName = "NSmartProxy Server v1.2.7103.8377";
}
4 changes: 3 additions & 1 deletion src/NSmartProxy/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,12 @@ private async Task<bool> ProcessAppRequestProtocol(TcpClient client, bool IsReco
//port proto option(iscompress) host description
//2 1 1 1024 96
byte[] consumerPortBytes = new byte[appCount * (2 + 1 + 1 + 1024 + 96)];//TODO 2 暂时这么写,亟需修改
int resultByte2 = await nstream.ReadAsyncEx(consumerPortBytes);
int resultByte2 = await nstream.ReadNextSTLengthBytes(consumerPortBytes);
// int resultBytetest = await nstream.ReadAsyncEx(consumerPortBytes);
//Server.Logger.Debug("consumerPortBytes received.");
if (resultByte2 < 1)
{
Server.Logger.Debug("定长包没有正确接收");
CloseClient(client);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/build_simple.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ rem windows only
rem NSP v1.1
@ECHO off

set Ver=v1.2_final3
set Ver=v1.2_final4
set BuildPath=%~dp0../build

set nsp_server_path=%BuildPath%/nspclient_%Ver%
Expand Down

0 comments on commit cf1d4d5

Please sign in to comment.