Пример #1
0
        void socketLintener_Disconnected(object sender, ConnectionEventArgs e)
        {
            try
            {
                ClientConnection clientConnection;
                if (clientConnections.TryGetValue(e.Socket, out clientConnection))
                {
                    clientConnections.Remove(clientConnection.SSID);
                    TraceLog.ReleaseWriteDebug("断开 IP:{0},ssid:{1}", clientConnection.Socket.RemoteEndPoint, clientConnection.SSID);

                    if (clientConnection.ServerId != 0)
                    {
                        NameValueCollection requestParam = new NameValueCollection();
                        requestParam["actionid"] = ((int)ActionEnum.Interrupt).ToString();
                        requestParam["ssid"]     = clientConnection.SSID.ToString("N");
                        requestParam["msgid"]    = "0";
                        string paramStr  = RequestParse.ToQueryString(requestParam);
                        byte[] paramData = Encoding.ASCII.GetBytes(paramStr);
                        try
                        {
                            gsConnectionManager.Send(clientConnection.GameId, clientConnection.ServerId, paramData);
                        }
                        catch (Exception ex)
                        {
                            TraceLog.WriteError("Send to tcp disconnected notify failed:{0}\r\nparam:{1}", ex, paramStr);
                        }
                    }
                }
                else
                {
                    TraceLog.ReleaseWriteDebug("断开 IP:{0}。", e.Socket.RemoteEndPoint);
                }
            }
            catch (Exception err)
            {
                TraceLog.WriteError("Disconnected:{0}", err);
            }
        }
Пример #2
0
        void socketLintener_Disconnected(object sender, ConnectionEventArgs e)
        {
            ClientConnection clientConnection;

            if (clientConnections.TryGetValue(e.Socket, out clientConnection))
            {
                clientConnections.Remove(clientConnection.SSID);
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("断开 IP:{0},ssid:{1}", clientConnection.Socket.RemoteEndPoint, clientConnection.SSID);
                }

                if (clientConnection.ServerId != 0)
                {
                    NameValueCollection requestParam = new NameValueCollection();
                    requestParam["actionid"] = "2";
                    requestParam["ssid"]     = clientConnection.SSID.ToString("N");
                    requestParam["msgid"]    = "0";

                    byte[] paramData = Encoding.ASCII.GetBytes(RequestParse.ToQueryString(requestParam));

                    try
                    {
                        gsConnectionManager.Send(clientConnection.GameId, clientConnection.ServerId, paramData);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("发送tcp连接断开通知失败。", ex);
                    }
                }
            }
            else
            {
                Logger.Debug("断开 IP:{0}。", e.Socket.RemoteEndPoint);
            }
        }
Пример #3
0
        public void ListenerCallback(IAsyncResult result)
        {
            HttpListener        listener = (HttpListener)result.AsyncState;
            HttpListenerContext context  = listener.EndGetContext(result);

            listener.BeginGetContext(ListenerCallback, listener);

            var ssid = Guid.NewGuid();
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            string data = "";

            if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                if (string.Compare(request.HttpMethod, "get", true) == 0)
                {
                    data = request.RawUrl.Substring(8);
                    data = HttpUtility.UrlDecode(data);
                }
            }
            else
            {
                data = request.QueryString["d"];
            }

            if (string.IsNullOrEmpty(data))
            {
                using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
                {
                    data = reader.ReadToEnd();
                    data = HttpUtility.ParseQueryString(data)["d"];
                }
            }

            int gameId, serverId, statuscode;
            var requestParam = RequestParse.Parse(request.RemoteEndPoint.Address.ToString(), request.RawUrl, data, out gameId, out serverId, out statuscode);

            if (statuscode != (int)HttpStatusCode.OK)
            {
                response.StatusCode = statuscode;
                response.Close();
                return;
            }

            requestParam["UserHostAddress"] = request.RemoteEndPoint.Address.ToString();
            requestParam["ssid"]            = ssid.ToString("N");
            requestParam["http"]            = "1";

            var clientConnection = new HttpClientConnection {
                Context = context, SSID = ssid, Param = requestParam
            };

            clientConnection.TimeoutTimer = new Timer(TimeoutSendback, clientConnection, httpProxyTimeout, Timeout.Infinite);
            byte[] paramData = Encoding.ASCII.GetBytes(RequestParse.ToQueryString(requestParam));
            pool[ssid] = clientConnection;

            try
            {
                gsConnectionManager.Send(gameId, serverId, paramData);
            }
            catch (Exception ex)
            {
                TraceLog.WriteError("无法连接游服error:{0}", ex);
                var responseData = RequestParse.CtorErrMsg(errmsg, requestParam);
                SendDataBack(ssid, responseData, 0, responseData.Length);
            }
        }