private void Client_MessageReceived(object sender, MessageReceivedEventArgs e) { ClientBase client = (ClientBase)sender; int clientId = clientTable[client]; MessageFromClientEventArgs args = new MessageFromClientEventArgs(e.RawMessage, client, clientId); MessageFromClient?.Invoke(this, args); }
public MessageReceivedEventArgs(string msg) { try { message = new MessageFromClient(); string[] words = msg.Split(new [] { ':' }); message.Ip = words[0]; message.Message = words[1]; } catch (Exception) { } }
private async Task OnMessageAsync(MessageFromClient message) { try { var networkCommand = JsonUtility.FromJson <NetworkCommand>(message.data); if (networkCommand.commandInternalIsResponse) { _connection.ResolveTransaction(networkCommand, message.data); } else { try { var responseToClient = await _commands.ProcessIncomingMessage(_connection, networkCommand, message.data, _master, message.client_id); responseToClient.commandInternalType = NetworkCommand.CommandTypeFor(responseToClient.GetType()); responseToClient.commandInternalIsResponse = true; responseToClient.commandInternalId = networkCommand.commandInternalId; await _connection.Send(responseToClient, message.client_id); } catch (Exception error) { _manager.Logger.OnError(error); await _connection.Send(new NetworkCommandError() { message = error.Message, commandInternalType = NetworkCommand.CommandTypeFor(typeof(NetworkCommandError)), commandInternalIsResponse = true, commandInternalId = networkCommand.commandInternalId }, message.client_id); } } } catch (Exception error) { _manager.Logger.OnError(error); } }
/// <summary> /// Get message from client /// </summary> public void Start() { tcpSocket.Bind(tcpEndpoint); tcpSocket.Listen(1); while (true) { Socket Listener = tcpSocket.Accept(); byte[] receivedBytes = new byte[128]; var size = 0; var data = new StringBuilder(); do { size = Listener.Receive(receivedBytes); data.Append(Encoding.UTF8.GetString(receivedBytes, 0, size)); } while (Listener.Available > 0); MessageFromClient?.Invoke(data.ToString()); Listener.Shutdown(SocketShutdown.Both); Listener.Close(); } }
/// <param name="args"></param> private static void Main(string[] args) { ConsoleEx.WriteAppCaption(); //--- Analyzujeme příkazovou řádku var cmdLine = new MyCommandLine(args); //je help? if (cmdLine.ExistsAny(new[] { PAR_HELP_1, PAR_HELP_2, PAR_HELP_3 })) { ShowHelp(); return; } var pipeName = cmdLine.Value(PAR_PIPE_NAME); if (string.IsNullOrEmpty(pipeName)) { ConsoleEx.WriteLine("Missing name of pipe."); ShowHelp(); return; } MessageFromClient message = null; try { message = GetMessage(cmdLine); } catch (Exception ex) { ConsoleEx.WriteError(ex); ShowHelp(); return; } if (message == null) { ConsoleEx.WriteLine("Missing command."); ShowHelp(); return; } try { using (var pipeClient = new NamedPipeClientStream( "." , pipeName , PipeDirection.InOut , PipeOptions.None , TokenImpersonationLevel.Impersonation)) { pipeClient.Connect(500); ConsoleEx.WriteLine($"Connect to pipe '{pipeName}' successed."); var streamString = new StreamString(pipeClient); streamString.WriteString(JsonConvert.SerializeObject(message)); do { ConsoleEx.WriteLine(ConsoleColor.White, streamString.ReadString()); } while (pipeClient.IsConnected); pipeClient.Close(); return; } } catch (Exception ex) { ConsoleEx.WriteError(ex); ConsoleEx.WriteError($"Connect to {pipeName} failed."); } //--- Debug.WriteLine($"Connect to {pipeName} failed."); }
public void OnMessage(MessageFromClient message) { OnMessageAsync(message).Promise().Dispatch(); }