private void ReceiveCallback(IAsyncResult ar) { try { StateObject state = (StateObject) ar.AsyncState; Socket client = state.WorkSocket; int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { Packet packet = ClassSerializer.DeserializeByteArray(state.Buffer); string response = packet.Message; if (packet.ImageData != null) { // We have image data } switch (response) { case "HI": // Server says hello! Let's open a Login dialog box now. Globals.DebugConsole.Log("Connected to Master Server: " + Properties.Settings.Default.MasterHost + ":" + Properties.Settings.Default.MasterPort + " and received HELLO!"); break; case "LOGIN_SUCCESS": if (packet.User != null) { Globals.User = packet.User; Globals.DebugConsole.Log(String.Format("Login OK for {0} {1} ({2}).", packet.User.FirstName, packet.User.LastName, packet.User.Username)); // Finalize login _login.FinalizeLogin(packet.User); } break; case "LIST_FRIEND": if (packet.User != null) { // We got a friend as a User object. Add it to the main form friends control. Globals.DebugConsole.Log(String.Format("Adding new friend: {0}, ({1} - {2})", packet.User.Username, packet.User.UserID, packet.User.FirstName)); _mainWindow.AddFriendThreadSafe(null, (packet.User.FirstName + " " + packet.User.LastName), packet.User.Status, packet.User.UserID); } break; case "PROMPT_SHARE": if (packet.User != null) { string request = String.Format("Do you want to accept a share from: {0}\nApplication: {1}?", packet.User.FirstName + " " + packet.User.LastName, packet.WindowTitle); var reply = MessageBox.Show(request, "Incoming Share Request", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (reply == DialogResult.Yes) { Packet p = new Packet { Message = "SHARE_ACCEPTED", ShareID = packet.ShareID, User = Globals.User }; SendData(_client, ClassSerializer.SerializeClass(p)); _mainWindow.ToggleShareThreadSafe(true, _shareWindow); } } else { MessageBox.Show("what is this i dont even", @"dafuq", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Question); } break; case "I": try { _shareWindow.UpdateImage(packet); } catch (Exception ex) { Globals.DebugConsole.Log("Error updating share window: " + ex.Message); } break; } } // Wait for more data client.BeginReceive( state.Buffer, 0, Bufsize, 0, ReceiveCallback, state); } catch (Exception ex) { ReportError(ex.Message); } }
public void Connect() { try { IPEndPoint remoteEndPoint = new IPEndPoint(_serverIP, _serverPort); // Create the TCP socket _client = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Establish connection to server _client.BeginConnect( remoteEndPoint, ConnectCallback, _client); // Wait for the connect callback to finish before continuing. _connectFinished.WaitOne(); // Send HELLO Packet packet = new Packet { Message = "HELLO" }; SendData(_client, ClassSerializer.SerializeClass(packet)); _sendFinished.WaitOne(); // Recieve a response ReceiveData(_client); _receiveFinished.WaitOne(); // Release //_client.Shutdown(SocketShutdown.Both); //_client.Close(); } catch (Exception ex) { ReportError(ex.Message); } }
public void UpdateDeviceDetails(User user) { Device device = new Device { DeviceID = 0, ExternalIP = String.Empty, // This is empty because its easier for the server to get the external IP when it populates the connected devices list InternalIP = Globals.LocalIP.ToString(), HardwareID = Tools.GetHWID(), Name = Environment.MachineName, UserID = user.UserID }; Packet deviceInformationPacket = new Packet { Message = "UPDATE_DEVICE", User = user, Device = device }; SendData(_client, ClassSerializer.SerializeClass(deviceInformationPacket)); }
public void SendLogin(string username, string password) { Globals.DebugConsole.Log("Sending login command for username: "******"{0}|{1}|{2}", "LOGIN", username, password); // Build the Login packet Packet packet = new Packet { Message = loginData }; SendData(_client, ClassSerializer.SerializeClass(packet)); }
public void SendImage(Capture.ImageData imageData) { if (imageData.Data != null) { CollabPacket.Packet imagePacket = new Packet { Message = "I", ShareID = 1, BoundingRect = imageData.BoundingBox, ImageData = imageData.Data }; Globals.DebugConsole.Log("Sending image data to server - " + imageData.Data.Length + " bytes."); SendData(_client, ClassSerializer.SerializeClass(imagePacket)); } }
public void RequestShare(string windowTitle, List<int> groupMembers) { Packet shareRequestPacket = new Packet { Message = "REQUEST_SHARE", WindowTitle = windowTitle, GroupMembers = groupMembers, User = Globals.User }; SendData(_client, ClassSerializer.SerializeClass(shareRequestPacket)); }
public void RequestFriends(User user) { // Pull friends from the database that belong to user Packet packet = new Packet {Message = "GET_FRIENDS", User = user}; SendData(_client, ClassSerializer.SerializeClass(packet)); }