internal bool Write(CTcpData data) { if (m_sock == null) { m_error = "socket closed"; return(false); } int bytestowrite = data.GetSize(); int byteswritten = 0; //loop until we've written all bytes while (byteswritten < bytestowrite) { //wait until socket becomes writeable bool returnv = WaitForSocket(true, "Write"); if (!returnv) { return(returnv); } int size = m_sock.Send(data.GetData(), byteswritten, data.GetSize() - byteswritten, SocketFlags.None, out SocketError sockError); if (size == -1) { m_error = "send() " + m_address + ":" + m_port + " " + sockError; return(false); } byteswritten += size; } return(true); }
public void Process() { //open listening socket if it's not already if (!m_socket.IsOpen) { string liseningAddress = String.IsNullOrEmpty(m_address) ? "*" : m_address; Util.Log($"opening listening socket on {liseningAddress}:{m_port}"); if (!m_socket.Open(m_address, m_port, 1000000)) { Util.LogError($"{m_socket.GetError()}"); m_socket.Close(); } } //see if there's a socket we can read from IList <Socket> sockets = GetReadableFd(); foreach (Socket sock in sockets) { if (sock == m_socket.GetSock()) //we can read from the listening socket { CClient client = new CClient(); bool returnv = m_socket.Accept(client.m_socket); if (returnv) { Util.Log($"{client.m_socket.Address}:{client.m_socket.Port} connected"); AddClient(client); } else { client.Dispose(); client = null; Util.Log(m_socket.GetError()); } } else { //get the client the sock fd belongs to CClient client = GetClientFromSock(sock); if (client == null) //guess it belongs to nobody { continue; } //try to read data from the client CTcpData data = new CTcpData(); bool returnv = client.m_socket.Read(data); if (!returnv) { //socket broke probably Util.Log(client.m_socket.GetError()); RemoveClient(client); continue; } //add data to the messagequeue client.m_messagequeue.AddData(data.GetData(), data.GetSize()); //check messages from the messaqueue and parse them, if it fails remove the client if (!HandleMessages(client)) { RemoveClient(client); } } } }