void DataReceived(object sender, SocketEventArgs e) { var bytes = new byte[16]; Buffer.BlockCopy(e.Data, 0, bytes, 0, 16); var ssid = new Guid(bytes); var data = e.Data; var sendResult = proxy.SendDataBack(ssid, e.Data, 16, e.Data.Length - 16); if (!sendResult) { Task.Factory.StartNew(() => { NameValueCollection requestParam = new NameValueCollection(); requestParam["actionid"] = "2"; requestParam["ssid"] = ssid.ToString("N"); requestParam["msgid"] = "0"; byte[] paramData = Encoding.ASCII.GetBytes(RequestParse.ToQueryString(requestParam)); try { Send(paramData); } catch (Exception ex) { logger.Error("发送tcp连接断开通知失败。", ex); } }); } }
/// <summary> /// 心跳包 /// </summary> /// <param name="state"></param> private void DoCheckHeartbeat(object state) { try { NameValueCollection requestParam = new NameValueCollection(); requestParam["actionid"] = ((int)ActionEnum.Heartbeat).ToString(); requestParam["ssid"] = _ssid.ToString("N"); requestParam["msgid"] = "0"; requestParam["isproxy"] = "true"; string paramStr = RequestParse.ToQueryString(requestParam); byte[] paramData = Encoding.ASCII.GetBytes(paramStr); try { Send(paramData); } catch (Exception ex) { TraceLog.WriteError("连接游戏服中断error:{0}\r\nParam:{1}", ex, paramStr); } } catch (Exception ex) { TraceLog.ReleaseWriteDebug("心跳包连接游戏服失败,error:{0}", ex); } }
public void FlushConnected() { foreach (var kv in pool) { var responseData = RequestParse.CtorErrMsg(errmsg, kv.Value.Param); SendDataBack(kv.Value.SSID, responseData, 0, responseData.Length); } }
private void TimeoutSendback(object state) { HttpClientConnection clientConnection = (HttpClientConnection)state; NameValueCollection requestParam = clientConnection.Param; var responseData = RequestParse.CtorErrMsg(errmsg, requestParam); TraceLog.WriteError("超时无法连接游服:{0}", RequestParse.ToQueryString(requestParam)); SendDataBack(clientConnection.SSID, responseData, 0, responseData.Length); }
private void TimeoutSendback(object state) { HttpClientConnection clientConnection = (HttpClientConnection)state; NameValueCollection requestParam = clientConnection.Param; Logger.Error("超时无法连接游服"); var responseData = RequestParse.CtorErrMsg(errmsg, requestParam); SendDataBack(clientConnection.SSID, responseData, 0, responseData.Length); }
void socketLintener_DataReceived(object sender, ConnectionEventArgs e) { var data = Encoding.UTF8.GetString(e.Data); int index = data.LastIndexOf("?d="); if (index > 0) { data = data.Substring(index, data.Length - index); } data = HttpUtility.ParseQueryString(data)["d"]; int gameId, serverId, statuscode; var ip = e.Socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0]; var requestParam = RequestParse.Parse(ip, "", data, out gameId, out serverId, out statuscode); if (statuscode != (int)HttpStatusCode.OK) {// 接收到非法数据 listener.CloseSocket(e.Socket); return; } ClientConnection clientConnection; if (!clientConnections.TryGetValue(e.Socket, out clientConnection)) { Logger.Warn("接收到不在连接池中的socket数据,哪里有bug。"); listener.CloseSocket(e.Socket); return; } if (clientConnection.GameId == 0) { clientConnection.GameId = gameId; } if (clientConnection.ServerId == 0) { clientConnection.ServerId = serverId; } requestParam["UserHostAddress"] = ip; requestParam["ssid"] = clientConnection.SSID.ToString("N"); requestParam["http"] = "0"; byte[] paramData = Encoding.ASCII.GetBytes(RequestParse.ToQueryString(requestParam)); try { gsConnectionManager.Send(gameId, serverId, paramData); } catch (Exception ex) { Logger.Error("无法连接游服。", ex); var responseData = RequestParse.CtorErrMsg(errmsg, requestParam); SendDataBack(clientConnection.SSID, responseData, 0, responseData.Length); } }
private void TimeoutSendback(object state) { try { HttpClientConnection clientConnection = (HttpClientConnection)state; NameValueCollection requestParam = clientConnection.Param; var responseData = RequestParse.CtorErrMsg(10000, RequestParse.ErrorMsgConnectTimeout, requestParam); TraceLog.WriteError("超时无法连接游服:{0}", RequestParse.ToQueryString(requestParam)); SendDataBack(clientConnection.SSID, responseData, 0, responseData.Length); } catch (Exception ex) { TraceLog.WriteError("Http Proxy Timeout Sendback:{0}", ex); } }
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); } }
void DataReceived(object sender, SocketEventArgs e) { try { var bytes = new byte[16]; Buffer.BlockCopy(e.Data, 0, bytes, 0, 16); var ssid = new Guid(bytes); var sendResult = proxy.SendDataBack(ssid, e.Data, 16, e.Data.Length - 16); if (!sendResult) { Task.Factory.StartNew(() => { string paramStr = ""; try { NameValueCollection requestParam = new NameValueCollection(); //连接中断通知游戏服 requestParam["actionid"] = ((int)ActionEnum.Interrupt).ToString(); requestParam["ssid"] = ssid.ToString("N"); requestParam["msgid"] = "0"; paramStr = RequestParse.ToQueryString(requestParam); byte[] paramData = Encoding.ASCII.GetBytes(paramStr); Send(paramData); } catch (Exception ex) { TraceLog.WriteError("连接中断通知游戏服error:{0}\r\nParam:{1}", ex, paramStr); } }); } } catch (Exception er) { TraceLog.WriteError("DataReceived error:{0}", er); } }
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); } }
void socketLintener_DataReceived(object sender, ConnectionEventArgs e) { var data = Encoding.ASCII.GetString(e.Data); try { string routeName = string.Empty; int index = data.LastIndexOf("?d="); if (index > 0) { if (data.StartsWith("route:", StringComparison.CurrentCultureIgnoreCase)) { routeName = data.Substring(6, index - 6); } data = data.Substring(index, data.Length - index); } data = HttpUtility.ParseQueryString(data)["d"]; int gameId, serverId, statuscode; var ip = e.Socket.RemoteEndPoint.ToString().Split(new char[] { ':' })[0]; var requestParam = RequestParse.Parse(ip, "", data, out gameId, out serverId, out statuscode); if (statuscode != (int)HttpStatusCode.OK) {// 接收到非法数据 listener.CloseSocket(e.Socket); return; } ClientConnection clientConnection; if (!clientConnections.TryGetValue(e.Socket, out clientConnection)) { TraceLog.WriteError("接收到不在连接池中的socket数据,哪里有bug。"); listener.CloseSocket(e.Socket); return; } if (clientConnection.GameId == 0) { clientConnection.GameId = gameId; } if (clientConnection.ServerId == 0) { clientConnection.ServerId = serverId; } requestParam["UserHostAddress"] = ip; requestParam["ssid"] = clientConnection.SSID.ToString("N"); requestParam["http"] = "0"; string paramStr = string.Format("{0}&UserHostAddress={1}&ssid={2}&http=0", data, ip, requestParam["ssid"]); if (!string.IsNullOrEmpty(routeName)) { requestParam["route"] = routeName; paramStr += "&route=" + routeName; } byte[] paramData = Encoding.ASCII.GetBytes(paramStr); try { gsConnectionManager.Send(gameId, serverId, paramData); } catch (Exception ex) { TraceLog.WriteError("无法连接游服error:{0}\r\nparam:{1}", ex, paramStr); var responseData = RequestParse.CtorErrMsg(errmsg, requestParam); SendDataBack(clientConnection.SSID, responseData, 0, responseData.Length); } } catch (Exception err) { TraceLog.WriteError("DataReceived error:{0},param:{1}", err, data); } }
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); } }