public void send(string cmdAndParams) { if (onCommandSent != null) { onCommandSent(new IRCCommandEvent(IRCCommand.fromLine(cmdAndParams))); } byte[] data = Encoding.UTF8.GetBytes(cmdAndParams + "\r\n"); try { stream.Write(data, 0, data.Length); } catch (SocketException ex) { handleException(ex, true); } catch (IOException ex) { handleException(ex, true); } }
public void update() { if (connected) { try { if (stream.CanRead) { //while (stream.DataAvailable) while (client.Available > 0) { int numBytes = stream.Read(buffer, 0, buffer.Length); if (numBytes > 0) { string text = Encoding.UTF8.GetString(buffer, 0, numBytes); textBuffer.Append(text); } } } } catch (SocketException ex) { handleException(ex, true); } if (textBuffer.Length > 0) { for (; ;) { int pos = textBuffer.ToString().IndexOf("\r\n"); if (pos >= 0) { string line = textBuffer.ToString().Substring(0, pos); textBuffer.Remove(0, pos + 2); if (onCommandReceived != null) { try { IRCCommand cmd = IRCCommand.fromLine(line); onCommandReceived(new IRCCommandEvent(cmd)); } catch (ArgumentException e) { Debug.LogException(e); } } } else { break; } } } // send something to socket to potentially trigger SocketException elsewhere when reading // off the socket long now = DateTime.UtcNow.Ticks / 10000L; if ((now - lastServerPing) >= SERVER_PING_INTERVAL) { lastServerPing = now; send("PING :" + now); } // only send auto joins if we've been connected for AUTO_JOIN_DELAY millis to allow time for post-connection stuff (user, nick) // and if AUTO_JOIN_TIME_BETWEEN_ATTEMPTS has elapsed since the last auto join happened, to avoid other join spam if (!autoJoinsSent && ((now - lastAutoJoinsSentTime) >= AUTO_JOIN_TIME_BETWEEN_ATTEMPTS) && ((now - connectTime) >= AUTO_JOIN_DELAY)) { autoJoinChannels(); } } }