/// <summary> /// Disconnects this client from the server if connected /// </summary> /// <returns>Did we disconnect successfully?</returns> protected bool InternalDisconnect() { if (!Connected) return false; try { byte[] SystemMessage = Encoding.UTF8.GetBytes(((uint)BlitziSocket.Protocol.SystemMessages.ClientClose).ToString()); SystemSocket.GetStream().Write(SystemMessage, 0, SystemMessage.Length); SystemSocket.Close(); return true; } catch (System.Exception e) { BlitziSocket.Exception.LowException Ex = new BlitziSocket.Exception.LowException("Could not disconnect from server", e); Ex.ErrorType = "WARN"; Ex.AdditionalInformation = new BlitziSocket.Exception.ExceptionInfo[] { new BlitziSocket.Exception.ExceptionInfo(new BlitziSocket.Exception.Info.ObjectInformation(SystemEndPoint)), new BlitziSocket.Exception.ExceptionInfo(new BlitziSocket.Exception.Info.ObjectInformation(SystemSocket)) }; throw Ex; } }
/// <summary> /// Listens for a socket and saves it in the list /// </summary> /// <returns>The newly connected TcpClient</returns> protected ServerTcpClient ListenForSocket() { // Wait until a client wants to connect while (!SystemListener.Pending()) ; int index = SystemClients.Count; try { SystemClients.Add(new ServerTcpClient(SystemListener.AcceptTcpClient(), NextID)); NextID++; return SystemClients[index]; } catch (System.Exception e) { BlitziSocket.Exception.LowException Ex = new BlitziSocket.Exception.LowException("Could not retrieve the connection of a new client", e); Ex.ErrorType = "WARN"; Ex.AdditionalInformation = new BlitziSocket.Exception.ExceptionInfo[] { new BlitziSocket.Exception.ExceptionInfo(new BlitziSocket.Exception.Info.ObjectInformation(SystemListener)), new BlitziSocket.Exception.ExceptionInfo(new BlitziSocket.Exception.Info.LongInformation(NextID - 1)) }; throw Ex; } }
/// <summary> /// Connects to the server /// </summary> /// <param name="EndPoint">The EndPoint of the server</param> /// <returns>Did we successfully connect?</returns> protected bool InternalConnect(IPEndPoint EndPoint) { SystemEndPoint = EndPoint; try { SystemSocket.Connect(SystemEndPoint); } catch (System.Exception e) { BlitziSocket.Exception.LowException Ex = new BlitziSocket.Exception.LowException("Could not connect to server", e); Ex.ErrorType = "ERROR"; Ex.AdditionalInformation = new BlitziSocket.Exception.ExceptionInfo[] { new BlitziSocket.Exception.ExceptionInfo(new BlitziSocket.Exception.Info.ObjectInformation(SystemEndPoint)) }; throw Ex; } return this.Connected; }