protected override void OnConnectCompleted(object sender, ConnectionEventArgs e) { Console.WriteLine("Client:{0} connect to server.", e.Socket.RemoteEndPoint); }
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); } }
protected override void OnConnectCompleted(object sender, ConnectionEventArgs e) { }
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); } }
void socketLintener_Connected(object sender, ConnectionEventArgs e) { var ssid = Guid.NewGuid(); var clientConnection = new ClientConnection { SSID = ssid, Socket = e.Socket }; if (Logger.IsDebugEnabled) { Logger.Debug("连接 IP:{0},ssid:{1}", e.Socket.RemoteEndPoint, ssid); } clientConnections.Add(ssid, e.Socket, clientConnection); }
private void OnDisconnected(ConnectionEventArgs e) { if (Disconnected != null) { Disconnected(this, e); } }
private void OnDataReceived(ConnectionEventArgs e) { if (DataReceived != null) { DataReceived(this, e); } }
private void OnConnected(ConnectionEventArgs e) { if (Connected != null) { Connected(this, e); } }