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); }
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 }
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 ListenServer() { while (true) { byte[] incomingPackage = null; try { incomingPackage = TCPMessageHandler.Read(this.server_client); } catch { C_OnDisconnected(); return; } C_OnReceivedMessage(incomingPackage); } }
void ListenClient(SharpClient client) { while (true) { byte[] package = null; try { package = TCPMessageHandler.Read(client.Connection); } catch { S_OnClientDisconnected(client); return; } S_OnReceivedMessage(client, package); } }
void HandleIncomingConnection(TcpClient client) { Stopwatch timeout = Stopwatch.StartNew(); LockableBool isdone = new LockableBool(); byte[] output = TCPMessageHandler.Read(client); //LockableObject<byte[]> output = null; //Thread thread; //TCPMessageHandler.Read(client, isdone, output, out thread); //while (!isdone) //{ // Thread.Sleep(1); // if (timeout.ElapsedMilliseconds > this.timeout) // { // try { thread.Abort(); thread = null; } catch { } // TCPMessageHandler.CloseConnection(client); // return; // } //} SharpSerializer package = SharpSerializer.Create(output); try { HandleIncomingConnectionRequest(package, client); } catch (Exception e) { TCPMessageHandler.CloseConnection(client); } package.Post(); }
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); } }