public static void Send(Socket sock, Message message) { byte[] buffer = message.GetBytes(); for (int sent = 0; sent < buffer.Length; ) { sent += sock.Send(buffer, sent, buffer.Length - sent, SocketFlags.None); } }
private void CreateClick(object sender, RoutedEventArgs e) { //send create request for that room to server SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.CreateRoom, Message.Encoding.GetBytes(InputBox.Text)); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(RoomJoined); if (!App.ConnectedSocket.SendAsync(args)) RoomJoined(null, args); }
protected override void OnRemovedFromJournal(JournalEntryRemovedEventArgs e) { //send a leave room request SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.LeaveRoom, null); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(MessagePosted); if (!App.ConnectedSocket.SendAsync(args)) MessagePosted(null, args); //continue with base base.OnRemovedFromJournal(e); }
private void PostClick(object sender, RoutedEventArgs e) { string post = InputBox.Text; InputBox.Text = string.Empty; SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.MessagePost, Message.Encoding.GetBytes(post)); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(MessagePosted); if (!App.ConnectedSocket.SendAsync(args)) MessagePosted(null, args); }
private void LogInClick(object sender, RoutedEventArgs e) { Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); SocketAsyncEventArgs args = new SocketAsyncEventArgs(); // set up endpoint for connect int split = this.Host.Text.IndexOf(':'); int port; if (!int.TryParse(this.Host.Text.Substring(split + 1), out port)) return; args.RemoteEndPoint = new DnsEndPoint(this.Host.Text.Substring(0, split), port); // set up buffer for send after connect Message message = new Message(MessageType.Login, Message.Encoding.GetBytes(this.Username.Text)); byte[] buffer = message.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); // set up callback args.Completed += new EventHandler<SocketAsyncEventArgs>(LogInCompleted); // begin connect if (!sock.ConnectAsync(args)) LogInCompleted(args.ConnectSocket, args); }
private void LogInCompleted(object sender, SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success) { //clean up if (e.ConnectSocket != null) { e.ConnectSocket.Shutdown(SocketShutdown.Both); e.ConnectSocket.Close(); } } else { //connected and data successfully sent App.ConnectedSocket = e.ConnectSocket; //request to get rooms SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.GetRooms, null); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(GetRoomsRequestSent); if (!App.ConnectedSocket.SendAsync(args)) GetRoomsRequestSent(null, args); } }
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { //If selected index is -1 (no selection) do nothing if (MainListBox.SelectedIndex == -1) return; //get id of selected room byte roomId = App.RoomInfos[MainListBox.SelectedIndex].Id; //send join request for that room to server SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.JoinRoom, new byte[]{ roomId }); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(RoomJoined); if (!App.ConnectedSocket.SendAsync(args)) RoomJoined(null, args); //clear selected index MainListBox.SelectedIndex = -1; }
private void RefreshClick(object sender, EventArgs e) { //request to get rooms SocketAsyncEventArgs args = new SocketAsyncEventArgs(); Message m = new Message(MessageType.GetRooms, null); byte[] buffer = m.GetBytes(); args.SetBuffer(buffer, 0, buffer.Length); args.Completed += new EventHandler<SocketAsyncEventArgs>(GetRoomsRequestSent); if (!App.ConnectedSocket.SendAsync(args)) GetRoomsRequestSent(null, args); }