Example #1
0
 public void Talk(ChatMessage message) {
     if (_ns == null || !_ns.CanWrite) {
         return;
     }
     var bytes = message.ToBytes();
     _ns.Write(bytes, 0, bytes.Length);
 }
Example #2
0
 public static ChatMessage FromBytes(byte[] bytes)
 {
     var ret = new ChatMessage();
     using (var stream = new MemoryStream(bytes))
     using (var reader = new BinaryReader(stream, Encoding.UTF8)) {
         ret.Command = (Commands)reader.ReadInt16();
         ret.Sender = reader.ReadString();
         ret.Body = reader.ReadString();
         var iconLength = reader.ReadInt32();
         if (iconLength > 0) {
             ret.Icon = reader.ReadBytes(iconLength);
         }
     }
     return ret;
 }
 public MessageReceivedEventArgs(ChatMessage message)
 {
     Message = message;
 }
Example #4
0
 private void HandleClient(Object state) {
     using (var client = _listener.AcceptTcpClient())
     using (var ns = client.GetStream()) {
         if (ns.CanTimeout) {
             ns.ReadTimeout = Timeout * 1000;
             ns.WriteTimeout = Timeout * 1000;
         }
         var endPoint = client.Client.RemoteEndPoint as IPEndPoint;
         var clientAddress = endPoint != null ?
             endPoint.Address.ToString() + ":" + endPoint.Port.ToString() :
             null;
         Message = "Connected: " + clientAddress;
         try {
             var isDisconnected = false;
             while (!isDisconnected &&
                 client.GetState() == TcpState.Established &&
                 _listener.Active) {
                 Thread.Sleep(100);
                 if (!ns.DataAvailable) {
                     continue;
                 }
                 ChatMessage message = new ChatMessage();
                 using (var ms = new MemoryStream()) {
                     var receiveBuffer = new byte[BufferSize];
                     do {
                         var receiveSize = ns.Read(receiveBuffer, 0, receiveBuffer.Length);
                         if (receiveSize == 0) {
                             isDisconnected = true;
                             break;
                         }
                         ms.Write(receiveBuffer, 0, receiveSize);
                     } while (ns.DataAvailable);
                     if (!isDisconnected) {
                         message = ChatMessage.FromBytes(ms.GetBuffer());
                     }
                 }
                 var response = this.NotifyMessageReceived(message);
                 if (!isDisconnected) {
                     var bytes = response.ToBytes();
                     ns.Write(bytes, 0, bytes.Length);
                 }
             }
         }
         catch (Exception ex) {
             var socketException = ex.InnerException as SocketException;
             var message = socketException == null ?
                 ex.GetAllMessages() :
                 ((SocketError)socketException.ErrorCode).ToString() + ": " + clientAddress;
             Message = message;
         }
         Message = "Disconnected: " + clientAddress;
     }
 }
 public static ChatMessage NotifyMessageReceived(this INotifyMessageReceived sender, ChatMessage message)
 {
     var e = new MessageReceivedEventArgs(message);
     sender.NotifyMessageReceived(e);
     return e.Response;
 }