private void AssembleMessage(int clientID, byte[] message) { try { StringBuilder s = new StringBuilder(); PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); for (int i = 0; i < IncomingData.szStringDataA.Length; i++) { s.Append(IncomingData.szStringDataA[i]); } PacketAsString = s.ToString(); switch (IncomingData.Data_Type) { case (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageStart: { if (svr.workerSockets.ContainsKey(clientID)) { OnCommunications( $"Client '{svr.workerSockets[clientID].szClientName}' sent some numbers and some text... num1= {IncomingData.Data16} and num2= {IncomingData.Data17}:", INK.CLR_BLUE); OnCommunications($"Client also said:", INK.CLR_BLUE); //sb = new StringBuilder(new string(IncomingData.szStringDataA).TrimEnd('\0')); OnCommunications(new string(IncomingData.szStringDataA).TrimEnd('\0'), INK.CLR_GREEN); } } 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: { //sb = new StringBuilder(new string(IncomingData.szStringDataA).TrimEnd('\0')); OnCommunications("FINISHED GETTING MESSAGE", INK.CLR_BLUE); /****************************************************************/ //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); } break; } } catch { Console.WriteLine("ERROR Assembling message"); } }
private void ReplyToHostPing(byte[] message) { try { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); TimeSpan ts = (new DateTime(IncomingData.DataLong1)) - (new DateTime(ServerTime)); Log.Message($"{string.Format("Ping From Server to client: {0:0.##}ms", ts.TotalMilliseconds)}"); ServerTime = IncomingData.DataLong1; 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) { Log.Message(ex.ToString()); } }
private void UpdateTheConnectionTimers(int clientNumber, byte[] message) { lock (svr.workerSockets) { try { if (svr.workerSockets.ContainsKey(clientNumber)) { svr.workerSockets[clientNumber].dTimer = DateTime.Now; Int64 elapsedTime = svr.workerSockets[clientNumber].PingStatClass.StopTheClock(); //Console.WriteLine("UpdateTheConnectionTimers: " + ConnectionID.ToString()); //Debug.WriteLine("Ping Time for " + ConnectionID.ToString() + ": " + elapsedTime.ToString() + "ms"); PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); int ThisClientsMaxReturnDataSetValue = IncomingData.Data16; Console.WriteLine($"{GeneralFunction.GetDateTimeFormatted}: Ping From Server to client: {elapsedTime}ms"); UpdateThePingTimeFromThread(clientNumber, elapsedTime); /****************************************************************************************/ } } catch (Exception ex) { string msg = (ex.InnerException == null) ? ex.Message : ex.InnerException.Message; OnCommunications($"EXCEPTION in UpdateTheConnectionTimers - {msg}", INK.CLR_RED); } } }
private void SendRegisteredMessage(int clientId, byte[] message) { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Registered; xdata.Data_Type = 0; xdata.Packet_Size = (UInt16)Marshal.SizeOf(typeof(PACKET_DATA)); xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = (UInt32)clientId; xdata.Data6 = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.Major; xdata.Data7 = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.Minor; xdata.Data8 = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.Build; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); svr.SendMessage(byData); } catch { } }
private void onUserClick(object sender, EventArgs e) { Button b = (Button)sender; PACKET_DATA xdata = new PACKET_DATA(); xdata.Data10 = (int)b.Tag; xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_ChangeChatRoom; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); client.SendMessage(byData); }
private void AddNewMember(byte[] newuser) { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(newuser, typeof(PACKET_DATA)); Button b = new Button(); b.Text = new string(IncomingData.szStringDataA).TrimEnd('\0'); b.Tag = IncomingData.Data10; b.Click += onUserClick; Console.WriteLine(new string(IncomingData.szStringDataA).TrimEnd('\0')); OnNewUser(b); }
public void SendDataToServer(string user, string msg, int num1 = 0, int num2 = 0) { PACKET_DATA xdata = new PACKET_DATA(); 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; xdata.Data16 = num1; xdata.Data17 = num2; int pos = 0; int chunkSize = xdata.szStringDataA.Length; if (msg.Length <= xdata.szStringDataA.Length) { msg.CopyTo(0, xdata.szStringDataA, 0, msg.Length); chunkSize = msg.Length; } else { msg.CopyTo(0, xdata.szStringDataA, 0, xdata.szStringDataA.Length); } if (!string.IsNullOrEmpty(room)) { room.CopyTo(0, xdata.szStringDataC, 0, room.Length); } if (!string.IsNullOrEmpty(user)) { user.CopyTo(0, xdata.szStringData150, 0, user.Length); } xdata.Data1 = (UInt32)chunkSize; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageGuts; pos = chunkSize; xdata.Data_Type = (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageEnd; xdata.Data1 = (UInt32)pos; byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); SendMessageToServer(byData); }
private void ChangeChatRoom(byte[] Message) { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(Message, typeof(PACKET_DATA)); if (IncomingData.Data10 == 0) { ChangeChatRoomLabel("Global"); } else { ChangeChatRoomLabel(new string(IncomingData.szStringDataA).TrimEnd('\0')); } ChatRoom = IncomingData.Data10; }
private void UserDisconnected(byte[] message) { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); if (IncomingData.idFrom == ChatRoom) { PACKET_DATA xdata = new PACKET_DATA(); xdata.Data10 = 0; xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_ChangeChatRoom; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); client.SendMessage(byData); } OnOldUser((int)IncomingData.idFrom, new string(IncomingData.szStringDataA).TrimEnd('\0')); }
private void frmServer_FormClosing(object sender, FormClosingEventArgs e) { if (svr != null) { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_HostExiting; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); svr.SendMessage(byData); Thread.Sleep(250); } ServerIsExiting = true; try { if (timerGarbagePatrol != null) { timerGarbagePatrol.Stop(); timerGarbagePatrol.Elapsed -= timerGarbagePatrol_Elapsed; timerGarbagePatrol.Dispose(); timerGarbagePatrol = null; } } catch { } try { if (timerPing != null) { timerPing.Stop(); timerPing.Elapsed -= timerPing_Elapsed; timerPing.Dispose(); timerPing = null; } } catch { } KillTheServer(); }
/// <summary> /// return bool, TRUE if its a FullClient Connection /// </summary> /// <param name="clientNumber"></param> /// <param name="message"></param> private void PostUserCredentials(int clientNumber, byte[] message) { if (InvokeRequired) { this.Invoke(new PostUserCredentialsDelegate(PostUserCredentials), new object[] { clientNumber, message }); return; } try { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(message, typeof(PACKET_DATA)); lock (svr.workerSockets) { string ComputerName = new string(IncomingData.szStringDataA).TrimEnd('\0'); //Station/Computer's name string VersionStr = new string(IncomingData.szStringDataB).TrimEnd('\0'); //app version string Room = new string(IncomingData.szStringDataC).TrimEnd('\0'); string ClientsName = new string(IncomingData.szStringData150).TrimEnd('\0'); //Client's Name string ClientIP = svr.workerSockets[clientNumber].UserSocket.RemoteEndPoint.ToString(); listView1.Items[clientNumber.ToString()].SubItems[1].Text = ComputerName; listView1.Items[clientNumber.ToString()].SubItems[2].Text = VersionStr; listView1.Items[clientNumber.ToString()].SubItems[4].Text = ClientsName; listView1.Items[clientNumber.ToString()].SubItems[6].Text = Room; if (svr.workerSockets.ContainsKey(clientNumber)) { svr.workerSockets[clientNumber].szStationName = ComputerName; svr.workerSockets[clientNumber].szRoom = Room.Trim().ToLower(); svr.workerSockets[clientNumber].szClientName = ClientsName; OnCommunications(string.Format("{0} Registered Connection ({1}) for '{2}' on PC: {3} IP: {4} Room: {5}", GeneralFunction.GetDateTimeFormatted, clientNumber, ClientsName, ComputerName, ClientIP, Room), INK.CLR_GREEN); } }//end lock } catch (Exception ex) { OnCommunications($"EXCEPTION: PostUserCredentials on client {clientNumber}, exception: {ex.Message}", INK.CLR_RED); } }
void PingTheConnections() { if (svr == null) { return; } try { PACKET_DATA xdata = new PACKET_DATA(); xdata.Packet_Type = (UInt16)PACKETTYPES.TYPE_Ping; xdata.Data_Type = 0; xdata.Packet_Size = 16; xdata.maskTo = 0; xdata.idTo = 0; xdata.idFrom = 0; xdata.DataLong1 = DateTime.UtcNow.Ticks; byte[] byData = PACKET_FUNCTIONS.StructureToByteArray(xdata); //Stopwatch sw = new Stopwatch(); //sw.Start(); lock (svr.workerSockets) { foreach (Server.UserSock s in svr.workerSockets.Values) { //Console.WriteLine("Ping id - " + s.iClientID.ToString()); //Thread.Sleep(25);//allow a slight moment so all the replies dont happen at the same time s.PingStatClass.StartTheClock(); try { svr.SendMessage(s.iClientID, byData); } catch { } } } //sw.Stop(); //Debug.WriteLine("TimeAfterSend: " + sw.ElapsedMilliseconds.ToString() + "ms"); } catch { } }
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 { } }
private void AssembleMessage(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: { //new num1 IncomingData.Data16 //num 2 IncomingData.Data17 OnCommunicationsClient(new string(IncomingData.szStringDataA).TrimEnd('\0'), new string(IncomingData.szStringDataB).TrimEnd('\0'), false); //OnCommunications(new string(IncomingData.szStringDataA).TrimEnd('\0'), INK.CLR_GREEN); } 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); OnCommunicationsClient(new string(IncomingData.szStringDataA).TrimEnd('\0'), new string(IncomingData.szStringDataB).TrimEnd('\0'), true); } break; case (UInt16)PACKETTYPES_SUBMESSAGE.SUBMSG_MessageEnd: { } break; } } catch { Console.WriteLine("ERROR Assembling message"); } }
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()); } }
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 ProcessRecievedServerData() { try { while (ServerConnected) { autoEvent2.WaitOne(10000); if (AppIsExiting || !ServerConnected) { break; } while (FullHostServerPacketList.Count > 0) { try { FullPacket fp; lock (FullHostServerPacketList) fp = FullHostServerPacketList.Dequeue(); UInt16 type = (ushort)(fp.ThePacket[1] << 8 | fp.ThePacket[0]); switch (type) { case (Byte)PACKETTYPES.TYPE_RequestCredentials: { ReplyToHostCredentialRequest(fp.ThePacket); } break; case (Byte)PACKETTYPES.TYPE_Ping: { ReplyToHostPing(fp.ThePacket); } break; case (Byte)PACKETTYPES.TYPE_HostExiting: HostCommunicationsHasQuit(true); break; case (Byte)PACKETTYPES.TYPE_Registered: { } break; case (Byte)PACKETTYPES.TYPE_MessageReceived: break; case (Byte)PACKETTYPES.TYPE_Message: { if (NewMessageEvent != null) { PACKET_DATA IncomingData = new PACKET_DATA(); IncomingData = (PACKET_DATA)PACKET_FUNCTIONS.ByteArrayToStructure(fp.ThePacket, typeof(PACKET_DATA)); var msg = new string(IncomingData.szStringDataA).TrimEnd('\0'); var user = new string(IncomingData.szStringData150).TrimEnd('\0'); NewMessageEvent(user, msg, IncomingData.Data16); } } break; } if (client != null) { client.LastDataFromServer = DateTime.Now; } } catch (Exception ex) { Log.Message(ex.ToString()); } } } } 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"); } }
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 = ""; }