Beispiel #1
0
 public SFrameMaskData(int DataLength, int KeyIndex, int TotalLenght, EOpcodeType Opcode)
 {
     this.DataLength  = DataLength;
     this.KeyIndex    = KeyIndex;
     this.TotalLenght = TotalLenght;
     this.Opcode      = Opcode;
 }
        public static ArraySegment <byte> GetFrameFromPacket(Packet message, EOpcodeType opcode = EOpcodeType.Binary)
        {
            byte[] response;
            byte[] bytesRaw = message != null?message.ProtoSerialize() : Array.Empty <byte>();

            byte[] frame  = new byte[10];
            int    length = bytesRaw.Length;

            frame[0] = (byte)(128 + (int)opcode);

            int indexStartRawData;

            if (length <= 125)
            {
                frame[1]          = (byte)length;
                indexStartRawData = 2;
            }
            else if (length >= 126 && length <= 65535)
            {
                frame[1]          = (byte)126;
                frame[2]          = (byte)((length >> 8) & 255);
                frame[3]          = (byte)(length & 255);
                indexStartRawData = 4;
            }
            else
            {
                frame[1] = (byte)127;
                frame[2] = (byte)((length >> 56) & 255);
                frame[3] = (byte)((length >> 48) & 255);
                frame[4] = (byte)((length >> 40) & 255);
                frame[5] = (byte)((length >> 32) & 255);
                frame[6] = (byte)((length >> 24) & 255);
                frame[7] = (byte)((length >> 16) & 255);
                frame[8] = (byte)((length >> 8) & 255);
                frame[9] = (byte)(length & 255);

                indexStartRawData = 10;
            }

            response = new byte[indexStartRawData + length];

            int i, reponseIdx = 0;

            //Add the frame bytes to the reponse
            for (i = 0; i < indexStartRawData; i++)
            {
                response[reponseIdx] = frame[i];
                reponseIdx++;
            }

            //Add the data bytes to the response
            for (i = 0; i < length; i++)
            {
                response[reponseIdx] = bytesRaw[i];
                reponseIdx++;
            }

            return(new ArraySegment <byte>(response));
        }
Beispiel #3
0
        private static byte[] GetFrameFromString(string Message, EOpcodeType Opcode = EOpcodeType.Text)
        {
            byte[] response;
            byte[] bytesRaw = Encoding.Default.GetBytes(Message);
            byte[] frame    = new byte[10];

            int indexStartRawData = -1;
            int length            = bytesRaw.Length;

            frame[0] = (byte)(128 + (int)Opcode);
            if (length <= 125)
            {
                frame[1]          = (byte)length;
                indexStartRawData = 2;
            }
            else if (length >= 126 && length <= 65535)
            {
                frame[1]          = (byte)126;
                frame[2]          = (byte)((length >> 8) & 255);
                frame[3]          = (byte)(length & 255);
                indexStartRawData = 4;
            }
            else
            {
                frame[1] = (byte)127;
                frame[2] = (byte)((length >> 56) & 255);
                frame[3] = (byte)((length >> 48) & 255);
                frame[4] = (byte)((length >> 40) & 255);
                frame[5] = (byte)((length >> 32) & 255);
                frame[6] = (byte)((length >> 24) & 255);
                frame[7] = (byte)((length >> 16) & 255);
                frame[8] = (byte)((length >> 8) & 255);
                frame[9] = (byte)(length & 255);

                indexStartRawData = 10;
            }

            response = new byte[indexStartRawData + length];

            int i, reponseIdx = 0;

            //Add the frame bytes to the reponse
            for (i = 0; i < indexStartRawData; i++)
            {
                response[reponseIdx] = frame[i];
                reponseIdx++;
            }

            //Add the data bytes to the response
            for (i = 0; i < length; i++)
            {
                response[reponseIdx] = bytesRaw[i];
                reponseIdx++;
            }

            Logger.Debug($"Produced Frame(Message:`{Message}`, OpCode=`{Opcode}`)");
            return(response);
        }
Beispiel #4
0
        public static ArraySegment <byte> GetFrameFromString(string message, EOpcodeType opcode = EOpcodeType.Text)
        {
            byte[] response;
            byte[] bytesRaw = Encoding.Default.GetBytes(message);
            byte[] frame    = new byte[10];
            int    length   = bytesRaw.Length;

            frame[0] = (byte)(128 + (int)opcode);

            int indexStartRawData;

            if (length <= 125)
            {
                frame[1]          = (byte)length;
                indexStartRawData = 2;
            }
            else if (length >= 126 && length <= 65535)
            {
                frame[1]          = (byte)126;
                frame[2]          = (byte)((length >> 8) & 255);
                frame[3]          = (byte)(length & 255);
                indexStartRawData = 4;
            }
            else
            {
                frame[1] = (byte)127;
                frame[2] = (byte)((length >> 56) & 255);
                frame[3] = (byte)((length >> 48) & 255);
                frame[4] = (byte)((length >> 40) & 255);
                frame[5] = (byte)((length >> 32) & 255);
                frame[6] = (byte)((length >> 24) & 255);
                frame[7] = (byte)((length >> 16) & 255);
                frame[8] = (byte)((length >> 8) & 255);
                frame[9] = (byte)(length & 255);

                indexStartRawData = 10;
            }

            response = new byte[indexStartRawData + length];

            int i, reponseIdx = 0;

            //Add the frame bytes to the reponse
            for (i = 0; i < indexStartRawData; i++)
            {
                response[reponseIdx] = frame[i];
                reponseIdx++;
            }

            //Add the data bytes to the response
            for (i = 0; i < length; i++)
            {
                response[reponseIdx] = bytesRaw[i];
                reponseIdx++;
            }

            return(new ArraySegment <byte>(response));
        }
        private void messageCallback(IAsyncResult ar)
        {
            var sw = ar.AsyncState as SocketWrapper;

            try
            {
                sw.workSocket.EndReceive(ar);

                int bytesReceived = sw.workSocket.Receive(sw.buffer);
                if (bytesReceived < sw.buffer.Length)
                {
                    Array.Resize <byte>(ref sw.buffer, bytesReceived);
                }

                if (bytesReceived == 0)
                {
                    AddMessage(String.Format("{0} 客户端退出", sw.workSocket.RemoteEndPoint.ToString()));
                    proxSockets.Remove(sw);
                }
                else
                {
                    EOpcodeType opcode = WebSocketHelper.GetFrameOpcode(sw.buffer);

                    // If the connection was closed
                    if (opcode == EOpcodeType.ClosedConnection)
                    {
                        sw.workSocket.Close();
                        return;
                    }

                    AddMessage(string.Format("接收到客户端:{0} 的消息是:{1}", sw.workSocket.RemoteEndPoint.ToString(), WebSocketHelper.GetDataFromFrame(sw.buffer)));
                    sw.buffer = new Byte[SocketWrapper.BUFFER_SIZE];
                    sw.workSocket.BeginReceive(new byte[0], 0, 0, SocketFlags.None, messageCallback, sw);
                }
            }
            catch (Exception ex)
            {
                sw.workSocket.Close();
                sw.workSocket.Dispose();
                Console.WriteLine(ex.Message);
            }
        }
Beispiel #6
0
        /// <summary>Called when a message was received from the client</summary>
        private void messageCallback(IAsyncResult AsyncResult)
        {
            try
            {
                GetSocket().EndReceive(AsyncResult);

                // Read the incomming message
                byte[] messageBuffer = new byte[2048];
                int    bytesReceived = GetSocket().Receive(messageBuffer);

                // Resize the byte array to remove whitespaces
                if (bytesReceived < messageBuffer.Length)
                {
                    Array.Resize <byte>(ref messageBuffer, bytesReceived);
                }

                // Get the opcode of the frame
                EOpcodeType opcode = Helpers.GetFrameOpcode(messageBuffer);

                // If the connection was closed
                if (opcode == EOpcodeType.ClosedConnection)
                {
                    GetServer().ClientDisconnect(this);
                    return;
                }

                // Pass the message to the server event to handle the logic
                GetServer().ReceiveMessage(this, Helpers.GetDataFromFrame(messageBuffer));

                // Start to receive messages again
                GetSocket().BeginReceive(new byte[] { 0 }, 0, 0, SocketFlags.None, messageCallback, null);
            }
            catch (Exception Exception)
            {
                GetSocket().Close();
                GetSocket().Dispose();
                GetServer().ClientDisconnect(this);
            }
        }