示例#1
0
        static void ListenServer(Socket socket, IPEndPoint ipend)
        {
            EndPoint ep = ipend;

            byte[] container = new byte[] { };
            int    length    = 0;

            while (true)
            {
                byte[] fullBuffer  = new byte[maxBufferSize];
                int    messageSize = socket.ReceiveFrom(fullBuffer, ref ep);
                byte[] buffer      = Buffer.ChangeBufferSize(fullBuffer, 0, messageSize);
                byte   code        = buffer[0];
                if (code == 0)
                {
                    length    = int.Parse($"{buffer[1]}{buffer[2]}{buffer[3]}{buffer[4]}");
                    container = new byte[] { };
                }
                else if (length != 0 && code == 1)
                {
                    container = Buffer.CombineBuffers(container, 0, buffer, 1, buffer.Length);
                    length   -= buffer.Length - 1;
                    if (length == 0)
                    {
                        MessageProcess(container, socket, ipend);
                        container = new byte[] { };
                    }
                }
            }
        }
示例#2
0
        static void AutoSender(Socket socket)
        {
            while (true)
            {
                if (messages.Count != 0)
                {
                    UDPMessage udpm       = messages.Dequeue();
                    int        bufferSize = udpm.message.Length;
                    int        lastIndex  = 0;

                    byte[] byteLength = new byte[4];
                    string str        = string.Format($"{udpm.message.Length}");
                    for (int i = byteLength.Length - str.Length, j = 0; i < byteLength.Length; i++, j++)
                    {
                        byteLength[i] = (byte)int.Parse($"{str[j]}");
                    }
                    socket.SendTo(Buffer.AddStartCode(byteLength, 0), udpm.ipend);

                    while (bufferSize > 0)
                    {
                        socket.SendTo(Buffer.AddStartCode(Buffer.ChangeBufferSize(udpm.message, lastIndex, bufferSize < maxBufferSize ? bufferSize : maxBufferSize - 1), 1), udpm.ipend);
                        bufferSize -= maxBufferSize - 1;
                        lastIndex  += maxBufferSize - 1;
                    }
                    Console.WriteLine("Сообщение отправлено");
                }
            }
        }
示例#3
0
 static void ClientListen(Socket socket)
 {
     while (true)
     {
         EndPoint   clientEndPoint = new IPEndPoint(IPAddress.None, 25000);
         byte[]     buffer         = new byte[maxBufferSize];
         int        messageSize    = socket.ReceiveFrom(buffer, ref clientEndPoint);
         IPEndPoint cleanEP        = (IPEndPoint)clientEndPoint;
         if (!UserInList(cleanEP))
         {
             Console.WriteLine("Новый пользователь");
             lock (lockUsersList) users.Add(new UDPUser(cleanEP));
         }
         byte[] messageBuffer = Buffer.ChangeBufferSize(buffer, 0, messageSize);
         for (int i = 0; i < users.Count; i++)
         {
             lock (lockUsersList)
             {
                 if (users[i].UserIP.ToString() == cleanEP.ToString())
                 {
                     byte code = buffer[0];
                     if (code == 0)
                     {
                         users[i].length       = int.Parse($"{messageBuffer[1]}{messageBuffer[2]}{messageBuffer[3]}{messageBuffer[4]}");
                         users[i].messageBytes = new byte[] { };
                     }
                     else if (users[i].length != 0 && code == 1)
                     {
                         byte[] messagePart = Buffer.ChangeBufferSize(messageBuffer, 1, messageBuffer.Length - 1);
                         users[i].messageBytes = Buffer.CombineBuffers(users[i].messageBytes, messagePart);
                         users[i].length      -= messageBuffer.Length - 1;
                         if (users[i].length == 0)
                         {
                             MessageProcess(users[i].messageBytes, socket, cleanEP);
                             users[i].messageBytes = new byte[] { };
                         }
                     }
                     break;
                 }
             }
         }
     }
 }
示例#4
0
        static void Send(object obj, Socket socket, IPEndPoint ipend)
        {
            byte[] message    = ToBinary(obj);
            int    bufferSize = message.Length;
            int    lastIndex  = 0;

            byte[] byteLength = new byte[4];
            string str        = string.Format($"{message.Length}");

            for (int i = byteLength.Length - str.Length, j = 0; i < byteLength.Length; i++, j++)
            {
                byteLength[i] = (byte)int.Parse($"{str[j]}");
            }
            socket.SendTo(Buffer.AddStartCode(byteLength, 0), ipend);

            while (bufferSize > 0)
            {
                byte[] messagePartBuffer = Buffer.ChangeBufferSize(message, lastIndex, bufferSize < maxBufferSize ? bufferSize : maxBufferSize - 1);
                socket.SendTo(Buffer.AddStartCode(messagePartBuffer, 1), ipend);
                bufferSize -= maxBufferSize - 1;
                lastIndex  += maxBufferSize - 1;
            }
        }