public void Init(EndPoint endpoint, int MaxReceiveLength) { m_OPTypeLength = 1; m_OPCodeLength = 1; m_ParaCountLength = 1; m_PerParaCountLength = 2; UserCommandBase[] baseArray = new UserCommandBase[] { new DoLoginResult(), new DoUserJoin(), new DoGetAllUser(), new DoGetUser(), new DoWhisper(), new DoBroadCast(), new DoUserLeave(), new DoUserStartCreateChat(), }; foreach (UserCommandBase base2 in baseArray) { this.m_CommandDict.Add(base2.Name, base2); } MyAsyncTcpSession session = new MyAsyncTcpSession(endpoint, MaxReceiveLength); session.Error += session_Error; session.DataReceived += session_DataReceived; session.Connected += session_Connected; session.Closed += session_Closed; session.GetSocket += session_GetSocket; this.Client = session; this.CommandReader = new UserReader(this); }
protected virtual void Dispose(bool disposing) { if (m_Disposed) { return; } if (disposing) { TcpClientSession client = Client; if (client != null) { client.Connected -= client_Connected; client.Closed -= client_Closed; client.Error -= client_Error; client.DataReceived -= client_DataReceived; if (client.IsConnected) { client.Close(); } Client = null; } ClearTimer(); } m_Disposed = true; }
// Token: 0x0600053D RID: 1341 RVA: 0x00018114 File Offset: 0x00016314 public void Close(int statusCode, string reason) { this.m_ClosedArgs = new ClosedEventArgs((short)statusCode, reason); if (Interlocked.CompareExchange(ref this.m_StateCode, 3, -1) == -1) { this.OnClosed(); return; } if (Interlocked.CompareExchange(ref this.m_StateCode, 2, 0) != 0) { this.m_StateCode = 2; this.ClearTimer(); this.m_WebSocketTimer = new Timer(new TimerCallback(this.CheckCloseHandshake), null, 5000, -1); try { this.ProtocolProcessor.SendCloseHandshake(this, statusCode, reason); } catch (Exception e) { if (this.Client != null) { this.OnError(e); } } return; } TcpClientSession client = this.Client; if (client != null && client.IsConnected) { client.Close(); return; } this.OnClosed(); }
protected virtual void Dispose(bool disposing) { if (this.m_disposed) { return; } if (disposing) { TcpClientSession client = this.Client; if (client != null) { client.Connected -= new EventHandler(this.client_Connected); client.Closed -= new EventHandler(this.client_Closed); client.Error -= new EventHandler <ErrorEventArgs>(this.client_Error); client.DataReceived -= new EventHandler <DataEventArgs>(this.client_DataReceived); if (client.IsConnected) { client.Close(); } this.Client = (TcpClientSession)null; } if (this.m_WebSocketTimer != null) { this.m_WebSocketTimer.Dispose(); } } this.m_disposed = true; }
public void Close(int statusCode, string reason) { this.m_ClosedArgs = new ClosedEventArgs((short)statusCode, reason); if (Interlocked.CompareExchange(ref this.m_StateCode, 3, -1) == -1) { this.OnClosed(); } else if (Interlocked.CompareExchange(ref this.m_StateCode, 2, 0) == 0) { TcpClientSession client = this.Client; if (client != null && client.IsConnected) { client.Close(); } else { this.OnClosed(); } } else { this.m_StateCode = 2; this.ClearTimer(); this.m_WebSocketTimer = new Timer(new TimerCallback(this.CheckCloseHandshake), (object)null, 5000, -1); this.ProtocolProcessor.SendCloseHandshake(this, statusCode, reason); } }
private void NewSession(TcpClientSession session) { session.Disconnected += (s, e) => { users.Remove(users.First(u => u.Session == e)); }; }
void targetSession_Connected(object sender, EventArgs e) { m_TargetSession = (AsyncTcpSession)sender; var connectedAction = m_ConnectedAction; m_ConnectedAction = null; connectedAction(this, m_TargetSession); }
private void NewSession(TcpClientSession session) { session.SentCompleted += (s, e) => { IncreaseSentSize(e.Header.DataLength + Header.HEADER_SIZE); if (e.Header.Key == MessageKeys.Literal) { var msg = (BytesMessage)e; Print($"{e.Header.From}:{MessageHelper.LiteralEncoding.GetString(msg.Bytes)}"); } else if (e.Header.Key == MessageKeys.File) { Print($"{e.Header.From}:{e.Header.State}"); } }; session.Received += (s, e) => { if (e.Header.Key == MessageKeys.Literal) { var msg = (BytesMessage)e; Print($"{e.Header.From ?? "system"}:{MessageHelper.LiteralEncoding.GetString(msg.Bytes)}"); } else if (e.Header.Key == MessageKeys.Remote) { Remote(session, e.Header.From); } else if (e.Header.Key == MessageKeys.RemoteStop) { } else if (e.Header.Key == MessageKeys.RemoteImage) { var bytesMesaage = e as BytesMessage; var terminal = RemoteForm.Get(e.Header.From); if (terminal == null) { session.Send(MessageHelper.CreateRemoteStopMessage(this.userName, e.Header.From, null)); return; } if (terminal.Disposing || terminal.IsDisposed) { return; } if (!terminal.Visible) { this.Invoke(new MethodInvoker(() => { terminal.Show(); })); } terminal.Draw(bytesMesaage.Bytes); Task.Run(() => { Thread.Sleep(1000); SendRemoteImageRequest(e.Header.From); }); } }; }
private void OtherProxyConnectedHandle(ProxySession session, TcpClientSession targetSession) { if (targetSession != null) { byte[] data = Encoding.ASCII.GetBytes(m_SendingHeader); m_SendingHeader = null; targetSession.Send(data, 0, data.Length); } }
// Token: 0x0600053F RID: 1343 RVA: 0x00018210 File Offset: 0x00016410 public void CloseWithoutHandshake() { TcpClientSession client = this.Client; if (client != null) { client.Close(); } }
void targetSession_Closed(object sender, EventArgs e) { if (this.Connected) { m_TargetSession = null; this.Close(); return; } }
public void Connect(EndPoint remoteEndPoint) { TcpClientSession session = new TcpClientSession(remoteEndPoint, bufferPoolSize, bufferSize, loger); session.OnReceived += OnReceived; session.OnConnected += OnConnected; session.OnDisConnect += OnDisConnect; session.Connect(); }
// Token: 0x060005C5 RID: 1477 RVA: 0x000198CC File Offset: 0x00017ACC private void SendDataFragment(WebSocket websocket, int opCode, byte[] playloadData, int offset, int length) { byte[] array = this.EncodeDataFrame(opCode, playloadData, offset, length); TcpClientSession client = websocket.Client; if (client != null) { client.Send(array, 0, array.Length); } }
private void ConnectProxyConnectedHandle(ProxySession session, TcpClientSession targetSession) { if (targetSession == null) { session.Send(m_FailedResponse, 0, m_FailedResponse.Length); } else { session.Send(m_OkResponse, 0, m_OkResponse.Length); } }
//--------------------------------------------------------------------- public void Close() { if (mSession != null) { mSession.Dispose(); mSession = null; if (OnSocketClosed != null) { OnSocketClosed.Invoke(null, EventArgs.Empty); } } }
//--------------------------------------------------------------------- public void connect(string ip, int port) { mIp = ip; mPort = port; EndPoint server_address = new IPEndPoint(IPAddress.Parse(mIp), mPort); mSession = new TcpClientSession(server_address); mSession.DataReceived += _onReceive; mSession.Connected += _onConnected; mSession.Closed += _onClosed; mSession.Error += _onError; mSession.connect(); }
// Token: 0x060005C7 RID: 1479 RVA: 0x000198FC File Offset: 0x00017AFC public override void SendData(WebSocket websocket, IList <ArraySegment <byte> > segments) { List <ArraySegment <byte> > list = new List <ArraySegment <byte> >(segments.Count); int num = segments.Count - 1; for (int i = 0; i < segments.Count; i++) { ArraySegment <byte> arraySegment = segments[i]; list.Add(new ArraySegment <byte>(this.EncodeDataFrame((i == 0) ? 2 : 0, i == num, arraySegment.Array, arraySegment.Offset, arraySegment.Count))); } TcpClientSession client = websocket.Client; if (client != null) { client.Send(list); } }
public void ChatConn(string toUserAddr, int toUserPort) { ToUserAddr = toUserAddr; ToUserPort = toUserPort; EndPoint endpoint = new IPEndPoint(IPAddress.Parse(toUserAddr), toUserPort - 1); MyAsyncTcpSession chatsession = new MyAsyncTcpSession(endpoint, 1024 * 10); chatsession.Error += chatsession_Error; chatsession.DataReceived += chatsession_DataReceived; chatsession.Connected += chatsession_Connected; chatsession.Closed += chatsession_Closed; Client = chatsession; Client.Connect(); if (!m_OpenedEvent.WaitOne(5000)) { throw new Exception("连接超时"); } var currentSessionIdData = Encoding.UTF8.GetBytes(CurrentUser.SessionId); this.SendData(UserOP.DoNewChat, currentSessionIdData); }
static void TaskReceive(TcpListener listener) { while (true) { // 接続要求あるかどうか確認 if (listener.Pending()) { // 接続要求を処理する var client = listener.AcceptTcpClient(); // 認証待ち一覧に追加 var session = new TcpClientSession(client); GameState.Instance.AddSession(session); LoggerService.Locator.Info("AcceptTcpClient : {0}", client.Client.RemoteEndPoint); } GameState.Instance.Update(); // TODO : 接続切断したものがあれば削除 //if (sessions.RemoveAll(s => s.Disconnected) > 0) //{ // // TODO : 以下コピペーなのでリファクタリング // var info = new Server2Client.UserInfo(); // info.numUser = sessions.Count; // info.Username = new string[sessions.Count]; // for (var i = 0; i < sessions.Count; ++i) // { // info.Username[i] = sessions[i].userInfo.Username; // } // var bytes = ProtoMaker.Pack(info); // // 削除されたものがあるので情報を再送信 // foreach (var session in sessions) // { // session.Send(bytes); // } //} Task.Delay(16).Wait(); } }
//--------------------------------------------------------------------- public void Connect(string ip_or_host, int port) { mIpOrHost = ip_or_host; mPort = port; IPAddress ip_address = null; bool is_host = !IPAddress.TryParse(mIpOrHost, out ip_address); IPHostEntry host_info = null; if (is_host) { host_info = Dns.GetHostEntry(mIpOrHost); } else { host_info = Dns.GetHostEntry(ip_address); } IPAddress[] ary_IP = host_info.AddressList; if (is_host) { mSession = new TcpClientSession(mIpOrHost, mPort); } else { EndPoint server_address = new IPEndPoint(IPAddress.Parse(mIpOrHost), mPort); mSession = new TcpClientSession(server_address); } mSession.OnDataReceived += _onReceive; mSession.OnConnected += _onConnected; mSession.OnClosed += _onClosed; mSession.OnError += _onError; mSession.Connect(ary_IP, is_host); }
private void Remote(TcpClientSession session, string userName) { // 新建一个和屏幕大小相同的图片 Bitmap catchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height); // 创建一个画板,让我们可以在画板上画图 // 这个画板也就是和屏幕大小一样大的图片 // 我们可以通过Graphics这个类在这个空白图片上画图 Graphics g = Graphics.FromImage(catchBmp); // 把屏幕图片拷贝到我们创建的空白图片 CatchBmp中 g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height)); var stream = new MemoryStream(); catchBmp.Save(stream, ImageFormat.Png); var array = stream.ToArray(); var compressed = Helper.Compress(array); session.Send(MessageHelper.CreateRemoteImageMessage(compressed, this.userName, userName, null)); g.Dispose(); catchBmp.Dispose(); stream.Dispose(); }
public void Close(int statusCode, string reason) { m_ClosedArgs = new ClosedEventArgs((short)statusCode, reason); if (Interlocked.CompareExchange(ref m_StateCode, 3, -1) == -1) { OnClosed(); } else if (Interlocked.CompareExchange(ref m_StateCode, 2, 0) == 0) { TcpClientSession client = Client; if (client != null && client.IsConnected) { client.Close(); } else { OnClosed(); } } else { m_StateCode = 2; ClearTimer(); m_WebSocketTimer = new Timer(CheckCloseHandshake, null, 5000, -1); try { ProtocolProcessor.SendCloseHandshake(this, statusCode, reason); } catch (Exception e) { if (Client != null) { OnError(e); } } } }
private void TcpClientAccepted(TcpClientSession session) { session.Received += (s, e) => { if (e.Header.Key == MessageKeys.Login) { var userName = e.Header.From; session.Send(MessageHelper.CreateLiteralMessage($"Hello,{userName}", null, userName, "system greeting")); users.Add(new User() { Session = session, UserName = userName }); Print($"{userName} is logged in."); } else if (e.Header.To != null) { var user = users.FirstOrDefault(u => u.UserName == e.Header.To); if (user == null) { session.Send(MessageHelper.CreateLiteralMessage($"user {e.Header.To} not found.", null, e.Header.From, "system reply")); } else { user.Session.Send(e); } } else if (e.Header.Key == MessageKeys.Literal) { var msg = (BytesMessage)e; Print($"{e.Header.From}:{MessageHelper.LiteralEncoding.GetString(msg.Bytes)}"); } else if (e.Header.Key == MessageKeys.CloseSession) { session.Disconnect("client disconnected"); } }; }