/// <summary> /// Continuously receive data from the server and take the appropriate actions. /// </summary> /// <param name="ss">The state for this specific socket connection.</param> private void ReceiveData(SocketState ss) { StringBuilder sb = ss.sb; String sbStr = sb.ToString(); //MessageBox.Show("(CALLBACK) Message from server: " + sb.ToString()); // For debugging try { String[] messageTokens = sbStr.Split(new Char[] { '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries); int opCode = int.Parse(messageTokens[0]); // Call the appropriate function based on the opCode switch (opCode) { case 0: ShowSpreadsheetList(messageTokens); break; case 1: formName = temporaryName; CreateNewSpreadsheet(messageTokens, formName); break; case 2: formName = temporaryName; OpenOldSpreadsheet(messageTokens, formName); break; case 3: CellEdit(messageTokens); break; case 4: // MessageBox.Show(null, "Valid Edit", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 5: MessageBox.Show(null, "Invalid Edit", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 6: formName = temporaryName; FileRename(messageTokens); break; case 7: MessageBox.Show(null, "File is saved", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 8: MessageBox.Show(null, "File is renamed", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); break; case 9: MessageBox.Show(null, "This filename exist. Please Try again", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } } catch (Exception) { } /* * The code below was not parsing correctly, so I commented it out and added back my old parsing code. * I understand what you're trying to do here, but it wasn't working. If you can get it working, then * that's fine, but please don't push any code that breaks code that's already working, upgrades * can be done later. */ //String[] message = sbStr.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); // Process every but the last message because it may not be completed /*for (int i = 0; i < message.Length - 1; i++) * { * try * { * * String[] messageTokens = message[i].Split(new Char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries); * * int opCode = int.Parse(messageTokens[0]); * * // Call the appropriate function based on the opCode * switch (opCode) * { * case 0: * ShowSpreadsheetList(messageTokens); * break; * case 1: * formName = temporaryName; * CreateNewSpreadsheet(messageTokens); * break; * case 2: * formName = temporaryName; * OpenOldSpreadsheet(messageTokens); * break; * case 3: * CellEdit(messageTokens); * break; * case 6: * formName = temporaryName; * FileRename(messageTokens); * break; * case 9: * MessageBox.Show(null, "This filename exist. Please Try again", "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Information); * break; * * } * } * * catch (Exception) * { * * } * * ss.callMe = new Action<SocketState>(this.ReceiveData); * //remove the every message + '\n' character * ss.sb.Remove(0, message[i].Length + 1); * }*/ ss.callMe = new Action <SocketState>(this.ReceiveData); ss.sb.Clear(); }
// Creates a control object public NetworkControl() { theServer = null; clientID = -1; clientName = null; }
/// <summary> /// As soon as the connection to the server is successfully established, send the name of /// the requested spreadsheet to the server. /// </summary> /// <param name="ss">The state for this specific socket connection.</param> private void FirstContact(SocketState ss) { ss.callMe = new Action <SocketState>(this.ReceiveStartup); }
/// <summary> /// This is a small helper function that the client View code will call whenever it wants more data. /// Note: the client will probably want more data every time it gets data, and has finished processing /// it in its callbackFunction. /// </summary> /// <param name="ss">The SocketState object.</param> public static void GetData(SocketState ss) { ss.theSocket.BeginReceive(ss.messageBuffer, 0, 1024, SocketFlags.None, new AsyncCallback(Networking.ReceiveCallback), ss); }