private void SendMessageOfClientDisconnect(int clientId) { try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_ClientDisconnecting; xdata.Data_Type = 0; xdata.Packet_Size = (UInt16)Marshal.SizeOf(typeof(PACKET_DATA)); xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = (UInt32)clientId; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); svr.SendMessage(byData); } catch { } }
private void TellServerImDisconnecting() { try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Close; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); } catch (Exception ex) { Log.Message(ex.ToString()); } }
private void ReplyToHostPing(byte[] message) { try { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); /****************************************************************************************/ //calculate how long that ping took to get here TimeSpan ts = (new DateTime(IncomingData.DataLong1)) - (new DateTime(ServerTime)); Console.WriteLine($"{GeneralFunction.GetDateTimeFormatted}: {string.Format("Ping From Server to client: {0:0.##}ms", ts.TotalMilliseconds)}"); /****************************************************************************************/ ServerTime = IncomingData.DataLong1;// Server computer's current time! PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_PingResponse; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; xdata.DataLong1 = IncomingData.DataLong1; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); CheckThisComputersTimeAgainstServerTime(); } catch (Exception ex) { string exceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; Console.WriteLine($"EXCEPTION IN: ReplyToHostPing - {exceptionMessage}"); } }
private void TellServerImDisconnecting() { try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Close; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); } catch (Exception ex) { string exceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; Console.WriteLine($"EXCEPTION IN: TellServerImDisconnecting - {exceptionMessage}"); } }
private void RequestNewConnectionCredentials(int ClientID) { try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_RequestCredentials; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = (UInt16)ClientID; xdata.idFrom = 0; xdata.DataLong1 = DateTime.UtcNow.Ticks; if (!svr.workerSockets.ContainsKey(ClientID)) { return; } lock (svr.workerSockets) { //ship back their address for reference to the client string clientAddr = ((IPEndPoint)svr.workerSockets[ClientID].UserSocket.RemoteEndPoint).Address.ToString(); clientAddr.CopyTo(0, xdata.szStringDataA, 0, clientAddr.Length); byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); if (svr.workerSockets[ClientID].UserSocket.Connected) { svr.SendMessage(ClientID, byData); Debug.WriteLine(DateTime.Now.ToShortDateString() + ", " + DateTime.Now.ToLongTimeString() + " - from " + ClientID.ToString()); } } } catch { } }
public void ReplyToHostCredentialRequest(byte[] message) { if (client == null) { return; } Console.WriteLine($"ReplyToHostCredentialRequest ThreadID = {Thread.CurrentThread.ManagedThreadId}"); Int32 Loc = 0; try { //We will assume to tell the host this is just an update of the //credentials we first sent during the application start. This //will be true if the 'message' argument is null, otherwise we //will change the packet type below to the 'TYPE_MyCredentials'. UInt16 PaketType = (UInt16)PACKETTYPES.TYPE_CredentialsUpdate; if (message != null) { int myOldServerID = 0; //The host server has past my ID. PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); Loc = 10; if (MyHostServerID > 0) { myOldServerID = MyHostServerID; } Loc = 20; MyHostServerID = (int)IncomingData.idTo;//Hang onto this value Loc = 25; Console.WriteLine($"My Host Server ID is {MyHostServerID}"); string MyAddressAsSeenByTheHost = new string(IncomingData.szStringDataA).TrimEnd('\0');//My computer address SetSomeLabelInfoFromThread($"My Address As Seen By The Server: {MyAddressAsSeenByTheHost}, and my ID given by the server is: {MyHostServerID}"); ServerTime = IncomingData.DataLong1; PaketType = (UInt16)PACKETTYPES.TYPE_MyCredentials; } //ods.DebugOut("Send Host Server some info about myself"); PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = PaketType; xdata.Data_Type = 0; xdata.Packet_Size = (UInt16)Marshal.SizeOf(typeof(PACKET_DATA)); xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; //Station Name string p = System.Environment.MachineName; if (p.Length > (xdata.szStringDataA.Length - 1)) { p.CopyTo(0, xdata.szStringDataA, 0, (xdata.szStringDataA.Length - 1)); } else { p.CopyTo(0, xdata.szStringDataA, 0, p.Length); } xdata.szStringDataA[(xdata.szStringDataA.Length - 1)] = '\0';//cap it off just incase //App and DLL Version string VersionNumber = string.Empty; VersionNumber = Assembly.GetEntryAssembly().GetName().Version.Major.ToString() + "." + Assembly.GetEntryAssembly().GetName().Version.Minor.ToString() + "." + Assembly.GetEntryAssembly().GetName().Version.Build.ToString(); Loc = 30; VersionNumber.CopyTo(0, xdata.szStringDataB, 0, VersionNumber.Length); Loc = 40; //Station Name string L = textBoxClientName.Text; if (L.Length > (xdata.szStringData150.Length - 1)) { L.CopyTo(0, xdata.szStringData150, 0, (xdata.szStringData150.Length - 1)); } else { L.CopyTo(0, xdata.szStringData150, 0, L.Length); } xdata.szStringData150[(xdata.szStringData150.Length - 1)] = '\0';//cap it off just incase Loc = 50; //Application type xdata.nAppLevel = (UInt16)APPLEVEL.None; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); Loc = 60; SendMessageToServer(byData); Loc = 70; } catch (Exception ex) { string exceptionMessage = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; Console.WriteLine($"EXCEPTION at location {Loc}, IN: ReplyToHostCredentialRequest - {exceptionMessage}"); } }
private void buttonSendDataToServer_Click(object sender, EventArgs e) { PACKET_DATA xdata = new PACKET_DATA(); /****************************************************************/ //prepair the start packet xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Message; xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageStart; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; int pos = 0; int chunkSize = xdata.szStringDataA.Length;//300 bytes int chunkSize1 = xdata.szStringDataB.Length; if (textBoxText.Text.Length <= xdata.szStringDataA.Length) { textBoxText.Text.CopyTo(0, xdata.szStringDataA, 0, textBoxText.Text.Length); chunkSize = textBoxText.Text.Length; } else { textBoxText.Text.CopyTo(0, xdata.szStringDataA, 0, xdata.szStringDataA.Length); } string ClientName = textBoxClientName.Text; if (ChatRoom != 0) { ClientName += "[P]"; } if (textBoxClientName.Text.Length <= xdata.szStringDataB.Length) { ClientName.CopyTo(0, xdata.szStringDataB, 0, ClientName.Length); chunkSize1 = ClientName.Length; } else { ClientName.CopyTo(0, xdata.szStringDataB, 0, xdata.szStringDataB.Length); } xdata.Data1 = (UInt32)chunkSize; xdata.Data2 = (UInt32)chunkSize1; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); /**************************************************/ //Send the message body(if there is any) xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageGuts; pos = chunkSize;//set positionf while (true) { int PosFromEnd = textBoxText.Text.Length - pos; if (PosFromEnd <= 0) { break; } Array.Clear(xdata.szStringDataA, 0, xdata.szStringDataA.Length);//Clear this field before putting more data in it if (PosFromEnd < xdata.szStringDataA.Length) { chunkSize = textBoxText.Text.Length - pos; } else { chunkSize = xdata.szStringDataA.Length; } textBoxText.Text.CopyTo(pos, xdata.szStringDataA, 0, chunkSize); xdata.Data1 = (UInt32)chunkSize; pos += chunkSize;//set new position byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); } /**************************************************/ //Send an EndMessage xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageEnd; xdata.Data1 = (UInt32)pos;//send the total which should be the 'pos' value byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); textBoxText.Text = ""; }
private void buttonSendDataToServer_Click(object sender, EventArgs e) { pictureBox1.Image = imageListStatusLights.Images["BLUE"]; PACKET_DATA xdata = new PACKET_DATA(); /****************************************************************/ //prepair the start packet xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Message; xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageStart; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; //Before we send the text, lets stuff those Number values in the first data packet! Int32 num1 = 0; Int32.TryParse(textBoxNum1.Text, out num1); xdata.Data16 = num1; Int32 num2 = 0; Int32.TryParse(textBoxNum2.Text, out num2); xdata.Data17 = num2; int pos = 0; int chunkSize = xdata.szStringDataA.Length;//300 bytes if (textBoxText.Text.Length <= xdata.szStringDataA.Length) { textBoxText.Text.CopyTo(0, xdata.szStringDataA, 0, textBoxText.Text.Length); chunkSize = textBoxText.Text.Length; } else textBoxText.Text.CopyTo(0, xdata.szStringDataA, 0, xdata.szStringDataA.Length); xdata.Data1 = (UInt32)chunkSize; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); /**************************************************/ //Send the message body(if there is any) xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageGuts; pos = chunkSize;//set position while (true) { int PosFromEnd = textBoxText.Text.Length - pos; if (PosFromEnd <= 0) break; Array.Clear(xdata.szStringDataA, 0, xdata.szStringDataA.Length);//Clear this field before putting more data in it if (PosFromEnd < xdata.szStringDataA.Length) chunkSize = textBoxText.Text.Length - pos; else chunkSize = xdata.szStringDataA.Length; textBoxText.Text.CopyTo(pos, xdata.szStringDataA, 0, chunkSize); xdata.Data1 = (UInt32)chunkSize; pos += chunkSize;//set new position byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); } /**************************************************/ //Send an EndMessage xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageEnd; xdata.Data1 = (UInt32)pos;//send the total which should be the 'pos' value byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); }
private void ReplyToHostCredentialRequest(byte[] message) { if (client == null) { return; } try { UInt16 PaketType = (UInt16)PACKETTYPES.TYPE_CredentialsUpdate; if (message != null) { int myOldServerID = 0; PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); if (MyHostServerID > 0) { myOldServerID = MyHostServerID; } MyHostServerID = (int)IncomingData.idTo; Log.Message($"My Host Server ID is {MyHostServerID}"); string MyAddressAsSeenByTheHost = new string(IncomingData.szStringDataA).TrimEnd('\0'); ServerTime = IncomingData.DataLong1; PaketType = (UInt16)PACKETTYPES.TYPE_MyCredentials; } PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = PaketType; xdata.Data_Type = 0; xdata.Packet_Size = (UInt16)Marshal.SizeOf(typeof(PACKET_DATA)); xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; string p = Environment.MachineName; if (p.Length > (xdata.szStringDataA.Length - 1)) { p.CopyTo(0, xdata.szStringDataA, 0, (xdata.szStringDataA.Length - 1)); } else { p.CopyTo(0, xdata.szStringDataA, 0, p.Length); } xdata.szStringDataA[(xdata.szStringDataA.Length - 1)] = '\0'; string VersionNumber = string.Empty; VersionNumber = Assembly.GetEntryAssembly().GetName().Version.Major.ToString() + "." + Assembly.GetEntryAssembly().GetName().Version.Minor.ToString() + "." + Assembly.GetEntryAssembly().GetName().Version.Build.ToString(); VersionNumber.CopyTo(0, xdata.szStringDataB, 0, VersionNumber.Length); if (room != null) { room.CopyTo(0, xdata.szStringDataC, 0, room.Length); } string L = user; if (L.Length > (xdata.szStringData150.Length - 1)) { L.CopyTo(0, xdata.szStringData150, 0, (xdata.szStringData150.Length - 1)); } else { L.CopyTo(0, xdata.szStringData150, 0, L.Length); } xdata.szStringData150[(xdata.szStringData150.Length - 1)] = '\0'; xdata.nAppLevel = (UInt16)APPLEVEL.None; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); } catch (Exception ex) { Log.Message(ex.ToString()); } }
private void AssembleMessage(int clientID, byte[] message) { try { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); switch (IncomingData.Data_Type) { case (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageStart: { if (svr.workerSockets.ContainsKey(clientID)) { OnCommunications( $"Client '{svr.workerSockets[clientID].szClientName}' color={IncomingData.Data16} message={new string(IncomingData.szStringDataA).TrimEnd('\0')}", INK.CLR_BLUE); } } break; case (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageGuts: { //sb.Append(new string(IncomingData.szStringDataA).TrimEnd('\0')); OnCommunications(new string(IncomingData.szStringDataA).TrimEnd('\0'), INK.CLR_GREEN); } break; case (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageEnd: { /****************************************************************/ //Now tell the client teh message was received! PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_MessageReceived; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); svr.SendMessage(clientID, byData); var msg = new string(IncomingData.szStringDataA).TrimEnd('\0'); var user = new string(IncomingData.szStringData150).TrimEnd('\0'); var room = new string(IncomingData.szStringDataC).TrimEnd('\0'); if (msg.Trim() == "" || room.Trim() == "") { break; } PACKET_DATA xdata2 = new PACKET_DATA(); xdata2.Packet_Type = (UInt16)PACKETTYPES.TYPE_Message; xdata2.Data16 = IncomingData.Data16; xdata2.szStringDataA = IncomingData.szStringDataA; xdata2.szStringData150 = IncomingData.szStringData150; byte[] byData2 = PACKET_FUNCTIONS.StructureToByteArray(xdata2); svr.SendMessageToAllClients(byData2, room); } break; } } catch { Console.WriteLine("ERROR Assembling message"); } }