/// <summary> /// Handle new connection messages /// </summary> /// <param name="connection">Connection that send the message</param> /// <param name="data">Message raw data</param> private void NewConnectionHandler(InputConnection connection, MemoryStream data) { try { List <Packet> messages = PacketHandler.Handle(data); Packet firstMessage = messages.FirstOrDefault(); _logger.Trace($"Client => Server: {firstMessage?.ToString()}"); switch (firstMessage?.Type) // FIX: RequestID is always an only packet? { case PacketType.HttpRequest: if (this._httpServerPort > 0) { connection.Send(Encoding.ASCII.GetBytes( "HTTP/1.1 301 Moved Permanently\nLocation: http://" + this._localEndPoint.Address.ToString() + (this._httpServerPort != 80 ? ":" + this._httpServerPort.ToString() : "") + "\nConnection: close\n\n")); // TODO: Set public IP or domain } else { connection.Send(Encoding.ASCII.GetBytes( "HTTP/1.1 503 Service Unavailable\nConnection: close\n\nThis is not a web server")); } connection.Disconnect(); break; case PacketType.RequestID: RequestIdPacket reqId = firstMessage as RequestIdPacket; Player newPlayer = new Player(reqId?.Name, connection); // TODO: Check bans. connection.Send(ResponseIdPacket.RejectPlayer().ToBinary()); this.AddPlayer(newPlayer); break; default: _logger.Warning( $"Packet not expected on new connection from {connection.Ip}: {Enum.GetName(typeof(PacketType), firstMessage?.Type)}"); break; } if (messages.Count > 1) { _logger.Warning("Multiple messages on new connection not expected."); } } catch (UnrecognizedPacketException ex) { _logger.Warning($"Unrecognized packet from client {connection.Ip}: {ex.Message}"); connection.Disconnect(); } catch (VersionNotFoundException ex) { _logger.Warning($"Protocol version mismatch from client {connection.Ip}: {ex.Message}"); connection.Disconnect(); } // Remove new connection handler. This isn't new already. connection.OnMessage -= this._newConnectionEventHandler; }
/// <summary> /// Check that the data can be used and read. /// </summary> /// <returns></returns> /// <summary> /// Is the supplied input data reachable? /// Does the File exists. /// </summary> /// <returns>true - its reachable : otherwise false</returns> internal virtual bool CheckDataReachable() { if (InputConnection.IndexOf(".") == -1) { Problem = new Exception(string.Format(FileInputConstants.NoExtension, InputConnection)); return(false); } // // No Backslash then it needs the app path. // if (InputConnection.Contains("\\") == false) { _filefullPath = AppDomain.CurrentDomain.BaseDirectory + "\\" + InputConnection; } else { _filefullPath = InputConnection; } if (!File.Exists(_filefullPath)) { Problem = new Exception(string.Format(FileInputConstants.FileMissing, InputConnection)); return(false); } if (InPutType.CompareTo(OutPutType).Equals(0)) { Problem = new Exception(string.Format(Constants.Conversion.InputOutputSame, InputConnection)); return(false); } return(true); }
/// <summary> /// The deep copy constructor. /// </summary> /// <param name="source">Source instance</param> public InputConnection(InputConnection source) { FieldIdx = source.FieldIdx; PoolID = source.PoolID; Density = source.Density; SynapseCfg = source.SynapseCfg.DeepClone(); return; }
/// <summary> /// Handle new connections /// </summary> /// <param name="connection">New connection</param> protected override void OnConnection(InputConnection connection) { // TODO: Save active connection list // Set new connection handler if (this._newConnectionEventHandler == null) { this._newConnectionEventHandler = new MessageEventHandler(NewConnectionHandler); } connection.OnMessage += this._newConnectionEventHandler; }
/// <summary> /// See the base. /// </summary> public override bool Equals(object obj) { if (obj == null) { return(false); } InputConnection cmpSettings = obj as InputConnection; if (FieldIdx != cmpSettings.FieldIdx || PoolID != cmpSettings.PoolID || Density != cmpSettings.Density || !Equals(SynapseCfg, cmpSettings.SynapseCfg) ) { return(false); } return(true); }
/// <summary> /// The deep copy constructor. /// </summary> /// <param name="source">Source instance</param> public InputConnection(InputConnection source) { FieldIdx = source.FieldIdx; PoolID = source.PoolID; Density = source.Density; SynapseCfg = null; if (source.SynapseCfg != null) { if (source.SynapseCfg.GetType() == typeof(StaticSynapseSettings)) { SynapseCfg = ((StaticSynapseSettings)source.SynapseCfg).DeepClone(); } else { SynapseCfg = ((DynamicSynapseSettings)source.SynapseCfg).DeepClone(); } } return; }
/// <summary> /// Check if it is a File Conversion. /// Does the File Exist /// </summary> /// <param name="args"></param> /// <returns></returns> public bool CheckItsTypeConversion(string[] args) { if (args[0].StartsWith("F\\") == false | args[1].StartsWith("O\\") == false) { return(false); } if (args[0].Length < 4 | args[1].Length < 4) { return(false); } InputConnection = args[0].Substring(2).ToLower(); int iindex = InputConnection.IndexOf("."); if (iindex > -1) { InPutType = InputConnection.Substring(iindex + 1); } OutPutFileName = args[1].Substring(2).ToLower(); iindex = OutPutFileName.IndexOf("."); if (iindex == -1) { OutPutType = OutPutFileName; OutPutFileName = InputConnection; } else { OutPutType = OutPutFileName.Substring(iindex + 1); OutPutFileName = OutPutFileName.Substring(0, OutPutFileName.Length - OutPutType.Length); } if (args.Length > 2 && args[2].StartsWith("P\\") && args[2].Length > 2) { InputPatternType = args[2].Substring(2).ToLower(); } return(true); }
// The activity (or some parent or controller) must give us // a reference to the current EditText's InputConnection public void setInputConnection(InputConnection ic) { this.inputConnection = ic; }