本文整理汇总了C#中ITcpChannel.Send方法的典型用法代码示例。如果您正苦于以下问题:C# ITcpChannel.Send方法的具体用法?C# ITcpChannel.Send怎么用?C# ITcpChannel.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITcpChannel
的用法示例。
在下文中一共展示了ITcpChannel.Send方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OnDecoderFailure
private void OnDecoderFailure(ITcpChannel channel, Exception error)
{
var pos = error.Message.IndexOfAny(new[] {'\r', '\n'});
var descr = pos == -1 ? error.Message : error.Message.Substring(0, pos);
var response = new HttpResponseBase(HttpStatusCode.BadRequest, descr, "HTTP/1.1");
channel.Send(response);
channel.Close();
}
示例2: OnDecoderFailure
private void OnDecoderFailure(ITcpChannel channel, Exception error)
{
var pos = error.Message.IndexOfAny(new[] {'\r', '\n'});
var descr = pos == -1 ? error.Message : error.Message.Substring(0, pos);
var response = new HttpResponse(HttpStatusCode.BadRequest, descr, "HTTP/1.1");
var counter = (int)channel.Data.GetOrAdd(HttpMessage.PipelineIndexKey, x => 1);
response.Headers[HttpMessage.PipelineIndexKey] = counter.ToString();
channel.Send(response);
channel.Close();
}
示例3: ExecuteConnectModules
private async Task ExecuteConnectModules(ITcpChannel channel, IClientContext context)
{
var result = ModuleResult.Continue;
for (var i = 0; i < _modules.Length; i++)
{
var connectMod = _modules[i] as IConnectionEvents;
if (connectMod == null)
continue;
try
{
result = await connectMod.OnClientConnected(context);
}
catch (Exception exception)
{
context.Error = exception;
result = ModuleResult.SendResponse;
}
if (result != ModuleResult.Continue)
break;
}
switch (result)
{
case ModuleResult.Disconnect:
channel.Close();
break;
case ModuleResult.SendResponse:
channel.Send(context.ResponseMessage);
break;
}
}
示例4: ExecuteModules
private async Task ExecuteModules(ITcpChannel channel, ClientContext context)
{
var result = ModuleResult.Continue;
for (var i = 0; i < _modules.Length; i++)
{
try
{
await _modules[i].BeginRequestAsync(context);
}
catch (Exception exception)
{
context.Error = exception;
result = ModuleResult.SendResponse;
}
}
if (result == ModuleResult.Continue)
{
for (var i = 0; i < _modules.Length; i++)
{
try
{
result = await _modules[i].ProcessAsync(context);
}
catch (Exception exception)
{
context.Error = exception;
result = ModuleResult.SendResponse;
}
if (result != ModuleResult.Continue)
break;
}
}
try
{
await EndRequestAsync(context);
}
catch (Exception exception)
{
if (context.ResponseMessage == null)
context.ResponseMessage = exception;
result = ModuleResult.Disconnect;
}
if (context.ResponseMessage != null)
channel.Send(context.ResponseMessage);
if (result == ModuleResult.Disconnect)
{
channel.Close();
}
}
示例5: OnMessage
/// <summary>
/// Handles the upgrade
/// </summary>
/// <param name="source">Channel that we've received a request from</param>
/// <param name="msg">Message received.</param>
protected override void OnMessage(ITcpChannel source, object msg)
{
var httpMessage = msg as IHttpMessage;
if (WebSocketUtils.IsWebSocketUpgrade(httpMessage))
{
if (httpMessage is IHttpRequest) // server mode
{
var args = new WebSocketClientConnectEventArgs(source, (IHttpRequest) httpMessage);
WebSocketClientConnect(this, args);
if (args.MayConnect)
{
var webSocketKey = httpMessage.Headers["Sec-WebSocket-Key"];
// TODO: why not provide the response in the WebSocketClientConnectEventArgs event?
var response = new WebSocketUpgradeResponse(webSocketKey);
source.Send(response);
WebSocketClientConnected(this,
new WebSocketClientConnectedEventArgs(source, (IHttpRequest) httpMessage, response));
}
else if (args.SendResponse)
{
var response = new HttpResponseBase(HttpStatusCode.NotImplemented, "Not Implemented", "HTTP/1.1");
if (args.Response != null)
response.Body = args.Response;
source.Send(response);
}
return;
}
if (httpMessage is IHttpResponse) // client mode
{
WebSocketClientConnected(this,
new WebSocketClientConnectedEventArgs(source, null, (IHttpResponse) httpMessage));
}
}
var webSocketMessage = msg as IWebSocketMessage;
if (webSocketMessage != null)
{
// standard message responses handled by listener
switch (webSocketMessage.Opcode)
{
case WebSocketOpcode.Ping:
source.Send(new WebSocketMessage(WebSocketOpcode.Pong, webSocketMessage.Payload));
return;
case WebSocketOpcode.Close:
source.Send(new WebSocketMessage(WebSocketOpcode.Close));
source.Close();
WebSocketClientDisconnected(this,
new ClientDisconnectedEventArgs(source, new Exception("WebSocket closed")));
return;
}
_webSocketMessageReceived(source, webSocketMessage);
return;
}
base.OnMessage(source, msg);
}
示例6: Process
/// <summary>
/// authenticate using serialized messages
/// </summary>
/// <param name="channel">channel to authenticate</param>
/// <param name="message">Received message, will be <see cref="AuthenticationRequiredException" /> first time and then responses to your authentication messages</param>
/// <returns>
/// <c>true</c> if authentication process completed.
/// </returns>
public bool Process(ITcpChannel channel, object message)
{
if (message is AuthenticationRequiredException)
{
var handshake = _authenticationMessageFactory.CreateHandshake(_credentials.UserName);
channel.Send(handshake);
_state = 1;
AuthenticationFailed = false;
return false;
}
if (message is AuthenticationHandshakeReply)
{
if (_state != 1)
{
AuthenticationFailed = true;
return true;
}
_state = 2;
var handshakeReply = (AuthenticationHandshakeReply)message;
_passwordHash = _passwordHasher.HashPassword(_credentials.Password, handshakeReply.AccountSalt);
var token = _passwordHasher.HashPassword(_passwordHash, handshakeReply.SessionSalt);
var auth = _authenticationMessageFactory.CreateAuthentication(token);
_clientSalt = auth.ClientSalt;
channel.Send(auth);
return false;
}
if (message is AuthenticateReply)
{
if (_state != 2)
{
AuthenticationFailed = true;
return true;
}
var result = (AuthenticateReply)message;
AuthenticationFailed = true;
if (result.State == AuthenticateReplyState.Success)
{
var ourToken = _passwordHasher.HashPassword(_passwordHash, _clientSalt);
if (!_passwordHasher.Compare(ourToken, result.AuthenticationToken))
return true;
}
else
return true;
AuthenticationFailed = false;
return true;
}
return false;
}
示例7: AuthenticateUser
private bool AuthenticateUser(ITcpChannel channel, IHttpRequest request)
{
if (channel.Data["Principal"] != null)
{
Thread.CurrentPrincipal = (IPrincipal)channel.Data["Principal"];
return true;
}
try
{
var user = AuthenticationService.Authenticate(request);
if (user == null)
return true;
if (PrincipalFactory != null)
{
var ctx = new PrincipalFactoryContext(request, user);
Thread.CurrentPrincipal = PrincipalFactory.Create(ctx);
channel.Data["Principal"] = Thread.CurrentPrincipal;
return true;
}
var roles = user as IUserWithRoles;
if (roles == null)
throw new InvalidOperationException(
"You must specify a PrincipalFactory if you do not return a IUserWithRoles from your IAccountService.");
Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user.Username), roles.RoleNames);
channel.Data["Principal"] = Thread.CurrentPrincipal;
}
catch (HttpException ex)
{
var response = request.CreateResponse();
response.StatusCode = ex.HttpCode;
response.ReasonPhrase = FirstLine(ex.Message);
;
channel.Send(response);
return false;
}
return true;
}
示例8: OnWebsocketMessage
private void OnWebsocketMessage(ITcpChannel channel, object message)
{
var request = (WebSocketRequest)message;
if (request.Opcode != WebSocketOpcode.Text)
{
Logger("Unexpected msg");
return;
}
var streamReader = new StreamReader(request.Payload);
var data = streamReader.ReadToEnd();
var pos = data.IndexOf(':');
if (pos == -1 || pos > 50)
{
Logger("cqsObjectName is missing.");
return;
}
var cqsName = data.Substring(0, pos);
var json = data.Substring(pos + 1);
Type type;
if (!_cqsTypes.TryGetValue(cqsName, out type))
{
var response = CreateWebSocketResponse(@"{ ""error"": ""Unknown type: " + cqsName + "\" }");
channel.Send(response);
Logger("Unknown type: " + cqsName + ".");
return;
}
var cqs = SimpleJson.DeserializeObject(json, type);
ClientResponse cqsReplyObject;
try
{
cqsReplyObject = _messageProcessor.ProcessAsync(cqs).Result;
}
catch (HttpException ex)
{
var responseJson = SimpleJson.SerializeObject(new
{
error = ex.Message,
statusCode = ex.HttpCode
});
var response = CreateWebSocketResponse(responseJson);
channel.Send(response);
return;
}
catch (Exception ex)
{
var responseJson = SimpleJson.SerializeObject(new
{
error = ex.Message,
statusCode = 500
});
var response = CreateWebSocketResponse(responseJson);
channel.Send(response);
return;
}
if (cqsReplyObject.Body is Exception)
{
var responseJson = SimpleJson.SerializeObject(new
{
error = ((Exception)cqsReplyObject.Body).Message,
statusCode = 500
});
var response = CreateWebSocketResponse(responseJson);
channel.Send(response);
}
else
{
json = SimpleJson.SerializeObject(cqsReplyObject.Body);
var reply = CreateWebSocketResponse(json);
channel.Send(reply);
}
}
示例9: OnMessage
private void OnMessage(ITcpChannel channel, object message)
{
var request = (HttpRequest) message;
if (_requestFilter != null)
{
var resp = _requestFilter(channel, request);
if (resp != null)
{
channel.Send(resp);
return;
}
}
var name = request.Headers["X-Cqs-Type"];
var dotNetType = request.Headers["X-Cqs-Object-Type"];
var cqsName = request.Headers["X-Cqs-Name"];
if (Authenticator != null)
{
if (!AuthenticateUser(channel, request))
return;
}
var json = "{}";
if (request.Body != null)
{
var reader = new StreamReader(request.Body);
json = reader.ReadToEnd();
}
object cqsObject;
if (!string.IsNullOrEmpty(dotNetType))
{
cqsObject = _cqsObjectMapper.Deserialize(dotNetType, json);
if (cqsObject == null)
{
var response = request.CreateResponse();
response.StatusCode = 400;
response.ReasonPhrase = "Unknown type: " + dotNetType;
Logger("Unknown type: " + dotNetType + " for " + request.Uri);
channel.Send(response);
return;
}
}
else if (!string.IsNullOrEmpty(cqsName))
{
cqsObject = _cqsObjectMapper.Deserialize(cqsName, json);
if (cqsObject == null)
{
var response = request.CreateResponse();
response.StatusCode = 400;
response.ReasonPhrase = "Unknown type: " + cqsName;
Logger("Unknown type: " + cqsName + " for " + request.Uri);
channel.Send(response);
return;
}
}
else
{
var response = request.CreateResponse();
response.StatusCode = 400;
response.ReasonPhrase =
"Expected a class name in the header 'X-Cqs-Name' or a .NET type name in the header 'X-Cqs-Object-Type'.";
Logger(
"Expected a class name in the header 'X-Cqs-Name' or a .NET type name in the header 'X-Cqs-Object-Type' for " +
request.Uri);
channel.Send(response);
return;
}
ClientResponse cqsReplyObject = null;
Exception ex = null;
try
{
cqsReplyObject = _messageProcessor.ProcessAsync(cqsObject).Result;
}
catch (AggregateException e1)
{
ex = e1.InnerException;
}
if (ex is HttpException)
{
Logger("Failed to process " + json + ", Exception:\r\n" + ex);
var response = request.CreateResponse();
response.StatusCode = ((HttpException) ex).HttpCode;
response.ReasonPhrase = FirstLine(ex.Message);
channel.Send(response);
return;
}
if (ex is AuthorizationException)
{
Logger("Failed to process " + json + ", Exception:\r\n" + ex);
var authEx = (AuthorizationException)ex;
var response = request.CreateResponse();
response.StatusCode = 401;
response.ReasonPhrase = FirstLine(ex.Message);
channel.Send(response);
return;
//.........这里部分代码省略.........