CentralMine.NET.Client.SendPacket C# (CSharp) Method

SendPacket() public method

public SendPacket ( byte data ) : void
data byte
return void
        void SendPacket(byte[] data)
        {
            if (!mClient.Connected || !mClient.GetStream().CanWrite)
                return;

            try
            {
                if (mType == Type.Javascript)
                {
                    // Encode the data for websocket
                    MemoryStream stream = new MemoryStream();
                    BinaryWriter bw = new BinaryWriter(stream);

                    // Write the data type
                    bw.Write((byte)130);

                    // Write the length
                    if (data.Length <= 125)
                    {
                        bw.Write((byte)data.Length);
                    }
                    else if (data.Length >= 126 && data.Length <= 65535)
                    {
                        bw.Write((byte)126);
                        bw.Write((ushort)IPAddress.NetworkToHostOrder((short)data.Length));
                    }
                    else
                    {
                        bw.Write((byte)127);
                        bw.Write((ulong)IPAddress.NetworkToHostOrder(data.LongLength));
                    }

                    // Write the data
                    bw.Write(data);

                    // Send to the client
                    byte[] output = stream.ToArray();
                    mClient.GetStream().BeginWrite(output, 0, output.Length, new AsyncCallback(SendCB), this);
                }
                else
                {
                    // Just send the data
                    mClient.GetStream().BeginWrite(data, 0, data.Length, new AsyncCallback(SendCB), this);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                mClient.Close();
            }
        }