Ejemplo n.º 1
0
        public void SendImage(Bitmap bmp, int imageNumber)
        {
            int count = 0, currentBit = 0;

            byte[] arr        = StreamToArray(ScreenCapture.VaryQualityLevel(bmp));
            int    numberBits = (arr.Length / PackageSize) + ((arr.Length % PackageSize) == 0 ? 0 : 1);

            while (count < arr.Length)
            {
                int    arrLength = (arr.Length - count) > PackageSize ? PackageSize : arr.Length - count;
                byte[] tempArr   = new byte[arrLength];
                Array.Copy(arr, count, tempArr, 0, arrLength);
                ImageBit bit = new ImageBit()
                {
                    NumberBits  = numberBits,
                    CurrentBit  = currentBit++,
                    Data        = tempArr,
                    ImageNumber = imageNumber,
                    Size        = arr.Length
                };
                tempArr = Serialize(bit);
                count  += arrLength;
                client.Send(tempArr);
            }
        }
Ejemplo n.º 2
0
    public void Send()
    {
        string message = isServer ? "I am server. " + BigString() : "I am client. " + BigString();

        Buffer       buffer = Buffer.Create();
        BinaryWriter writer = buffer.BeginPacket(Packet.Empty);

        writer.Write(message);
        buffer.EndPacket();

        Debug.Log("Buffer size: " + buffer.size);

        if (!isReliable)
        {
            UdpProtocol.Send(buffer, NetworkUtility.ResolveEndPoint(ServerExternalIP, UDPPort));
        }
        else
        {
            if (!isServer)
            {
                TcpProtocol.SendTcpPacket(buffer);
            }
            else
            {
                TcpProtocol.SendToClient(0, buffer);
            }
        }
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Keep receiving incoming packets.
    /// </summary>

    void Update()
    {
        Buffer     buffer;
        IPEndPoint ip;
        bool       changed = false;
        long       time    = System.DateTime.UtcNow.Ticks / 10000;

        // Receive and process UDP packets one at a time
        while (mUdp != null && mUdp.ReceivePacket(out buffer, out ip))
        {
            if (buffer.size > 0)
            {
                try
                {
                    BinaryReader reader   = buffer.BeginReading();
                    Packet       response = (Packet)reader.ReadByte();

                    if (response == Packet.ResponseServerList)
                    {
                        isActive  = true;
                        mNextSend = time + 3000;
                        knownServers.ReadFrom(reader, time);
                        knownServers.Cleanup(time);
                        changed = true;
                    }
                    else if (response == Packet.Error)
                    {
                        errorString = reader.ReadString();
                        Debug.LogWarning(errorString);
                        changed = true;
                    }
                }
                catch (System.Exception) { }
            }
            buffer.Recycle();
        }

        // Clean up old servers
        if (knownServers.Cleanup(time))
        {
            changed = true;
        }

        // Trigger the listener callback
        if (changed && onChange != null)
        {
            onChange();
        }
        else if (mNextSend < time && mUdp != null)
        {
            // Send out the update request
            mNextSend = time + 3000;
            mUdp.Send(mRequest, mRemoteAddress);
        }
    }
Ejemplo n.º 4
0
 public void SendMessage(string text)
 {
     byte[] datagram = Encoding.UTF8.GetBytes(text);
     udp.Send(datagram);
 }