private void MessagingClient_MessageReceived(object sender, MessageReceivedEventArgs e) { string text = e.GetText(); int chatBoxFieldTextLength = chatBoxField.Text.Length; string msg = _logic.ParseMessage(text, chatBoxFieldTextLength); if (msg != null) { Invoke(new Action<string>(chatWindowControl.AppendTextBox), new object[] { msg }); } else { Invoke(new Action(InitDataTable)); } }
void RequestParser_messageReceived(object sender, MessageReceivedEventArgs e) { string text = e.GetText(); byte[] byteCode = Convert.FromBase64String(text); BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(byteCode); String msg = formatter.Deserialize(stream) as String; if (chatBoxField.Text.Length > 0) { msg = "\r\n" + msg; } Invoke(new Action<string>(AppendTextBox), new object[] { msg }); }
private void ClientListener_MessageReceived(object sender, MessageReceivedEventArgs e) { }
private static void ReceiveCallback(IAsyncResult ar) { try { // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.WorkSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); bool endOfStream = true; //check if end of stream if (bytesRead >= 10) { for (byte i = 0; i < 10; i++) { if (state.Buffer[bytesRead - 1 - i] != 9 - i) { endOfStream = false; break; } } } else { for (byte i = 0; i < bytesRead; i++) { if (state.Buffer[bytesRead - 1 - i] != 9 - i) { endOfStream = false; break; } } for (byte i = 0; i < 10 - bytesRead; i++) { if (_tmpBuf[_tmpBuf.Count - 1 - i] != 9 - bytesRead - i) { endOfStream = false; break; } } } if (bytesRead > 0) { // There might be more data, so store the data received so far. state.Sb.Append(Encoding.ASCII.GetString(state.Buffer, 0, bytesRead)); for (var i = 0; i < bytesRead; i++) { _tmpBuf.Add(state.Buffer[i]); } if (endOfStream) { _tmpBuf.RemoveRange(_tmpBuf.Count - 10, 10); if (state.Sb.Length > 1) { _response = new byte[_tmpBuf.Count]; _response = _tmpBuf.ToArray(); _tmpBuf.Clear(); } // Signal that all bytes have been received. _receiveDone.Set(); //when sending message to the client who just logged in, //NullReferenceException is thrown because the not all objects are //constructed at that point. That client gets up-to-date settings //because he gets them after logging in try { string base64Response = Convert.ToBase64String(_response); MessageReceivedEventArgs args = new MessageReceivedEventArgs(base64Response); MessageReceived.Invoke(null, args); } catch (NullReferenceException exc) { Console.WriteLine("Wut I just logged in"); } } // Get the rest of the data. else { client.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveCallback, state); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
protected virtual void OnMessageReceived(MessageReceivedEventArgs e) { MessageReceived?.Invoke(this, e); }