示例#1
0
文件: Program.cs 项目: 9tong/Scut
 protected override void OnConnectCompleted(object sender, ConnectionEventArgs e)
 {
     Console.WriteLine("Client:{0} connect to server.", e.Socket.RemoteEndPoint);
 }
示例#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
 protected override void OnConnectCompleted(object sender, ConnectionEventArgs e)
 {
 }
示例#4
0
        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);
            }
        }
示例#5
0
        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);
        }
示例#6
0
 private void OnDisconnected(ConnectionEventArgs e)
 {
     if (Disconnected != null)
     {
         Disconnected(this, e);
     }
 }
示例#7
0
 private void OnDataReceived(ConnectionEventArgs e)
 {
     if (DataReceived != null)
     {
         DataReceived(this, e);
     }
 }
示例#8
0
 private void OnConnected(ConnectionEventArgs e)
 {
     if (Connected != null)
     {
         Connected(this, e);
     }
 }