Skip to content

Commit

Permalink
Socket 接收超时时间配置
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqilscy committed Sep 2, 2015
1 parent 37be386 commit cbb3832
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
5 changes: 5 additions & 0 deletions NewLife.Core/Net/ISocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public interface ISocketClient : ISocketRemote
///// <summary>会话数据流,供用户程序使用,内部不做处理。可用于解决Tcp粘包的问题,把多余的分片放入该数据流中。</summary>
//Stream Stream { get; set; }

/// <summary>
/// 超时时间
/// </summary>
Int32 Timeout { get; set; }

/// <summary>是否活动</summary>
Boolean Active { get; set; }
#endregion
Expand Down
16 changes: 10 additions & 6 deletions NewLife.Net/NetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static ISocketClient CreateClient(NetUri uri)
/// <summary>根据网络标识创建客户端会话并连接(对Tcp)</summary>
/// <param name="uri"></param>
/// <returns></returns>
public static ISocketSession CreateSession(NetUri uri)
public static ISocketSession CreateSession(NetUri uri, int timeout = 3000)
{
if (uri == null) throw new ArgumentNullException("uri");

Expand All @@ -70,11 +70,15 @@ public static ISocketSession CreateSession(NetUri uri)
}

var client = Container.Resolve<ISocketSession>(uri.ProtocolType);
//if (uri.EndPoint != null)
//{
// //if (uri.ProtocolType == ProtocolType.Tcp) (client as ISocketClient).Connect(uri.EndPoint);
// if (client is ISocketClient && !uri.EndPoint.IsAny()) (client as ISocketClient).Connect(uri.EndPoint);
//}
if (uri.EndPoint != null)
{
//if (uri.ProtocolType == ProtocolType.Tcp) (client as ISocketClient).Connect(uri.EndPoint);
if (client is ISocketClient && !uri.EndPoint.IsAny()) //(client as ISocketClient).Connect(uri.EndPoint);
{
var socketClient = client as ISocketClient;
socketClient.Timeout = timeout;
}
}
client.Remote = uri;

return client;
Expand Down
2 changes: 1 addition & 1 deletion Test2/TestNewLife_Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void ServerSession_Received(object sender, ReceivedEventArgs e)
private ISocketSession CreateSession(string host, int port = 65530, ProtocolType protocolType = ProtocolType.Tcp)
{
var ip = NetHelper.ParseAddress(host);
var session = NetService.CreateSession(new NetUri(protocolType, ip, port));
var session = NetService.CreateSession(new NetUri(protocolType, ip, port), 5000);
//session.Received += ClientSession_Received;
return session;
}
Expand Down

0 comments on commit cbb3832

Please sign in to comment.