Beispiel #1
0
        public static T RecieveMsg <T>(Socket receiver)
        {
            byte[] length = new byte[256];

            int c    = 0;
            int step = 256;

            while (c < 256)
            {
                if (c + step > 256)
                {
                    step = 256 - c;
                }
                c += receiver.Receive(length, c, step, SocketFlags.None);
            }

            int bytesRec = BinFormatter.FromBytes <int>(length);

            byte[] bytes = new byte[bytesRec];

            int a = 0;

            step = bytesRec;
            while (a < bytesRec)
            {
                if (a + step > bytesRec)
                {
                    step = bytesRec - a;
                }
                a += receiver.Receive(bytes, a, step, SocketFlags.None);
            }
            return(BinFormatter.FromBytes <T>(bytes));
        }
Beispiel #2
0
        public static async Task SocketRecieve(CancellationToken ct)
        {
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sender.Bind(new IPEndPoint(IPAddress.Any, portDispatcherServer));
            sender.Listen(10);
            while (!ct.IsCancellationRequested)
            {
                MessageSendRecieve msg = new MessageSendRecieve();
                try
                {
                    Socket receiver = sender.Accept();
                    msg    = RecieveMsg <MessageSendRecieve>(receiver);
                    msg.IP = (receiver.RemoteEndPoint as IPEndPoint).Address.ToString();


                    Program.msgsWithHosts_Semaphore.WaitOne();
                    bool nameRepeats = false;
                    for (int i = 0; i < Program.msgsWithHosts.Count && !nameRepeats; i++)
                    {
                        nameRepeats = Program.msgsWithHosts[i].mqName == msg.mqName;
                    }
                    Program.msgsWithHosts_Semaphore.Release();

                    if (!nameRepeats)
                    {
                        receiver.Send(BinFormatter.ToBytes <bool>(true));
                    }
                    else
                    {
                        Console.WriteLine($"Server {msg.mqIP} denied");
                        receiver.Send(BinFormatter.ToBytes <bool>(false));
                        receiver.Shutdown(SocketShutdown.Both);
                        receiver.Close();
                        break;
                    }

                    receiver.Shutdown(SocketShutdown.Both);
                    receiver.Close();

                    Program.msgsWithHosts_Semaphore.WaitOne();
                    Program.msgsWithHosts.Add(msg);
                    Console.WriteLine("New server connected");
                    Program.msgsWithHosts_Semaphore.Release();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
        public static async Task ServersListSend(CancellationToken ct)
        {
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sender.Bind(new IPEndPoint(IPAddress.Any, portDispatcherClient));
            sender.Listen(10);
            while (!ct.IsCancellationRequested)
            {
                try
                {
                    Socket handler = sender.Accept();
                    Program.msgsWithHosts_Semaphore.WaitOne();
                    int serversCount             = Program.msgsWithHosts.Count;
                    MessageSendRecieve[] servers = new MessageSendRecieve[serversCount];
                    if (serversCount != 0)
                    {
                        int i = 0;
                        foreach (var elem in Program.msgsWithHosts)
                        {
                            MessageSendRecieve msg = elem;
                            servers[i] = msg;
                            i++;
                        }
                        SendMsg <MessageSendRecieve[]>(handler, servers);
                        Console.WriteLine("Server list send");
                    }
                    else
                    {
                        byte[] byteSet = BinFormatter.ToBytes <int>(0);
                        handler.Send(byteSet);
                    }
                    Program.msgsWithHosts_Semaphore.Release();

                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
        }
 public static void SendMsg <T>(Socket handler, T msg)
 {
     byte[] byteSet = BinFormatter.ToBytes <T>(msg);
     handler.Send(BinFormatter.ToBytes <int>(byteSet.Length));
     handler.Send(byteSet);
 }