Exemplo n.º 1
0
        private void OnSendComplete(object o)
        {
            if (this.IsDisposed)
            {
                throw new Exception("TChannel已经被Dispose, 不能发送消息");
            }
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                Log.Error($"session disposed!");
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                this.OnError((int)e.SocketError);
                return;
            }
            this.sendBuffer.FirstIndex += e.BytesTransferred;
            if (this.sendBuffer.FirstIndex == this.sendBuffer.ChunkSize)
            {
                this.sendBuffer.FirstIndex = 0;
                this.sendBuffer.RemoveFirst();
            }

            this.StartSend();
        }
Exemplo n.º 2
0
        private void OnConnectComplete(object o)
        {
            if (this.IsDisposed)
            {
                throw new Exception("TChannel已经被Dispose, 不能发送消息");
            }
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                Log.Error($"session disposed!");
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                this.OnError((int)e.SocketError);
                return;
            }

            e.RemoteEndPoint = null;
            this.isConnected = true;

            this.Start();
        }
Exemplo n.º 3
0
        private void OnAcceptComplete(object sender, SocketAsyncEventArgs o)
        {
            SocketAsyncEventArgs e             = o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                Log.Error($"accept error {e.SocketError}");
                return;
            }
            TChannel channel = new TChannel(e.AcceptSocket, this);

            this.idChannels[channel.Id] = channel;

            try
            {
                this.OnAccept(channel);
            }
            catch (Exception exception)
            {
                Log.Error(exception);
            }

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }

            this.AcceptAsync();
        }
Exemplo n.º 4
0
        private void OnRecvComplete(object o)
        {
            if (this.IsDisposed)
            {
                throw new Exception("TChannel已经被Dispose, 不能发送消息");
            }
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                Log.Error($"session disposed!");
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                this.OnError((int)e.SocketError);
                return;
            }

            if (e.BytesTransferred == 0)
            {
                this.OnError((int)e.SocketError);
                return;
            }

            this.recvBuffer.LastIndex += e.BytesTransferred;
            if (this.recvBuffer.LastIndex == this.recvBuffer.ChunkSize)
            {
                this.recvBuffer.AddLast();
                this.recvBuffer.LastIndex = 0;
            }

            // 收到消息回调
            while (true)
            {
                if (!this.parser.Parse())
                {
                    break;
                }

                Packet packet = this.parser.GetPacket();
                try
                {
                    this.OnRead(packet);
                }
                catch (Exception exception)
                {
                    Log.Error(exception);
                }
            }

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }
            this.StartRecv();
        }
Exemplo n.º 5
0
        private void OnConnectComplete(object o)
        {
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                Log.Debug("err:%v" + e.SocketError);
                this.OnError((int)e.SocketError);
                return;
            }
            this.isConnected = true;

            this.StartSend();
            this.StartRecv();
        }
Exemplo n.º 6
0
        private void OnConnectComplete(object o)
        {
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                this.OnError((int)e.SocketError);
                return;
            }

            e.RemoteEndPoint = null;
            this.isConnected = true;

            this.Start();
        }
Exemplo n.º 7
0
        private void OnSendComplete(object o)
        {
            SocketAsyncEventArgs e             = (SocketAsyncEventArgs)o;
            UserTokenInfo        userTokenInfo = (UserTokenInfo)e.UserToken;

            if (userTokenInfo.InstanceId != this.InstanceId)
            {
                return;
            }

            if (e.SocketError != SocketError.Success)
            {
                this.OnError((int)e.SocketError);
                return;
            }
            this.sendBuffer.FirstIndex += e.BytesTransferred;
            if (this.sendBuffer.FirstIndex == this.sendBuffer.ChunkSize)
            {
                this.sendBuffer.FirstIndex = 0;
                this.sendBuffer.RemoveFirst();
            }

            this.StartSend();
        }