/// <summary> /// Internal ID /// 0 - Disconnecting /// 2 - Name Change /// 3 - Initialized Connection Msg /// 4 - Extended Msg (stub) /// 5 - RC data(stub) /// 6 - Chat /// -> 0 - Broadcast to all /// -> 1 - /// -> 2 - /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected override bool OnRecv(BaseNetState state, byte msgType, object data) { TcpState tcpState = state as TcpState; switch (msgType) { case 0: //if (console != null) console.Print("Server", "Info", string.Format("Client {0} Disconnect ({1})", e.ClientID, e.FormattedData)); RemoveClient(state.ID); return true; case 2: //verify state.Name = VerifyName((string)data); //send byte[] sendingData = msgTypes[msgType].ServerFormatData(state.Name); tcpState.Connection.BeginSend(sendingData, 0, state.Name.Length + 8, 0, new AsyncCallback(SendAck), state); return true; case 3: if ((byte)data == 0) { state.Initialized = true; //if (console != null) console.Print("Server", "Info", "Client (" + state.id + ")" + state.connection.LocalEndPoint + " Connected"); RaiseOnConnect(new ConnectionArgs(state.ID, state.Name)); } //send names //byte[] sending = PackageNames(); //state.connection.BeginSend(sending, 0, sending.Length, 0, new AsyncCallback(SendAck), state); return true; case 6: ChatData chat = data as ChatData; byte[] payload = msgTypes[msgType].ServerFormatData(chat.Message); if (chat.Recievers == null) { for (int i = 0; i < states.Length; i++) { TcpState recipiantState = states[i]; if (recipiantState == null) continue; recipiantState.Connection.BeginSend(payload, 0, payload.Length, 0, new AsyncCallback(SendAck), recipiantState); } return true; } foreach (byte recipiant in chat.Recievers) { TcpState recipiantState = states[recipiant]; recipiantState.Connection.BeginSend(payload, 0, payload.Length, 0, new AsyncCallback(SendAck), recipiantState); } return true; } return false; }
protected override bool OnRecv(BaseNetState state, byte msgType, object data) { switch (msgType) { case 0: //console.Print("Client", "Info", string.Format("Server shutting down({0})", e.FormattedData)); RaiseOnDisconnect(new ConnectionArgs(serverState.ID, serverState.Name)); return true; case 2: serverState.Name = (string)data; //Temporary - Initialize connection byte[] sending = msgTypes[3].ClientFormatData((byte)0); serverState.Connection.BeginSend(sending, 0, sending.Length, 0, new AsyncCallback(SendAck), serverState); return true; case 3: serverState.ID = (byte)data; //console.Print("Client", "Info", "Connected to server. Received id of " + serverState.id); byte[] sendingData = msgTypes[msgType].ClientFormatData((byte)0); RaiseOnConnect(new ConnectionArgs(serverState.ID, serverState.Name)); return true; case 6: //console.Print("Client", "Info", "Server sent chat"); if (OnChat != null) OnChat(this, new NetChatArgs(255, (string)data)); return true; } return false; }