| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Text;
|
|---|
| 4 | using System.Threading;
|
|---|
| 5 | using System.IO;
|
|---|
| 6 | using System.Net;
|
|---|
| 7 | using System.Net.Sockets;
|
|---|
| 8 |
|
|---|
| 9 | using Circuit.Net.Irc;
|
|---|
| 10 |
|
|---|
| 11 | namespace Misuzilla.Applications.Mobile.Criw0
|
|---|
| 12 | {
|
|---|
| 13 | class IRCConnection : IDisposable
|
|---|
| 14 | {
|
|---|
| 15 | private Boolean _retryOnDisconnected = false;
|
|---|
| 16 | private TcpClient _tcpClient;
|
|---|
| 17 | private StreamReader _streamReader;
|
|---|
| 18 | private StreamWriter _streamWriter;
|
|---|
| 19 |
|
|---|
| 20 | private Boolean _connected = false;
|
|---|
| 21 | private Thread _runner;
|
|---|
| 22 |
|
|---|
| 23 | public event EventHandler<MessageReceivedEventArgs> MessageReceived;
|
|---|
| 24 | public void OnMessageReceived(IRCMessage m)
|
|---|
| 25 | {
|
|---|
| 26 | if (MessageReceived != null)
|
|---|
| 27 | {
|
|---|
| 28 | MessageReceived(this, new MessageReceivedEventArgs(m));
|
|---|
| 29 | }
|
|---|
| 30 | }
|
|---|
| 31 | public event EventHandler Connected;
|
|---|
| 32 | protected void OnConnected()
|
|---|
| 33 | {
|
|---|
| 34 | if (Connected != null)
|
|---|
| 35 | {
|
|---|
| 36 | Connected(this, new EventArgs());
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 | public event EventHandler Connecting;
|
|---|
| 40 | protected void OnConnecting()
|
|---|
| 41 | {
|
|---|
| 42 | if (Connecting != null)
|
|---|
| 43 | {
|
|---|
| 44 | Connecting(this, new EventArgs());
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | public event EventHandler Disconnected;
|
|---|
| 48 | protected void OnDisconnected()
|
|---|
| 49 | {
|
|---|
| 50 | if (Disconnected != null)
|
|---|
| 51 | {
|
|---|
| 52 | Disconnected(this, new EventArgs());
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public Boolean IsConnected
|
|---|
| 57 | {
|
|---|
| 58 | get { return _connected; }
|
|---|
| 59 | }
|
|---|
| 60 | public Boolean RetryOnDisconnected
|
|---|
| 61 | {
|
|---|
| 62 | get { return _retryOnDisconnected; }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | public void Connect(String host, Int32 port, String userName, String password, String nickName, String userInfo)
|
|---|
| 67 | {
|
|---|
| 68 | if (_connected)
|
|---|
| 69 | {
|
|---|
| 70 | throw new ApplicationException("���łɐڑ����m���������܂�");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | OnConnecting();
|
|---|
| 74 | _tcpClient = new TcpClient();
|
|---|
| 75 | try
|
|---|
| 76 | {
|
|---|
| 77 | _tcpClient.Connect(host, port);
|
|---|
| 78 | _connected = true;
|
|---|
| 79 | Encoding enc = Encoding.GetEncoding("ISO-2022-JP");
|
|---|
| 80 | _streamReader = new StreamReader(_tcpClient.GetStream(), enc);
|
|---|
| 81 | _streamWriter = new StreamWriter(_tcpClient.GetStream(), enc);
|
|---|
| 82 | Send(String.Format("USER {0} * * :{1}", userName, (userInfo.Length > 0 ? userInfo : "-")));
|
|---|
| 83 | if (password.Length > 0)
|
|---|
| 84 | {
|
|---|
| 85 | Send(String.Format("PASS {0}", password));
|
|---|
| 86 | }
|
|---|
| 87 | Send(String.Format("NICK {0}", nickName));
|
|---|
| 88 | }
|
|---|
| 89 | catch (SocketException socketEx)
|
|---|
| 90 | {
|
|---|
| 91 | _connected = false;
|
|---|
| 92 | OnDisconnected();
|
|---|
| 93 | return;
|
|---|
| 94 | }
|
|---|
| 95 | catch (IOException)
|
|---|
| 96 | {
|
|---|
| 97 | _connected = false;
|
|---|
| 98 | OnDisconnected();
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | OnConnected();
|
|---|
| 102 |
|
|---|
| 103 | _runner = new Thread(new ThreadStart(Runner));
|
|---|
| 104 | _runner.Start();
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public void Send(String rawMessage)
|
|---|
| 108 | {
|
|---|
| 109 | if (!_connected)
|
|---|
| 110 | {
|
|---|
| 111 | throw new ApplicationException("�ڑ����m���������܂���
|
|---|
| 112 | }
|
|---|
| 113 | try
|
|---|
| 114 | {
|
|---|
| 115 | _streamWriter.WriteLine(rawMessage);
|
|---|
| 116 | _streamWriter.Flush();
|
|---|
| 117 | }
|
|---|
| 118 | catch (IOException)
|
|---|
| 119 | {
|
|---|
| 120 | Close();
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | public void Disconnect(String quitMessage)
|
|---|
| 125 | {
|
|---|
| 126 | if (!_connected) return;
|
|---|
| 127 | // TODO: ���g���C���Ȃ��悤�Ɂ�Send������悤�� |
|---|
| 128 | _connected = false;
|
|---|
| 129 | _streamWriter.WriteLine("QUIT :"+quitMessage);
|
|---|
| 130 |
|
|---|
| 131 | try
|
|---|
| 132 | {
|
|---|
| 133 | if (_streamWriter.BaseStream.CanWrite)
|
|---|
| 134 | {
|
|---|
| 135 | _streamWriter.Flush();
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | catch (IOException) { }
|
|---|
| 139 |
|
|---|
| 140 | Close();
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | private delegate void MessageReceivedDelegate(String line);
|
|---|
| 144 | private void Runner()
|
|---|
| 145 | {
|
|---|
| 146 | String line;
|
|---|
| 147 | try
|
|---|
| 148 | {
|
|---|
| 149 | while ((line = _streamReader.ReadLine()) != null)
|
|---|
| 150 | {
|
|---|
| 151 | try
|
|---|
| 152 | {
|
|---|
| 153 | OnMessageReceived(IRCMessage.CreateMessage(line));
|
|---|
| 154 | }
|
|---|
| 155 | catch (IRCInvalidMessageException e)
|
|---|
| 156 | {
|
|---|
| 157 | foreach (String l in e.ToString().Split(new Char[] { '\n' })) {
|
|---|
| 158 | NoticeMessage n = new NoticeMessage();
|
|---|
| 159 | n.Content = l;
|
|---|
| 160 | n.Sender = "_Internal";
|
|---|
| 161 | n.IsServerMessage = true;
|
|---|
| 162 | OnMessageReceived(n);
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 | catch (IOException) { }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | public void Close()
|
|---|
| 171 | {
|
|---|
| 172 | if (_tcpClient != null)
|
|---|
| 173 | {
|
|---|
| 174 | _runner.Abort();
|
|---|
| 175 | _streamReader.Close();
|
|---|
| 176 | _streamWriter.Close();
|
|---|
| 177 | _tcpClient.Close();
|
|---|
| 178 | _tcpClient = null;
|
|---|
| 179 | _connected = false;
|
|---|
| 180 | }
|
|---|
| 181 | OnDisconnected();
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 | #region IDisposable �����o
|
|---|
| 186 |
|
|---|
| 187 | public void Dispose()
|
|---|
| 188 | {
|
|---|
| 189 | Close();
|
|---|
| 190 | GC.SuppressFinalize(this);
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | #endregion
|
|---|
| 194 |
|
|---|
| 195 | public class MessageReceivedEventArgs : EventArgs
|
|---|
| 196 | {
|
|---|
| 197 | private IRCMessage _m;
|
|---|
| 198 | public MessageReceivedEventArgs(IRCMessage m)
|
|---|
| 199 | {
|
|---|
| 200 | _m = m;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | public IRCMessage Message
|
|---|
| 204 | {
|
|---|
| 205 | get { return _m; }
|
|---|
| 206 | set { _m = value; }
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|