예제 #1
0
 public override void Stop()
 {
     if (channel != null)
     {
         ChangeState(ChannelState.Disconnecting);
         channel.Close();
     }
     else
     {
         base.Stop();
     }
 }
예제 #2
0
 /// <summary>
 /// 解析数据并且传回
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="channel"></param>
 public static void AnalysisRequest <T>(NetChannel <T> channel)
 {
     //
     if (channel.recData == null)
     {
         return;
     }
     else
     {
         if (typeof(byte[]) == channel.recData.GetType())
         {
             object data = channel.recData;
             byte[] req  = (byte[])data;
             //说明是HTTP
             if (serviceHttp == null)
             {
                 serviceHttp = new ServiceHttpProcessor();
             }
             HttpRequest request = serviceHttp.ProcessRequestReturn(req);
             SrvRquest   srvReq  = null;
             object      result  = null;
             string      json    = null;
             if (request != null)
             {
                 srvReq = CreateSrvReq(request);
                 result = InvokeService(srvReq);
             }
             else
             {
                 json = "解析HTTP错误,无法获取请求的参数或者服务名称";
             }
             if (result != null)
             {
                 json = StreamSerializer.JSONObjectToString <object>(result);
             }
             int streamLen = 1000;
             if (!string.IsNullOrEmpty(json))
             {
                 streamLen = json.Length * 2;
             }
             StreamBuffer stream = HTTPStream.GetInstance().GetStreamCompare(streamLen);
             serviceHttp.ProcessResult(json, request, stream.Buffer);
             stream.ResetSize((int)stream.Buffer.Length);
             stream.ResetPostion();             //重置位置;
             int        len      = stream.Size; //数据长度
             byte[]     tmp      = null;
             ByteBuffer buffer   = null;
             int        position = 0;
             if (len < BufferManager.GetInstance().BufferSize)
             {
                 //取出缓存Buffer
                 buffer   = BufferManager.GetInstance().GetBufferNum(1)[0];
                 tmp      = buffer.buffer;
                 position = buffer.Position;
             }
             else
             {
                 tmp = new byte[len];
             }
             ISocketChannel socket = channel.channel as ISocketChannel;
             if (socket != null)
             {
                 stream.Buffer.Read(tmp, position, len);
                 socket.SendData(tmp, position, len);
                 socket.Close();
             }
             if (buffer != null)
             {
                 //释放取出的缓存
                 BufferManager.GetInstance().FreeBuffer(buffer);
             }
             HTTPStream.GetInstance().FreeBuffer(stream);
         }
         else
         {
             object    data   = channel.recData;
             SrvRquest req    = (SrvRquest)data;
             object    result = InvokeService(req);
             if (result != null)
             {
                 ISocketClient <T> socket = channel.channel as ISocketClient <T>;
                 if (socket != null)
                 {
                     socket.SendData(result);
                 }
                 socket.Close();
             }
         }
     }
 }
예제 #3
0
 /// <summary>
 /// 关闭
 /// </summary>
 public void Close()
 {
     channel.Close();
 }
예제 #4
0
        public PlayerConnection(BasePlayer player, ISocketChannel channel)
        {
            _serializer   = new BinarySerializer();
            _deserializer = new BinaryDeserializer();

            _player          = player;
            _channel         = channel;
            _messageProtocol = ProtocolType.Auto;

            _player.IPAddress = IPAddress.Parse(_channel.RemoteEndPoint.ToString());

            _deserializer.OnDeserializedMessage += (message) => {
                if (!_connected)
                {
                    if (message.Type == "join")
                    {
                        // find the game from the decrypted joinKey.
                        var joinKey = JoinKey.Decode(message.GetString(0));

                        if (joinKey != null)
                        {
                            var game = _player.Host.Games.FirstOrDefault(g => g.RoomType == joinKey.ServerType);

                            if (game != null)
                            {
                                _connected = true;

                                game.RoomId = joinKey.RoomId;

                                _player.ConnectUserId = joinKey.ConnectUserId;

                                _player.Game = game;
                                _player.Game.UserJoined(_player);

                                this.Send(new Message("playerio.joinresult", true));
                                return;
                            }
                        }

                        this.Send(new Message("playerio.joinresult", false));
                        _channel.Close();
                    }

                    return;
                }

                _player.Game.GotMessage(_player, message);
            };

            _channel.Closed += (s, e) => {
                _connected = false;

                if (_player.Game != null)
                {
                    _player.Game.Disconnect(player);
                }
            };

            _channel.BytesReceived += (s, bytes) => {
                switch (_messageProtocol)
                {
                case ProtocolType.Auto:
                    switch (bytes[0])
                    {
                    case 0: _messageProtocol = ProtocolType.Binary; break;
                    }

                    if (bytes.Length > 1)
                    {
                        _deserializer.AddBytes(bytes.Skip(1).ToArray());
                    }
                    break;

                case ProtocolType.Binary:
                    _deserializer.AddBytes(bytes);
                    break;
                }
            };
        }