public int SendSync(BufferSegment segBuff)
 {
     return(sock.Send(segBuff.buffer, segBuff.offset, segBuff.count, SocketFlags.None));
 }
 public bool SendAsync(BufferSegment seg, IPEndPoint remoteEP, bool waiting = true)
 {
     return(_socketSend.Send(seg, remoteEP, waiting));
 }
 public int SendSync(IPEndPoint remoteEP, BufferSegment seg)
 {
     return(_socketSend.SendSync(seg, remoteEP));
 }
Example #4
0
        public bool DecodingFromBytes(BufferSegment data, bool isMaskResolve = true)
        {
            if (data.count < 4)
            {
                return(false);
            }

            int pos = data.offset;

            IsEof  = (data.buffer[pos] >> 7) == 1;
            OpCode = (byte)(data.buffer[pos] & 0xf);

            Mask          = (data.buffer[++pos] >> 7) == 1;
            PayloadLength = (data.buffer[pos] & 0x7f);

            //校验截取长度
            if (PayloadLength >= data.count)
            {
                return(false);
            }

            ++pos;
            //数据包长度超过126,需要解析附加数据
            if (PayloadLength < 126)
            {
                //直接等于消息长度
            }
            if (PayloadLength == 126)
            {
                PayloadLength = data.buffer.ToUInt16(pos);// BitConverter.ToUInt16(segOffset.buffer, pos);
                pos          += 2;
            }
            else if (PayloadLength == 127)
            {
                PayloadLength = (long)data.buffer.ToUInt64(pos);
                pos          += 8;
            }

            Payload = new BufferSegment()
            {
                offset = pos,
                buffer = data.buffer,
                count  = (int)PayloadLength
            };

            //数据体
            if (Mask)
            {
                //获取掩码密钥
                MaskKey    = new byte[4];
                MaskKey[0] = data.buffer[pos];
                MaskKey[1] = data.buffer[pos + 1];
                MaskKey[2] = data.buffer[pos + 2];
                MaskKey[3] = data.buffer[pos + 3];
                pos       += 4;

                Payload.buffer = data.buffer;
                Payload.offset = pos;
                if (isMaskResolve)
                {
                    long p = 0;

                    for (long i = 0; i < PayloadLength; ++i)
                    {
                        p = (long)pos + i;

                        Payload.buffer[p] = (byte)(Payload.buffer[p] ^ MaskKey[i % 4]);
                    }
                }
            }
            else
            {
                Payload.buffer = data.buffer;
                Payload.offset = pos;
            }

            return(true);
        }
Example #5
0
        public void SendPong(BufferSegment buf)
        {
            var seg = new WebsocketFrame().ToSegmentFrame(buf, OpCodeType.Bong);

            _token.SendAsync(seg, true);
        }
Example #6
0
        private void OnReceieve(SocketToken token, BufferSegment seg)
        {
            var connection = ConnectionPool.Find(x => x.sToken.tokenId == token.tokenId);

            if (connection == null)
            {
                connection = new ConnectionInfo()
                {
                    sToken = token
                };

                ConnectionPool.Add(connection);
            }

            if (connection.IsHandShaked == false)
            {
                var serverFrame = new WebsocketFrame();

                var access = serverFrame.GetHandshakePackage(seg);
                connection.IsHandShaked = access.IsHandShaked();

                if (connection.IsHandShaked == false)
                {
                    CloseAndRemove(connection);
                    return;
                }
                connection.ConnectedTime = DateTime.Now;

                var rsp = serverFrame.RspAcceptedFrame(access);

                this.token.SendAsync(token, rsp);

                connection.accessInfo = access;

                if (onAccept != null)
                {
                    onAccept(token);
                }
            }
            else
            {
                RefreshTimeout(token);

                WebsocketFrame packet = new WebsocketFrame();
                bool           isOk   = packet.DecodingFromBytes(seg, true);
                if (isOk == false)
                {
                    return;
                }

                if (packet.OpCode == 0x01)//text
                {
                    if (onRecieveString != null)
                    {
                        onRecieveString(token, encoding.GetString(seg.buffer,
                                                                  seg.offset, seg.count));
                    }

                    return;
                }
                else if (packet.OpCode == 0x08)//close
                {
                    CloseAndRemove(connection);
                    return;
                }
                else if (packet.OpCode == 0x09)//ping
                {
                    SendPong(token, seg);
                }
                else if (packet.OpCode == 0x0A)//pong
                {
                    //  SendPing(session.sToken);
                }

                if (onReceieve != null && packet.Payload.count > 0)
                {
                    onReceieve(token, packet.Payload);
                }
            }
        }
Example #7
0
        private void SendPong(SocketToken token, BufferSegment seg)
        {
            var buffer = new WebsocketFrame().ToSegmentFrame(seg, OpCodeType.Bong);

            this.token.SendAsync(token, buffer);
        }
Example #8
0
 public bool Send(SocketToken token, BufferSegment seg, bool waiting = true)
 {
     return(this.token.SendAsync(token, seg, waiting));
 }
Example #9
0
 public int SendSync(SocketToken token, BufferSegment seg)
 {
     return(token.sock.Send(seg.buffer, seg.offset, seg.count, SocketFlags.None));
 }
Example #10
0
 public int SendSync(BufferSegment segBuff, IPEndPoint remoteEP)
 {
     return(_sock.SendTo(segBuff.buffer, segBuff.offset, segBuff.count, SocketFlags.None, remoteEP));
 }