public void SendMessage(int Channel, byte[] data, SharpClient client) { SharpSerializer ser = SharpSerializer.Create(); ser.Write((byte)SharpConnectionMessages.DirectData); ser.Write((short)0); ser.Write(Channel); ser.Write(data.Length); ser.Write(data); if (client.isMine) { S_OnReceivedData(serverclient, Channel, data); } else { client.SendMessage(ser.DataAndPost()); } if (ser.inUse) { ser.Post(); } }
void S_OnClientDisconnected(SharpClient client) { #region Broadcasting Other Clients SharpSerializer ser = SharpSerializer.Create(); ser.Write((byte)SharpConnectionMessages.ClientDisconnected); ser.Write(client.NetworkID); Broadcast(ser.DataAndPost(), client); #endregion SharpNetworking.Instance.OnClientDisconnected(client); room.RemoveClient(client); #region Closing Socket & Thread try { TCPMessageHandler.CloseConnection(client.Connection); } catch { } try { client.ListeningThread.Abort(); } catch { } client.ListeningThread = null; #endregion }
void HandleAcceptedConnection(TcpClient client, SharpClient sclient) { SharpSerializer syncPack = SharpSerializer.Create(); syncPack.Write((byte)IncomingConnectionResponse.Approved); short netID = this.room.NextFreeID; sclient.AssingNetID(netID); syncPack.Write(netID); this.room.Write(ref syncPack); TCPMessageHandler.Write(client, syncPack.DataAndPost()); this.room.AssignClient(sclient); Thread t = new Thread(() => ListenClient(sclient)); sclient.ListeningThread = t; sclient.Connection = client; t.Start(); S_OnClientConnected(sclient); }
static void Main(string[] args) { try { SharpClient client = new SharpClient(); client.OnConnect = () => { Console.WriteLine("OnConnect"); string msg = "hello from sharpclient"; client.Write(Encoding.UTF8.GetBytes(msg)); }; client.OnClose = () => { Console.WriteLine("close now"); }; client.OnMessage = (byte[] msg) => { Console.WriteLine("read :" + Encoding.UTF8.GetString(msg)); }; client.Connect("localhost", 28800); } catch (Exception e) { Console.WriteLine(e); } Console.ReadKey(); }
static void Main(string[] args) { try { SharpClient client = new SharpClient(); client.SetChannel("test_1"); client.OnConnect = () => { Console.WriteLine("OnConnect"); string msg = "hello from sharpclient"; client.Write(Encoding.UTF8.GetBytes(msg)); }; client.OnClose = () => { Console.WriteLine("close now"); }; client.OnMessage = (byte[] msg) => { Console.WriteLine("read :" + Encoding.UTF8.GetString(msg)); }; //client.Connect("jgate.qipai.io", 18666);//公网 client.Connect("localhost", 18666);//内网 } catch (Exception e) { Console.WriteLine(e); } Console.ReadKey(); }
void C_OnReceivedMessage(byte[] rawMessage) { SharpSerializer ser = SharpSerializer.Create(rawMessage); SharpConnectionMessages incomingType = (SharpConnectionMessages)ser.ReadByte(); if (incomingType == SharpConnectionMessages.ClientConnected) { SharpClient incomingClient = new SharpClient(ref ser); this.room.AssignClient(incomingClient); C_OnClientConnected(incomingClient); } else if (incomingType == SharpConnectionMessages.ClientDisconnected) { short netID = ser.ReadInt16(); C_OnClientDisconnected(this.room.Get(netID)); } else if (incomingType == SharpConnectionMessages.DirectData) { short sender = ser.ReadShort(); int channel = ser.ReadInt32(); int packageSize = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(packageSize); C_OnReceivedData(this.room.Get(sender), channel, rawData); } ser.Post(); }
public void Disconnect() { if (!connected) { throw new Exception("You are not connected already"); } connected = false; ip = ""; port = 0; room = null; me = null; try { TCPMessageHandler.CloseConnection(server_client); } catch { } try { listenerThread.Abort(); listenerThread = null; } catch { } }
void HandleIncomingConnectionRequest(SharpSerializer incomingData, TcpClient client) { IncomingConnectionRequests request = (IncomingConnectionRequests)incomingData.ReadByte(); if (request == IncomingConnectionRequests.RoomDetails) { //Sending Room Details And Goodby SharpRoomDetails room_details = this.room.GetAsDetails(); SharpSerializer respond = SharpSerializer.Create(); room_details.Write(ref respond); TCPMessageHandler.Write(client, respond.DataAndPost()); TCPMessageHandler.CloseConnection(client); return; } else if (request == IncomingConnectionRequests.ConnectionApprove) { SharpSerializer respond = SharpSerializer.Create(); SharpClient incomingClient = new SharpClient(ref incomingData); if (this.room.isFull) { respond.Write((byte)IncomingConnectionResponse.ServerIsFull); TCPMessageHandler.Write(client, respond.DataAndPost()); TCPMessageHandler.CloseConnection(client); return; } if (this.room.isExistName(incomingClient.ClientName)) { respond.Write((byte)IncomingConnectionResponse.NameIsExist); TCPMessageHandler.Write(client, respond.DataAndPost()); TCPMessageHandler.CloseConnection(client); return; } string incomingPassword = incomingData.ReadString(); if (!this.room.isValidPassword(incomingPassword)) { respond.Write((byte)IncomingConnectionResponse.WrongPassword); TCPMessageHandler.Write(client, respond.DataAndPost()); TCPMessageHandler.CloseConnection(client); return; } HandleAcceptedConnection(client, incomingClient); } else { TCPMessageHandler.CloseConnection(client); } }
public void Disconnect() { if (!hosted) { throw new Exception("You are not hosted already"); } hosted = false; foreach (var item in this.room.ConnectedClients) { try { TCPMessageHandler.CloseConnection(item.Connection); } catch { } try { item.ListeningThread.Abort(); } catch { } item.ListeningThread = null; } port = 0; serverclient = null; room = null; try { listener.Stop(); } catch { } try { listener = null; } catch { } try { S_StoppedHosted(); } catch { } try { listenerThread.Abort(); listenerThread = null; } catch { } }
void Broadcast(byte[] data, SharpClient except) { foreach (var item in this.room.ConnectedClients) { if (item == except) { continue; } item.SendMessage(data); } }
void S_OnClientConnected(SharpClient client) { #region Broadcasting To Other clients SharpSerializer ser = SharpSerializer.Create(); ser.Write((byte)SharpConnectionMessages.ClientConnected); client.Write(ref ser); Broadcast(ser.DataAndPost(), client); #endregion SharpNetworking.Instance._OnClientConnected(client); }
public static bool Connect(string IP, int Port, string Password, SharpClient self, out ConnectResults result) { if (IsConnected) { result = ConnectResults.AlreadyConnected; return(false); } bool succes = isconnected = client.Connect(IP, Port, Password, self, out result); _ishost = !succes; return(succes); }
public void SendMessage(int Channel, byte[] data, SharpClient client) { SharpSerializer ser = SharpSerializer.Create(); ser.Write((byte)SharpConnectionMessages.MessageType2); ser.Write(client.NetworkID); ser.Write(Channel); ser.Write(data.Length); ser.Write(data); Server.SendMessage(ser.DataAndPost()); }
void ListenClient(SharpClient client) { while (true) { byte[] package = null; try { package = TCPMessageHandler.Read(client.Connection); } catch { S_OnClientDisconnected(client); return; } S_OnReceivedMessage(client, package); } }
public bool Host(out HostResult result, int Port, SharpRoom room) { if (hosted) { result = HostResult.AlreadyHosted; return(false); } try { this.port = Port; this.room = room; IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, this.port); listener = new TcpListener(endPoint); listener.Start(); serverclient = new SharpClient("ServerClient"); room.AssignClient(serverclient); listenerThread = new Thread(ListenClients); listenerThread.Start(); hosted = true; S_OnHosted(); result = HostResult.Succes; return(true); } catch (Exception e) { try { listener.Stop(); } catch { } try { listenerThread.Abort(); } catch { } listenerThread = null; listener = null; hosted = false; result = HostResult.UnhandledException; return(false); } }
protected void OnDoubleClickTab(SharpClient.UI.Controls.TabControl tc, SharpClient.UI.Controls.TabPage page) { Content c = (Content)page.Tag; // Make Content record its current position and remember it for the future c.RecordRestore(); // Invert docked status c.Docked = (c.Docked == false); // Remove from current WindowContent and restore its position _manager.HideContent(c, false, true); _manager.ShowContent(c); }
void C_OnReceivedData(SharpClient sender, int channel, byte[] data) { //Console.WriteLine(sender.ClientName + " : " + Encoding.UTF8.GetString(data)); SharpNetworking.Instance.OnMessageReceived(sender, channel, data); }
void C_OnClientDisconnected(SharpClient client) { //Console.WriteLine("A Player Disconnected : " + client.ClientName); SharpNetworking.Instance.OnClientDisconnected(client); }
public DrawTab(SharpClient.UI.Controls.TabPage tabPage, Rectangle drawRect, int index) { _index = index; _tabPage = tabPage; _drawRect = drawRect; }
void S_OnReceivedData(SharpClient sender, int channel, byte[] data) { SharpNetworking.Instance.OnMessageReceived(sender, channel, data); }
void S_OnReceivedMessage(SharpClient sender, byte[] data) { SharpSerializer ser = SharpSerializer.Create(data); SharpConnectionMessages type = (SharpConnectionMessages)ser.ReadByte(); if (type == SharpConnectionMessages.MessageType1) { #region Message Type 1 SharpTargets targets = (SharpTargets)ser.ReadByte(); int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); if (targets == SharpTargets.Server) { S_OnReceivedData(sender, channel, rawData); return; } SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); if (targets == SharpTargets.All) { Broadcast(package.DataAndPost(), null); S_OnReceivedData(sender, channel, rawData); } else if (targets == SharpTargets.ClientsOnly) { Broadcast(package.DataAndPost(), null); } else if (targets == SharpTargets.OtherClientsOnly) { Broadcast(package.DataAndPost(), sender); } else if (targets == SharpTargets.Others) { Broadcast(package.DataAndPost(), sender); S_OnReceivedData(sender, channel, rawData); } #endregion } else if (type == SharpConnectionMessages.MessageType2) { #region Message Type 2 short targetID = ser.ReadInt16(); int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); if (targetID == 0) { S_OnReceivedData(sender, channel, rawData); return; } SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); this.room.Get(targetID).SendMessage(package.DataAndPost()); #endregion } else if (type == SharpConnectionMessages.MessageType3) { #region Message Type 3 short targetCount = ser.ReadInt16(); short[] targets = new short[targetCount]; for (int i = 0; i < targetCount; i++) { targets[i] = ser.ReadInt16(); } int channel = ser.ReadInt32(); int size = ser.ReadInt32(); byte[] rawData = ser.ReadBytes(size); SharpSerializer package = SharpSerializer.Create(); package.Write((byte)SharpConnectionMessages.DirectData); package.Write(sender.NetworkID); package.Write(channel); package.Write(size); package.Write(rawData); byte[] goData = package.DataAndPost(); for (int i = 0; i < targets.Length; i++) { if (targets[i] == 0) { S_OnReceivedData(sender, channel, rawData); } else { this.room.Get(targets[i]).SendMessage(goData); } } #endregion } }
// public void AddStackControlTab(string ContentTitle,string uniqueTitle,int imageindex,SharpClient.UI.Docking.State _docState,string stackHeader,string stackData,string details,int ColumnHeaderValue,bool sortable) // { // try // { // SharpClient.UI.Controls.TabControl stackTabControl=AddPanelContents(ContentTitle,imageindex,_docState,true); // // foreach(SharpClient.UI.Controls.TabPage t in stackTabControl.TabPages) // { // if(Convert.ToString(t.Tag).ToLower()==uniqueTitle.ToLower() ) // { // foreach(Control control in t.Controls) // { // if(control.GetType()==typeof(PGRptControl.StackControl)) // { // ((PGRptControl.StackControl)control).RefreshData(stackHeader,stackData,details,ColumnHeaderValue); // break; // } // } // stackTabControl.SelectedTab=t; // t.Show(); // return; // } // } // // SharpClient.UI.Controls.TabPage tPage=new SharpClient.UI.Controls.TabPage(uniqueTitle); // tPage.Tag=uniqueTitle; // tPage.Dock=DockStyle.Fill; // stackTabControl.TabPages.Add(tPage); // PGRptControl.StackControl stackControl=new PGRptControl.StackControl(sortable); // stackControl.RefreshData(stackHeader,stackData,details,ColumnHeaderValue); // stackControl.Dock=DockStyle.Fill; // tPage.Controls.Add(stackControl); // stackTabControl.SelectedTab=tPage; // tPage.Show(); // } // catch(Exception ex) // { // MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK ,MessageBoxIcon.Error); // } // } public void AddStackControlTab(string ContentTitle,string uniqueTitle,int imageindex,SharpClient.UI.Docking.State _docState,string stackHeader,string stackData,string details,int ColumnHeaderValue,bool wizard,bool sortable) { try { SharpClient.UI.Controls.TabControl stackTabControl=AddPanelContents(ContentTitle,imageindex,_docState,true); foreach(SharpClient.UI.Controls.TabPage t in stackTabControl.TabPages) { if(Convert.ToString(t.Tag).ToLower()==uniqueTitle.ToLower() ) { foreach(Control control in t.Controls) { if(control.GetType()==typeof(PGRptControl.StackControl)) { ((PGRptControl.StackControl)control).RefreshData(stackHeader,stackData,details,ColumnHeaderValue); break; } } stackTabControl.SelectedTab=t; t.Show(); return; } } stackTabControl.ClosePressed-=new EventHandler(stackTabControl_ClosePressed); //Remove the closing option stackTabControl.ShowArrows=false; stackTabControl.ShowClose=false; SharpClient.UI.Controls.TabPage tPage=new SharpClient.UI.Controls.TabPage(uniqueTitle); tPage.Tag=uniqueTitle; tPage.Dock=DockStyle.Fill; stackTabControl.TabPages.Add(tPage); PGRptControl.StackControl stackControl=new PGRptControl.StackControl(sortable); stackControl.Wizard= wizard; stackControl.StackControl_ItemSelected+=new PGRptControl.StackControl_ItemSelectedHandler(stackControl_StackControl_ItemSelected); stackControl.RefreshData(stackHeader,stackData,details,ColumnHeaderValue); stackControl.Dock=DockStyle.Fill; tPage.Controls.Add(stackControl); stackTabControl.SelectedTab=tPage; tPage.Show(); } catch(Exception ex) { MessageBox.Show(ex.Message,"Error",MessageBoxButtons.OK ,MessageBoxIcon.Error); } }
public bool Connect(string IP, int Port, string Password, SharpClient Self, out ConnectResults result) { if (Password == null) { Password = ""; } if (this.connected) { result = ConnectResults.AlreadyConnected; return(false); } try { server_client = new TcpClient(); IPAddress address = null; #region IP Parse try { address = IPAddress.Parse(IP); } catch { result = ConnectResults.InvalidIpAddress; return(false); } #endregion IPEndPoint remoteAddress = new IPEndPoint(address, Port); #region Physical Connetion try { server_client.Connect(remoteAddress); } catch { result = ConnectResults.UnhandledException; return(false); } #endregion #region ID Self SharpSerializer hailMessage = SharpSerializer.Create(); hailMessage.Write((byte)IncomingConnectionRequests.ConnectionApprove); Self.Write(ref hailMessage); hailMessage.Write(Password); TCPMessageHandler.Write(server_client, hailMessage.DataAndPost()); #endregion #region Wait For Response byte[] responseRaw = TCPMessageHandler.Read(server_client); SharpSerializer response = SharpSerializer.Create(responseRaw); #endregion IncomingConnectionResponse message = (IncomingConnectionResponse)response.ReadByte(); if (message == IncomingConnectionResponse.WrongPassword) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.InvalidPassword; return(false); } if (message == IncomingConnectionResponse.ServerIsFull) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.ServerIsFull; return(false); } if (message == IncomingConnectionResponse.NameIsExist) { TCPMessageHandler.CloseConnection(server_client); result = ConnectResults.NameIsExist; return(false); } short myID = response.ReadInt16(); Self.AssingNetID(myID); this.room = new SharpRoom(ref response); this.listenerThread = new Thread(ListenServer); this.listenerThread.Start(); connected = true; me = Self; room.AssignClient(me); room.Server.Connection = server_client; room.Server.ListeningThread = listenerThread; C_OnConnected(room); result = ConnectResults.Succes; return(true); } catch (Exception e) { try { TCPMessageHandler.CloseConnection(server_client); } catch { } server_client = null; connected = false; try { listenerThread.Abort(); } catch { } listenerThread = null; me = null; room = null; result = ConnectResults.UnhandledException; return(false); } }
private SharpClient.UI.Controls.TabControl AddPanelContents(string ContentTitle,int imageListIndex,SharpClient.UI.Docking.State _dockstate,bool bShow) { SharpClient.UI.Controls.TabControl stackTabControl=null; Content cStack=null; if(_stackManager==null) { _stackManager=new DockingManager(this,SharpClient.UI.Common.VisualStyle.IDE); _stackManager.InnerControl=sharpClientMDITab; _stackManager.OuterControl= psStatusBar; } if(_stackManager.Contents[ContentTitle]==null) { stackTabControl=new SharpClient.UI.Controls.TabControl(); stackTabControl.Appearance = SharpClient.UI.Controls.TabControl.VisualAppearance.MultiDocument; stackTabControl.BackColor = System.Drawing.SystemColors.ActiveCaptionText; stackTabControl.ButtonActiveColor = System.Drawing.SystemColors.ActiveCaptionText; stackTabControl.ButtonInactiveColor = System.Drawing.SystemColors.InactiveCaptionText; stackTabControl.Dock = System.Windows.Forms.DockStyle.Fill; stackTabControl.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); stackTabControl.HideTabsMode = SharpClient.UI.Controls.TabControl.HideTabsModes.ShowAlways; stackTabControl.HotTextColor = System.Drawing.SystemColors.Desktop; stackTabControl.IDEPixelBorder = false; stackTabControl.Location = new System.Drawing.Point(0, 31); stackTabControl.Size = new System.Drawing.Size(790, 484); stackTabControl.ClosePressed+=new EventHandler(stackTabControl_ClosePressed); cStack=_stackManager.Contents.Add(stackTabControl,ContentTitle,imageList1,imageListIndex ); _stackManager.AddContentWithState(cStack,_dockstate); } else { cStack=_stackManager.Contents[ContentTitle]; stackTabControl=cStack.Control as SharpClient.UI.Controls.TabControl; } ShowStackControl(bShow,cStack); return stackTabControl; }
protected override void calculateInsets(SharpClient.UI.Charting.Insets insets, System.Drawing.Graphics g) { base.calculateInsets(insets, g); generateAxisLabel(); setMaxXLabelHeight(g); setMaxYLabelWidth(g); insets.Left += (m_maxYLabelWidth + m_maxXLabelHeight); //insets.Top += m_maxXLabelHeight; insets.Bottom += 3 * m_maxXLabelHeight; insets.Right += 2 * m_maxYLabelWidth; }