public static bool SendUser(string user_name, string group, int type, string msg) { try { MSG packet = new MSG(group, type, msg); byte[] buf = new byte[2048]; buf = Util.Serialization(packet); foreach (DictionaryEntry de in Server.clients) { if ((de.Value).ToString() == user_name) Server.MessageSender((Socket)de.Key, buf); } } catch (System.Exception ex) { //parse this error and other send to administrator Log.Write(ex.Message, "Exception", "exception"); Color.WriteLineColor(ex.Message, ConsoleColor.Red); return false; } return true; }
public static byte[] Serialization(MSG obj) { BinaryFormatter formatter = new BinaryFormatter(); MemoryStream stream = new MemoryStream(); formatter.Serialize(stream, obj); byte[] msg = stream.ToArray(); return msg; }
public static void Sender(string group, int type, string msg) { MSG packet = new MSG(group, type, msg); byte[] buf = new byte[1024]; buf = Util.Serialization(packet); server.Send(buf); try { Thread.CurrentThread.Abort(); } catch { //Log.ExcWrite("[Sender][ThreadAbortException] " + exc.Message); threads.Remove(Thread.CurrentThread); Thread.ResetAbort(); } }
private static void Receiver(Socket client) { Thread th = new Thread(delegate() { while (true) { try { byte[] bytes = new byte[1024]; client.Receive(bytes); if (bytes.Length != 0) { //Принимаемый пакет разбор структуры MSG packet = new MSG("0", 0, "null"); packet = Util.DeSerialization(bytes); Packages.parse(packet.group, packet.type, packet.message); } } catch (SocketException exc) { if (exc.ErrorCode == 10054) Environment.Exit(0); Log.ExcWrite("[Receiver][SocketException] " + exc.Message); //TODO FORM RECCONECT TO SERVER!!!! } catch (Exception exc) { Color.WriteLineColor("[Receiver][Exception] " + exc.Message, ConsoleColor.Red); Log.ExcWrite("[Receiver][Exception] " + exc.Message); } } }); th.Start(); th.Name = "Слушаю ответ"; threads.Add(th); }
private static void MessageReceiver(Socket r_client) { var user = new User(); while (isServerRunning) { try { if (!r_client.Connected) return; user.ipaddress = IPAddress.Parse(((IPEndPoint)r_client.RemoteEndPoint).Address.ToString()); user.port = ((IPEndPoint)r_client.RemoteEndPoint).Port; var bytes = new byte[4096]; r_client.Receive(bytes); MSG packet = new MSG("0", 0, "null"); packet = Util.DeSerialization(bytes); //add debug paramter #if DEBUG Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] " + "[DEBUG] " + packet.group + " " + packet.type + " " + packet.message); #endif Packages.parse(packet.group, packet.type, packet.message, user, r_client); } catch (SocketException exc) { if (exc.ErrorCode == 10054) { Packages.connector.ExecuteNonQuery("UPDATE `barrierserver`.`users` SET `online`='0' WHERE `username`='" + user.username + "'"); Color.WriteLineColor("Клиент отключился: " + Server.clients[r_client], ConsoleColor.Cyan); if (!r_client.Connected) r_client.Disconnect(true); clients.Remove(r_client); if (!abort_thread(Thread.CurrentThread)) { Color.WriteLineColor("Поток не завершен!", ConsoleColor.Red); } break; } } catch(ThreadAbortException) { break; } catch (Exception exc) { Console.WriteLine("[" + DateTime.Now.ToLongTimeString() + "] " + exc.Message); Thread.Sleep(5000); } } }